Guest User

Untitled

a guest
May 5th, 2018
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1.  
  2. =encoding utf8
  3.  
  4. =head1 TITLE
  5.  
  6. DRAFT: Synopsis 32: Setting Library - Temporal
  7.  
  8. =head1 AUTHORS
  9.  
  10. Carl Mäsak <cmasak@gmail.com>
  11. Martin Berends <mberends@autoexec.demon.nl>
  12. (but see FOOTNOTE at bottom)
  13.  
  14. =head1 VERSION
  15.  
  16. Created: 19 Mar 2009
  17.  
  18. Last Modified: 8 Apr 2010
  19. Version: 7
  20.  
  21. The document is a draft.
  22.  
  23. If you read the HTML version, it is generated from the Pod in the pugs
  24. repository under /docs/Perl6/Spec/S32-setting-library/Temporal.pod -- if you
  25. would like to make changes to the document, that's the place to look.
  26.  
  27. =head1 Time and time again
  28.  
  29. Two chief aspects of a Perl 6 synopsis seem to contribute to it having some
  30. extra volatility: how far it sits from the rest of the data model of the
  31. language, and how everyday the topic in question is. C<S32> has always been
  32. volatile for these reasons; C<S32::Temporal> doubly so.
  33.  
  34. The truth is that while there are many interests to satisfy in the case of a
  35. C<Temporal> module, and many details to take into account, there's also the
  36. danger of putting too much in. Therefore, Perl 6's C<Temporal> module takes
  37. the C<DateTime> module on CPAN as a starting point, adapts it to the Perl 6
  38. OO system, and boils it down to bare essentials.
  39.  
  40. One of the unfortunate traditions that Perl 6 aims to break is that of having a
  41. set of "core" modules which could better serve the community on CPAN than in
  42. the Perl core. For this reason, this module doesn't handle all the world's
  43. time zones, locales, date formatters or calendars. Instead, it handles a number
  44. of "natural" operations well enough for most people to be happy, and shows how
  45. those who want more than that can load a module, or roll their own variants.
  46. Put differently, the below are the aspects of time that are felt to be stable
  47. enough to belong in the core.
  48.  
  49. =head1 C<time>
  50.  
  51. Returns an C<Instant> representing the current time as measured in atomic
  52. second since the epoch, suitable for feeding to some of the C<DateTime>
  53. constructors.
  54.  
  55. =head1 C<DateTime>
  56.  
  57. A C<DateTime> object describes the time as it would appear on someone's
  58. calendar and someone's clock. You can create a C<DateTime> object from the
  59. C<Instant> returned by the C<time> function:
  60.  
  61. my $now = DateTime.from_epoch(time);
  62.  
  63. This is such a common use case, that there's a C<DateTime.now> constructor
  64. that does this for you:
  65.  
  66. my $now = DateTime.now();
  67.  
  68. If you're interested in the current date but not the time, you can use
  69. the C<today> method instead:
  70.  
  71. my $today = DateTime.today();
  72.  
  73. This has the same effect as doing C<DateTime.now().truncate('day')>; see
  74. '"Set" methods' below.
  75.  
  76. General dates can be specified through the C<new> constructor:
  77.  
  78. my $moonlanding = DateTime.new( :year(1969), :month(7), :day(16),
  79. :hour(20), :minute(17) ); # UTC time
  80.  
  81. The full list of named arguments is as follows:
  82.  
  83. :year required
  84. :month defaults to 1 range 1..12
  85. :day defaults to 1 range 1..31
  86. :hour defaults to 0 range 0..23
  87. :minute defaults to 0 range 0..59
  88. :second defaults to 0 range 0..61
  89. :nanosecond defaults to 0
  90. :timezone defaults to '+0000' (UTC)
  91. :formatter defaults to an iso8601 formatter, see below
  92.  
  93. A shorter way to send in date and time information to is providing a single
  94. string with a full ISO8601 date and time. The example from above would then
  95. be
  96.  
  97. my $moonlanding = DateTime.new( '1969-07-16T20:17:00Z' ); # UTC time
  98.  
  99. The general form is C<< <date>T<time><offset> >>, with C<< <date> >> given
  100. as C<YYYY-MM-DD> and C<< <time> >> given as C<hh:mm:ss>.
  101.  
  102. The final C<Z> is a short form for C<+0000>, meaning UTC time. The general
  103. notation for the C<< <offset> >> is C<+hhmm> or C<-hhmm>.
  104.  
  105. With all the above constructors, if you attempt to pass in values that are
  106. outside of the ranges specified in the list above, you'll get an exception.
  107. An exception will also be thrown if the particular day doesn't exist in that
  108. month (for example April 31st) or in that non-leap year (for example February
  109. 29th 1996). By default, no such checking is done against leap seconds. This
  110. class also explicitly does not check against ambiguous or invalid local times
  111. caused by Daylight Saving Time.
  112.  
  113. If you pass in a C<:nanosecond> value greater or equal to one billion (1e9),
  114. it will be normalized, and the excess seconds will be transferred to the
  115. C<:second> value.
  116.  
  117. =head2 "Get" methods
  118.  
  119. There are methods C<year>, C<month>, C<day>, C<hour>, C<minute>, C<second>,
  120. and C<nanosecond>, giving you the corresponding values of the C<DateTime>
  121. object. The C<day> method also has the synonym C<day_of_month>.
  122.  
  123. The method C<week> returns two values, the I<week year> and I<week number>.
  124. (These are also available through the methods C<week_year> and C<week_number>,
  125. respectively.) The first week of the year is defined by ISO as the one which
  126. contains the fourth day of January. Thus, dates early in January often end
  127. up in the last week of the prior year, and similarly, the final few days of
  128. December may be placed in the first week of the next year.
  129.  
  130. There's a C<day_of_week> method, which returns the day of the week as a number
  131. 1..7, with 1 being Monday and 7 being Sunday.
  132.  
  133. The C<weekday_of_month> method returns a number 1..5 indicating the number of
  134. times a particular weekday has occurred so far during that month, the day
  135. itself included. For example, June 9, 2003 is the second Monday of the month,
  136. and so this method returns 2 for that day.
  137.  
  138. The C<quarter> method returns the quarter of the year, a value between 1 and 4.
  139. The C<day_of_quarter> method returns the day of the quarter.
  140.  
  141. The C<day_of_year> method returns the day of the year, a value between 1 and
  142. 366.
  143.  
  144. The method C<fractional_second> returns the second as a real number, with the
  145. fractional part coming from the C<nanosecond> value. The methods
  146. C<millisecond>, C<microsecond>, and C<nanosecond> return the nanosecond part
  147. in the corresponding unit, rounded to an integer.
  148.  
  149. The following methods work as a sort of formatting methods:
  150.  
  151. $dt.ymd('-') (also $dt.date('-'))
  152. $dt.mdy('-')
  153. $dt.dmy('-')
  154.  
  155. $dt.hms(':') (also $dt.time(':'))
  156.  
  157. The single argument of each of those methods is optional, but the above shows
  158. the defaults: C<'-'> for dates and C<':'> for times.
  159.  
  160. The C<time_zone> method returns the C<DateTime::TimeZone> object for the
  161. C<DateTime> object. The method C<offset> returns the offset from UTC, in
  162. seconds, of the C<DateTime> object according to the time zone.
  163.  
  164. The C<formatter> method returns the formatter for the C<DateTime> object.
  165.  
  166. =head2 "Set" methods
  167.  
  168. To set the the day of a C<DateTime> object to something, just assign to its
  169. public accessor:
  170.  
  171. $dt.day = 15;
  172.  
  173. The same methods exists for all the values you can set in the constructor:
  174. C<year>, C<month>, C<day>, C<hour>, C<minute>, C<second>, C<nanosecond>,
  175. C<time_zone> and C<formatter>. Also, there's a C<set> method, which accepts
  176. all of these as named arguments, allowing several values to be set at once:
  177.  
  178. $dt.set( :year(2014), :month(12), :day(25) );
  179.  
  180. Just as with the C<new> method, validation is performed on the resulting
  181. values, and an exception is thrown if the result isn't a sensible date and
  182. time.
  183.  
  184. If you use the C<time_zone> public accessor to adjust the time zone, the
  185. local time zone is adjusted accordingly:
  186.  
  187. my $dt = DateTime.new('2005-02-01T15:00:00+0900');
  188. say $dt.hour; # 15
  189. $dt.time_zone = '+0600';
  190. say $dt.hour; # 12
  191.  
  192. The C<truncate> method allows you to "clear" a number of time values below
  193. a given resolution:
  194.  
  195. $dt.truncate( :to<hour> ); # clears minutes, seconds, and nanoseconds
  196.  
  197. The time units are "cleared" in the sense that they are set to their inherent
  198. defaults: 1 for months and days, 0 for the time components.
  199.  
  200. If you pass in C<< :to<week> >>, the C<DateTime> object is set to the Monday
  201. of the week in which it occurs, and the time components are all set to 0.
  202.  
  203. =head1 Additions
  204.  
  205. Please post errors and feedback to C<perl6-language>. If you are making
  206. a general laundry list, please separate messages by topic. Please try to
  207. keep bikeshedding down.
  208.  
  209. =head1 FOOTNOTE
  210.  
  211. The indirect contribution of the previous authors prevents the authors of
  212. the current rewrite to fail to mention:
  213.  
  214. The authors of the related Perl 5 docs
  215. Rod Adams <rod@rodadams.net>
  216. Larry Wall <larry@wall.org>
  217. Aaron Sherman <ajs@ajs.com>
  218. Mark Stosberg <mark@summersault.com>
  219. Carl Mäsak <cmasak@gmail.com>
  220. Moritz Lenz <moritz@faui2k3.org>
  221. Tim Nelson <wayland@wayland.id.au>
  222. Daniel Ruoso <daniel@ruoso.com>
  223. Dave Rolsky <autarch@urth.org>
  224. Matthew (lue) <rnddim@gmail.com>
Add Comment
Please, Sign In to add comment