|
| 1 | +=head1 Wibbly-Wobbly Timey-Wimey |
| 2 | + |
| 3 | +It's the 0x10th day of Christmas, and it's time for you to learn of time. The synopsis S32::Temporal has been heavily revised in the past year, and today's post details some of the basics of time as it is implemented in Perl 6. |
| 4 | + |
| 5 | +=head2 time and now |
| 6 | + |
| 7 | +The two terms that give the current time (at least what your system thinks is the current time) are C<time> and C<now>. Here's a quick example: |
| 8 | + |
| 9 | + > say time; say now; |
| 10 | + 1292460064 |
| 11 | + Instant:2010-12-16T00:41:4.873248Z |
| 12 | + |
| 13 | +The first (obvious) difference is that C<time> returns POSIX time, as an integer. C<now> returns an object known as an C<Instant>. Use C<now> if you want fractions of a second and recognition of leap seconds. C<time> won't give you fractions of a second or leap seconds, because it returns POSIX time. Which one you use all depends on what you need. |
| 14 | + |
| 15 | +=head2 DateTime and friend |
| 16 | + |
| 17 | +Most of the time, you will want to store dates other than now. For this, the C<DateTime> object is what you need. If you want to store the current time, you can use: |
| 18 | + |
| 19 | + my $moment = DateTime.new(now); # or DateTime.new(time) |
| 20 | + |
| 21 | +Otherwise, there are two ways of creating a DateTime object: |
| 22 | + |
| 23 | + my $dw = DateTime.new(:year(1963), :month(11), :day(23), :hour(17), :minute(15)); |
| 24 | + |
| 25 | +This is in UTC, if you want to enter it in in another timezone, use the C<:timezone> adverb. Here, only C<:year> is required, the rest defaults to midnight on January 1 of the year. |
| 26 | + |
| 27 | +This way is also pretty tedious. You could instead create a DateTime object by inputting an ISO 8601 timestamp, as a string. |
| 28 | + |
| 29 | + my $dw = DateTime.new("1963-11-23T17:15:00Z"); |
| 30 | + |
| 31 | +The Z denotes UTC. To change that, replace Z with +hhmm or -hhmm, where 'hh' is the number of hours offset and 'mm' the number of minutes. |
| 32 | + |
| 33 | +There is also a Date object, which is created in a similar way, but without hours, minutes, or seconds. For example: |
| 34 | + |
| 35 | + my $jfk = DateTime.new("1963-11-22"); # you can also use :year and so on |
| 36 | + |
| 37 | +=head2 Finally... |
| 38 | + |
| 39 | +That's about it for Time in P6. To see all the gritty details go to http://perlcabal.org/syn/S32/Temporal.html or ask about it in the community! |
0 commit comments