home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Piece.pm < prev    next >
Encoding:
Perl POD Document  |  2002-12-22  |  18.5 KB  |  763 lines

  1. # $Id: Piece.pm,v 1.16 2002/12/22 11:17:48 matt Exp $
  2.  
  3. package Time::Piece;
  4.  
  5. use strict;
  6. use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
  7.  
  8. require Exporter;
  9. require DynaLoader;
  10. use Time::Seconds;
  11. use Carp;
  12. use Time::Local;
  13. use UNIVERSAL qw(isa);
  14.  
  15. @ISA = qw(Exporter DynaLoader);
  16.  
  17. @EXPORT = qw(
  18.     localtime
  19.     gmtime
  20. );
  21.  
  22. %EXPORT_TAGS = (
  23.     ':override' => 'internal',
  24.     );
  25.  
  26. $VERSION = '1.08';
  27.  
  28. bootstrap Time::Piece $VERSION;
  29.  
  30. my $DATE_SEP = '-';
  31. my $TIME_SEP = ':';
  32. my @MON_LIST = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
  33. my @FULLMON_LIST = qw(January February March April May June July
  34.                       August September October November December);
  35. my @DAY_LIST = qw(Sun Mon Tue Wed Thu Fri Sat);
  36. my @FULLDAY_LIST = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
  37.  
  38. use constant 'c_sec' => 0;
  39. use constant 'c_min' => 1;
  40. use constant 'c_hour' => 2;
  41. use constant 'c_mday' => 3;
  42. use constant 'c_mon' => 4;
  43. use constant 'c_year' => 5;
  44. use constant 'c_wday' => 6;
  45. use constant 'c_yday' => 7;
  46. use constant 'c_isdst' => 8;
  47. use constant 'c_epoch' => 9;
  48. use constant 'c_islocal' => 10;
  49.  
  50. sub localtime {
  51.     my $time = shift;
  52.     $time = time if (!defined $time);
  53.     _mktime($time, 1);
  54. }
  55.  
  56. sub gmtime {
  57.     my $time = shift;
  58.     $time = time if (!defined $time);
  59.     _mktime($time, 0);
  60. }
  61.  
  62. sub new {
  63.     my $class = shift;
  64.     my ($time) = @_;
  65.     
  66.     my $self;
  67.     
  68.     if (defined($time)) {
  69.         $self = &localtime($time);
  70.     }
  71.     elsif (ref($class) && $class->isa(__PACKAGE__)) {
  72.         $self = _mktime($class->epoch, $class->[c_islocal]);
  73.     }
  74.     else {
  75.         $self = &localtime();
  76.     }
  77.     
  78.     return bless $self, $class;
  79. }
  80.  
  81. sub parse {
  82.     my $proto = shift;
  83.     my $class = ref($proto) || $proto;
  84.     my @components;
  85.     if (@_ > 1) {
  86.         @components = @_;
  87.     }
  88.     else {
  89.         @components = shift =~ /(\d+)$DATE_SEP(\d+)$DATE_SEP(\d+)(?:(?:T|\s+)(\d+)$TIME_SEP(\d+)(?:$TIME_SEP(\d+)))/;
  90.         @components = reverse(@components[0..5]);
  91.     }
  92.     return $class->new(_strftime("%s", @components));
  93. }
  94.  
  95. sub _mktime {
  96.     my ($time, $islocal) = @_;
  97.     if (ref($time)) {
  98.     $time->[c_epoch] = undef;
  99.     return wantarray ? @$time : bless [@$time, $islocal], 'Time::Piece';
  100.     }
  101.     my @time = $islocal ?
  102.             CORE::localtime($time)
  103.                 :
  104.             CORE::gmtime($time);
  105.     wantarray ? @time : bless [@time, $time, $islocal], 'Time::Piece';
  106. }
  107.  
  108. sub import {
  109.     # replace CORE::GLOBAL localtime and gmtime if required
  110.     my $class = shift;
  111.     my %params;
  112.     map($params{$_}++,@_,@EXPORT);
  113.     if (delete $params{':override'}) {
  114.         $class->export('CORE::GLOBAL', keys %params);
  115.     }
  116.     else {
  117.         $class->export((caller)[0], keys %params);
  118.     }
  119. }
  120.  
  121. ## Methods ##
  122.  
  123. sub sec {
  124.     my $time = shift;
  125.     $time->[c_sec];
  126. }
  127.  
  128. *second = \&sec;
  129.  
  130. sub min {
  131.     my $time = shift;
  132.     $time->[c_min];
  133. }
  134.  
  135. *minute = \&min;
  136.  
  137. sub hour {
  138.     my $time = shift;
  139.     $time->[c_hour];
  140. }
  141.  
  142. sub mday {
  143.     my $time = shift;
  144.     $time->[c_mday];
  145. }
  146.  
  147. *day_of_month = \&mday;
  148.  
  149. sub mon {
  150.     my $time = shift;
  151.     $time->[c_mon] + 1;
  152. }
  153.  
  154. sub _mon {
  155.     my $time = shift;
  156.     $time->[c_mon];
  157. }
  158.  
  159. sub month {
  160.     my $time = shift;
  161.     if (@_) {
  162.         return $_[$time->[c_mon]];
  163.     }
  164.     elsif (@MON_LIST) {
  165.         return $MON_LIST[$time->[c_mon]];
  166.     }
  167.     else {
  168.         return $time->strftime('%b');
  169.     }
  170. }
  171.  
  172. *monname = \&month;
  173.  
  174. sub fullmonth {
  175.     my $time = shift;
  176.     if (@_) {
  177.         return $_[$time->[c_mon]];
  178.     }
  179.     elsif (@FULLMON_LIST) {
  180.         return $FULLMON_LIST[$time->[c_mon]];
  181.     }
  182.     else {
  183.         return $time->strftime('%B');
  184.     }
  185. }
  186.  
  187. sub year {
  188.     my $time = shift;
  189.     $time->[c_year] + 1900;
  190. }
  191.  
  192. sub _year {
  193.     my $time = shift;
  194.     $time->[c_year];
  195. }
  196.  
  197. sub yy {
  198.     my $time = shift;
  199.     my $res = $time->[c_year] % 100;
  200.     return $res > 9 ? $res : "0$res";
  201. }
  202.  
  203. sub wday {
  204.     my $time = shift;
  205.     $time->[c_wday] + 1;
  206. }
  207.  
  208. sub _wday {
  209.     my $time = shift;
  210.     $time->[c_wday];
  211. }
  212.  
  213. *day_of_week = \&_wday;
  214.  
  215. sub wdayname {
  216.     my $time = shift;
  217.     if (@_) {
  218.         return $_[$time->[c_wday]];
  219.     }
  220.     elsif (@DAY_LIST) {
  221.         return $DAY_LIST[$time->[c_wday]];
  222.     }
  223.     else {
  224.         return $time->strftime('%a');
  225.     }
  226. }
  227.  
  228. *day = \&wdayname;
  229.  
  230. sub fullday {
  231.     my $time = shift;
  232.     if (@_) {
  233.         return $_[$time->[c_wday]];
  234.     }
  235.     elsif (@FULLDAY_LIST) {
  236.         return $FULLDAY_LIST[$time->[c_wday]];
  237.     }
  238.     else {
  239.         return $time->strftime('%A');
  240.     }
  241. }
  242.  
  243. sub yday {
  244.     my $time = shift;
  245.     $time->[c_yday];
  246. }
  247.  
  248. *day_of_year = \&yday;
  249.  
  250. sub isdst {
  251.     my $time = shift;
  252.     $time->[c_isdst];
  253. }
  254.  
  255. *daylight_savings = \&isdst;
  256.  
  257. # Thanks to Tony Olekshy <olekshy@cs.ualberta.ca> for this algorithm
  258. sub tzoffset {
  259.     my $time = shift;
  260.  
  261.     my $epoch = $time->epoch;
  262.  
  263.     my $j = sub {
  264.  
  265.         my ($s,$n,$h,$d,$m,$y) = @_; $m += 1; $y += 1900;
  266.  
  267.         $time->_jd($y, $m, $d, $h, $n, $s);
  268.  
  269.     };
  270.  
  271.     # Compute floating offset in hours.
  272.     #
  273.     my $delta = 24 * (&$j(CORE::localtime $epoch) - &$j(CORE::gmtime $epoch));
  274.  
  275.     # Return value in seconds rounded to nearest minute.
  276.     return Time::Seconds->new( int($delta * 60 + ($delta >= 0 ? 0.5 : -0.5)) * 60 );
  277. }
  278.  
  279. sub epoch {
  280.     my $time = shift;
  281.     if (defined($time->[c_epoch])) {
  282.     return $time->[c_epoch];
  283.     }
  284.     else {
  285.     my $epoch = $time->[c_islocal] ?
  286.       timelocal(@{$time}[c_sec .. c_mon], $time->[c_year]+1900)
  287.       :
  288.       timegm(@{$time}[c_sec .. c_mon], $time->[c_year]+1900);
  289.     $time->[c_epoch] = $epoch;
  290.     return $epoch;
  291.     }
  292. }
  293.  
  294. sub hms {
  295.     my $time = shift;
  296.     my $sep = @_ ? shift(@_) : $TIME_SEP;
  297.     sprintf("%02d$sep%02d$sep%02d", $time->[c_hour], $time->[c_min], $time->[c_sec]);
  298. }
  299.  
  300. *time = \&hms;
  301.  
  302. sub ymd {
  303.     my $time = shift;
  304.     my $sep = @_ ? shift(@_) : $DATE_SEP;
  305.     sprintf("%d$sep%02d$sep%02d", $time->year, $time->mon, $time->[c_mday]);
  306. }
  307.  
  308. *date = \&ymd;
  309.  
  310. sub mdy {
  311.     my $time = shift;
  312.     my $sep = @_ ? shift(@_) : $DATE_SEP;
  313.     sprintf("%02d$sep%02d$sep%d", $time->mon, $time->[c_mday], $time->year);
  314. }
  315.  
  316. sub dmy {
  317.     my $time = shift;
  318.     my $sep = @_ ? shift(@_) : $DATE_SEP;
  319.     sprintf("%02d$sep%02d$sep%d", $time->[c_mday], $time->mon, $time->year);
  320. }
  321.  
  322. sub datetime {
  323.     my $time = shift;
  324.     my %seps = (date => $DATE_SEP, T => 'T', time => $TIME_SEP, @_);
  325.     return join($seps{T}, $time->date($seps{date}), $time->time($seps{time}));
  326. }
  327.  
  328.  
  329.  
  330. # Julian Day is always calculated for UT regardless
  331. # of local time
  332. sub julian_day {
  333.     my $time = shift;
  334.     # Correct for localtime
  335.     $time = &gmtime( $time->epoch ) if $time->[c_islocal];
  336.  
  337.     # Calculate the Julian day itself
  338.     my $jd = $time->_jd( $time->year, $time->mon, $time->mday,
  339.                         $time->hour, $time->min, $time->sec);
  340.  
  341.     return $jd;
  342. }
  343.  
  344. # MJD is defined as JD - 2400000.5 days
  345. sub mjd {
  346.     return shift->julian_day - 2_400_000.5;
  347. }
  348.  
  349. # Internal calculation of Julian date. Needed here so that
  350. # both tzoffset and mjd/jd methods can share the code
  351. # Algorithm from Hatcher 1984 (QJRAS 25, 53-55), and
  352. #  Hughes et al, 1989, MNRAS, 238, 15
  353. # See: http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode=1989MNRAS.238.1529H&db_key=AST
  354. # for more details
  355.  
  356. sub _jd {
  357.     my $self = shift;
  358.     my ($y, $m, $d, $h, $n, $s) = @_;
  359.  
  360.     # Adjust input parameters according to the month
  361.     $y = ( $m > 2 ? $y : $y - 1);
  362.     $m = ( $m > 2 ? $m - 3 : $m + 9);
  363.  
  364.     # Calculate the Julian Date (assuming Julian calendar)
  365.     my $J = int( 365.25 *( $y + 4712) )
  366.       + int( (30.6 * $m) + 0.5)
  367.         + 59
  368.           + $d
  369.             - 0.5;
  370.  
  371.     # Calculate the Gregorian Correction (since we have Gregorian dates)
  372.     my $G = 38 - int( 0.75 * int(49+($y/100)));
  373.  
  374.     # Calculate the actual Julian Date
  375.     my $JD = $J + $G;
  376.  
  377.     # Modify to include hours/mins/secs in floating portion.
  378.     return $JD + ($h + ($n + $s / 60) / 60) / 24;
  379. }
  380.  
  381. sub week {
  382.     my $self = shift;
  383.  
  384.     my $J  = $self->julian_day;
  385.     # Julian day is independent of time zone so add on tzoffset
  386.     # if we are using local time here since we want the week day
  387.     # to reflect the local time rather than UTC
  388.     $J += ($self->tzoffset/(24*3600)) if $self->[c_islocal];
  389.  
  390.     # Now that we have the Julian day including fractions
  391.     # convert it to an integer Julian Day Number using nearest
  392.     # int (since the day changes at midday we oconvert all Julian
  393.     # dates to following midnight).
  394.     $J = int($J+0.5);
  395.  
  396.     use integer;
  397.     my $d4 = ((($J + 31741 - ($J % 7)) % 146097) % 36524) % 1461;
  398.     my $L  = $d4 / 1460;
  399.     my $d1 = (($d4 - $L) % 365) + $L;
  400.     return $d1 / 7 + 1;
  401. }
  402.  
  403. sub _is_leap_year {
  404.     my $year = shift;
  405.     return (($year %4 == 0) && !($year % 100 == 0)) || ($year % 400 == 0)
  406.                ? 1 : 0;
  407. }
  408.  
  409. sub is_leap_year {
  410.     my $time = shift;
  411.     my $year = $time->year;
  412.     return _is_leap_year($year);
  413. }
  414.  
  415. my @MON_LAST = qw(31 28 31 30 31 30 31 31 30 31 30 31);
  416.  
  417. sub month_last_day {
  418.     my $time = shift;
  419.     my $year = $time->year;
  420.     my $_mon = $time->_mon;
  421.     return $MON_LAST[$_mon] + ($_mon == 1 ? _is_leap_year($year) : 0);
  422. }
  423.  
  424. sub strftime {
  425.     my $time = shift;
  426.     my $format = @_ ? shift(@_) : "%a, %d %b %Y %H:%M:%S %Z";
  427.     if (!defined $time->[c_wday]) {
  428.     if ($time->[c_islocal]) {
  429.             return _strftime($format, CORE::localtime($time->epoch));
  430.     }
  431.     else {
  432.         return _strftime($format, CORE::gmtime($time->epoch));
  433.     }
  434.     }
  435.     return _strftime($format, (@$time)[c_sec..c_isdst]);
  436. }
  437.  
  438. sub strptime {
  439.     my $time = shift;
  440.     my $string = shift;
  441.     my $format = @_ ? shift(@_) : "%a, %d %b %Y %H:%M:%S %Z";
  442.     my @vals = _strptime($string, $format);
  443. #    warn(sprintf("got vals: %d-%d-%d %d:%d:%d\n", reverse(@vals)));
  444.     return scalar _mktime(\@vals, (ref($time) ? $time->[c_islocal] : 0));
  445. }
  446.  
  447. sub day_list {
  448.     shift if ref($_[0]) && $_[0]->isa(__PACKAGE__); # strip first if called as a method
  449.     my @old = @DAY_LIST;
  450.     if (@_) {
  451.         @DAY_LIST = @_;
  452.     }
  453.     return @old;
  454. }
  455.  
  456. sub mon_list {
  457.     shift if ref($_[0]) && $_[0]->isa(__PACKAGE__); # strip first if called as a method
  458.     my @old = @MON_LIST;
  459.     if (@_) {
  460.         @MON_LIST = @_;
  461.     }
  462.     return @old;
  463. }
  464.  
  465. sub time_separator {
  466.     shift if ref($_[0]) && $_[0]->isa(__PACKAGE__);
  467.     my $old = $TIME_SEP;
  468.     if (@_) {
  469.         $TIME_SEP = $_[0];
  470.     }
  471.     return $old;
  472. }
  473.  
  474. sub date_separator {
  475.     shift if ref($_[0]) && $_[0]->isa(__PACKAGE__);
  476.     my $old = $DATE_SEP;
  477.     if (@_) {
  478.         $DATE_SEP = $_[0];
  479.     }
  480.     return $old;
  481. }
  482.  
  483. use overload '""' => \&cdate,
  484.              'cmp' => \&str_compare,
  485.              'fallback' => undef;
  486.  
  487. sub cdate {
  488.     my $time = shift;
  489.     if ($time->[c_islocal]) {
  490.         return scalar(CORE::localtime($time->epoch));
  491.     }
  492.     else {
  493.         return scalar(CORE::gmtime($time->epoch));
  494.     }
  495. }
  496.  
  497. sub str_compare {
  498.     my ($lhs, $rhs, $reverse) = @_;
  499.     if (UNIVERSAL::isa($rhs, 'Time::Piece')) {
  500.         $rhs = "$rhs";
  501.     }
  502.     return $reverse ? $rhs cmp $lhs->cdate : $lhs->cdate cmp $rhs;
  503. }
  504.  
  505. use overload
  506.         '-' => \&subtract,
  507.         '+' => \&add;
  508.  
  509. sub subtract {
  510.     my $time = shift;
  511.     my $rhs = shift;
  512.     if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
  513.         $rhs = $rhs->seconds;
  514.     }
  515.     die "Can't subtract a date from something!" if shift;
  516.     
  517.     if (UNIVERSAL::isa($rhs, 'Time::Piece')) {
  518.         return Time::Seconds->new($time->epoch - $rhs->epoch);
  519.     }
  520.     else {
  521.         # rhs is seconds.
  522.         return _mktime(($time->epoch - $rhs), $time->[c_islocal]);
  523.     }
  524. }
  525.  
  526. sub add {
  527.     my $time = shift;
  528.     my $rhs = shift;
  529.     if (UNIVERSAL::isa($rhs, 'Time::Seconds')) {
  530.         $rhs = $rhs->seconds;
  531.     }
  532.     croak "Invalid rhs of addition: $rhs" if ref($rhs);
  533.  
  534.     return _mktime(($time->epoch + $rhs), $time->[c_islocal]);
  535. }
  536.  
  537. use overload
  538.         '<=>' => \&compare;
  539.  
  540. sub get_epochs {
  541.     my ($lhs, $rhs, $reverse) = @_;
  542.     if (!UNIVERSAL::isa($rhs, 'Time::Piece')) {
  543.         $rhs = $lhs->new($rhs);
  544.     }
  545.     if ($reverse) {
  546.         return $rhs->epoch, $lhs->epoch;
  547.     }
  548.     return $lhs->epoch, $rhs->epoch;
  549. }
  550.  
  551. sub compare {
  552.     my ($lhs, $rhs) = get_epochs(@_);
  553.     return $lhs <=> $rhs;
  554. }
  555.  
  556. 1;
  557. __END__
  558.  
  559. =head1 NAME
  560.  
  561. Time::Piece - Object Oriented time objects
  562.  
  563. =head1 SYNOPSIS
  564.  
  565.     use Time::Piece;
  566.     
  567.     my $t = localtime;
  568.     print "Time is $t\n";
  569.     print "Year is ", $t->year, "\n";
  570.  
  571. =head1 DESCRIPTION
  572.  
  573. This module replaces the standard localtime and gmtime functions with
  574. implementations that return objects. It does so in a backwards
  575. compatible manner, so that using localtime/gmtime in the way documented
  576. in perlfunc will still return what you expect.
  577.  
  578. The module actually implements most of an interface described by
  579. Larry Wall on the perl5-porters mailing list here:
  580. http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2000-01/msg00241.html
  581.  
  582. =head1 USAGE
  583.  
  584. After importing this module, when you use localtime or gmtime in a scalar
  585. context, rather than getting an ordinary scalar string representing the
  586. date and time, you get a Time::Piece object, whose stringification happens
  587. to produce the same effect as the localtime and gmtime functions. There is 
  588. also a new() constructor provided, which is the same as localtime(), except
  589. when passed a Time::Piece object, in which case it's a copy constructor. The
  590. following methods are available on the object:
  591.  
  592.     $t->sec                 # also available as $t->second
  593.     $t->min                 # also available as $t->minute
  594.     $t->hour                # 24 hour
  595.     $t->mday                # also available as $t->day_of_month
  596.     $t->mon                 # 1 = January
  597.     $t->_mon                # 0 = January
  598.     $t->monname             # Feb
  599.     $t->month               # same as $t->monname
  600.     $t->fullmonth           # February
  601.     $t->year                # based at 0 (year 0 AD is, of course 1 BC)
  602.     $t->_year               # year minus 1900
  603.     $t->yy                  # 2 digit year
  604.     $t->wday                # 1 = Sunday
  605.     $t->_wday               # 0 = Sunday
  606.     $t->day_of_week         # 0 = Sunday
  607.     $t->wdayname            # Tue
  608.     $t->day                 # same as wdayname
  609.     $t->fullday             # Tuesday
  610.     $t->yday                # also available as $t->day_of_year, 0 = Jan 01
  611.     $t->isdst               # also available as $t->daylight_savings
  612.  
  613.     $t->hms                 # 12:34:56
  614.     $t->hms(".")            # 12.34.56
  615.     $t->time                # same as $t->hms
  616.  
  617.     $t->ymd                 # 2000-02-29
  618.     $t->date                # same as $t->ymd
  619.     $t->mdy                 # 02-29-2000
  620.     $t->mdy("/")            # 02/29/2000
  621.     $t->dmy                 # 29-02-2000
  622.     $t->dmy(".")            # 29.02.2000
  623.     $t->datetime            # 2000-02-29T12:34:56 (ISO 8601)
  624.     $t->cdate               # Tue Feb 29 12:34:56 2000
  625.     "$t"                    # same as $t->cdate
  626.  
  627.     $t->epoch               # seconds since the epoch
  628.     $t->tzoffset            # timezone offset in a Time::Seconds object
  629.  
  630.     $t->julian_day          # number of days since Julian period began
  631.     $t->mjd                 # modified Julian date (JD-2400000.5 days)
  632.  
  633.     $t->week                # week number (ISO 8601)
  634.  
  635.     $t->is_leap_year        # true if it its
  636.     $t->month_last_day      # 28-31
  637.  
  638.     $t->time_separator($s)  # set the default separator (default ":")
  639.     $t->date_separator($s)  # set the default separator (default "-")
  640.     $t->day_list(@days)     # set the default weekdays
  641.     $t->mon_list(@days)     # set the default months
  642.  
  643.     $t->strftime(FORMAT)    # same as POSIX::strftime (without the overhead
  644.                             # of the full POSIX extension)
  645.     $t->strftime()          # "Tue, 29 Feb 2000 12:34:56 GMT"
  646.     
  647.     Time::Piece->strptime(STRING, FORMAT)
  648.                             # see strptime man page. Creates a new
  649.                             # Time::Piece object
  650.  
  651. =head2 Local Locales
  652.  
  653. Both wdayname (day) and monname (month) allow passing in a list to use
  654. to index the name of the days against. This can be useful if you need
  655. to implement some form of localisation without actually installing or
  656. using locales.
  657.  
  658.   my @days = qw( Dimanche Lundi Merdi Mercredi Jeudi Vendredi Samedi );
  659.  
  660.   my $french_day = localtime->day(@days);
  661.  
  662. These settings can be overriden globally too:
  663.  
  664.   Time::Piece::day_list(@days);
  665.  
  666. Or for months:
  667.  
  668.   Time::Piece::mon_list(@months);
  669.  
  670. And locally for months:
  671.  
  672.   print localtime->month(@months);
  673.  
  674. =head2 Date Calculations
  675.  
  676. It's possible to use simple addition and subtraction of objects:
  677.  
  678.     use Time::Seconds;
  679.     
  680.     my $seconds = $t1 - $t2;
  681.     $t1 += ONE_DAY; # add 1 day (constant from Time::Seconds)
  682.  
  683. The following are valid ($t1 and $t2 are Time::Piece objects):
  684.  
  685.     $t1 - $t2; # returns Time::Seconds object
  686.     $t1 - 42; # returns Time::Piece object
  687.     $t1 + 533; # returns Time::Piece object
  688.  
  689. However adding a Time::Piece object to another Time::Piece object
  690. will cause a runtime error.
  691.  
  692. Note that the first of the above returns a Time::Seconds object, so
  693. while examining the object will print the number of seconds (because
  694. of the overloading), you can also get the number of minutes, hours,
  695. days, weeks and years in that delta, using the Time::Seconds API.
  696.  
  697. =head2 Date Comparisons
  698.  
  699. Date comparisons are also possible, using the full suite of "<", ">",
  700. "<=", ">=", "<=>", "==" and "!=".
  701.  
  702. =head2 Date Parsing
  703.  
  704. Time::Piece links to your C library's strptime() function, allowing
  705. you incredibly flexible date parsing routines. For example:
  706.  
  707.   my $t = Time::Piece->strptime("Sun 3rd Nov, 1943",
  708.                                 "%A %drd %b, %Y");
  709.   
  710.   print $t->strftime("%a, %d %b %Y");
  711.  
  712. Outputs:
  713.  
  714.   Wed, 03 Nov 1943
  715.  
  716. (see, it's even smart enough to fix my obvious date bug)
  717.  
  718. For more information see "man strptime", which should be on all unix
  719. systems.
  720.  
  721. =head2 YYYY-MM-DDThh:mm:ss
  722.  
  723. The ISO 8601 standard defines the date format to be YYYY-MM-DD, and
  724. the time format to be hh:mm:ss (24 hour clock), and if combined, they
  725. should be concatenated with date first and with a capital 'T' in front
  726. of the time.
  727.  
  728. =head2 Week Number
  729.  
  730. The I<week number> may be an unknown concept to some readers.  The ISO
  731. 8601 standard defines that weeks begin on a Monday and week 1 of the
  732. year is the week that includes both January 4th and the first Thursday
  733. of the year.  In other words, if the first Monday of January is the
  734. 2nd, 3rd, or 4th, the preceding days of the January are part of the
  735. last week of the preceding year.  Week numbers range from 1 to 53.
  736.  
  737. =head2 Global Overriding
  738.  
  739. Finally, it's possible to override localtime and gmtime everywhere, by
  740. including the ':override' tag in the import list:
  741.  
  742.     use Time::Piece ':override';
  743.  
  744. =head1 AUTHOR
  745.  
  746. Matt Sergeant, matt@sergeant.org
  747. Jarkko Hietaniemi, jhi@iki.fi (while creating Time::Piece for core perl)
  748.  
  749. =head1 License
  750.  
  751. This module is free software, you may distribute it under the same terms
  752. as Perl.
  753.  
  754. =head1 SEE ALSO
  755.  
  756. The excellent Calendar FAQ at http://www.tondering.dk/claus/calendar.html
  757.  
  758. =head1 BUGS
  759.  
  760. The test harness leaves much to be desired. Patches welcome.
  761.  
  762. =cut
  763.