Permalink
Browse files

import DateTime 0.05 from CPAN

git-cpan-module:   DateTime
git-cpan-version:  0.05
git-cpan-authorid: DROLSKY
git-cpan-file:     authors/id/D/DR/DROLSKY/DateTime-0.05.tar.gz
  • Loading branch information...
1 parent bca5326 commit 34494b6b14a2859c977e1614c46a18d10eaa49cd @autarch autarch committed with Feb 14, 2003
Showing with 230 additions and 181 deletions.
  1. +20 −0 Changes
  2. +1 −1 Makefile.PL
  3. +3 −0 TODO
  4. +49 −39 lib/DateTime.pm
  5. +3 −36 t/01sanity.t
  6. +2 −0 t/02last_day.t
  7. +32 −15 t/03components.t
  8. +57 −31 t/04epoch.t
  9. +2 −2 t/05set.t
  10. +33 −33 t/06add.t
  11. +14 −14 t/07compare.t
  12. +5 −5 t/10subtract.t
  13. +1 −1 t/11duration.t
  14. +1 −0 t/12week.t
  15. +3 −2 t/13strftime.t
  16. +4 −2 t/14language.t
View
20 Changes
@@ -1,3 +1,23 @@
+0.05 2003-02-13
+
+- Fix handling and reporting of epoch times. Epoch times are, by
+definition, UTC times, so any time created from an epoch should always
+have its time zone set to "UTC". This can be changed after the object
+is created. Similarly, value returned by the epoch() method needs to
+be based on the object's UTC time, not it's local time. Bug reported
+by Kellan.
+
+- Change year_0 so that -1 BCE is 0, not 1 CE. This corresponds to
+astronomical years.
+
+- Change ymd, dmy, mdy, and iso8601 to use Gregorian years (..., -2,
+-1, 1, 2, ... ) as opposed to astronomical years. Also make sure all
+negative years are formatted as 4 digits.
+
+0.04 2003-02-10
+
+- Explicitly set time zone for objects created during tests.
+
0.03 2003-02-09
- Giving a language parameter to a constructor method didn't load the
View
@@ -9,7 +9,7 @@ WriteMakefile( NAME => 'DateTime',
PREREQ_PM => { 'Class::Factory::Util' => 1.2,
'Date::Leapyear' => 0,
- 'DateTime::TimeZone' => 0.03,
+ 'DateTime::TimeZone' => 0.05,
'Params::Validate' => 0,
'Test::More' => 0,
'Time::Local' => 0,
View
3 TODO
@@ -33,3 +33,6 @@ version that works best is in 5.8.0, and not otherwise available.
- by default, reject out of bounds datetime values (minute 75) but
offer a "normalize" parameter that will wrap these.
+
+- make languages available via ISO codes as well as English name.
+Eric Cholet.
View
@@ -4,7 +4,7 @@ use strict;
use vars qw($VERSION);
-$VERSION = '0.03';
+$VERSION = '0.05';
use Date::Leapyear ();
use DateTime::Duration;
@@ -81,8 +81,6 @@ sub new {
$self->{language} = $lang_class->new;
}
- $args{time_zone} = 'local' unless exists $args{time_zone};
-
$self->{tz} =
( ref $args{time_zone} ?
$args{time_zone} :
@@ -170,7 +168,6 @@ sub from_epoch {
my %args = validate( @_,
{ epoch => { type => SCALAR },
language => { type => SCALAR | OBJECT, optional => 1 },
- time_zone => { type => SCALAR | OBJECT, optional => 1 },
}
);
@@ -183,7 +180,7 @@ sub from_epoch {
$p{month}++;
# pass other args like time_zone to constructor
- return $class->new( %args, %p );
+ return $class->new( %args, %p, time_zone => 'UTC' );
}
# use scalar time in case someone's loaded Time::Piece
@@ -384,7 +381,7 @@ sub _beginning_of_month_day_of_year {
}
sub year { $_[0]->{c}{year} <= 0 ? $_[0]->{c}{year} - 1 : $_[0]->{c}{year} }
-sub year_0 { $_[0]->{c}{year} - 1 }
+sub year_0 { $_[0]->{c}{year} }
sub month { $_[0]->{c}{month} }
*mon = \&month;
@@ -437,8 +434,8 @@ sub day_of_year_0 { $_[0]->{c}{day_of_year} - 1 }
sub ymd {
my ( $self, $sep ) = @_;
$sep = '-' unless defined $sep;
- return sprintf( "%04d%s%02d%s%02d",
- $self->{c}{year}, $sep,
+ return sprintf( "%0.4d%s%0.2d%s%0.2d",
+ $self->year, $sep,
$self->{c}{month}, $sep,
$self->{c}{day} );
}
@@ -447,19 +444,19 @@ sub ymd {
sub mdy {
my ( $self, $sep ) = @_;
$sep = '-' unless defined $sep;
- return sprintf( "%02d%s%02d%s%04d",
+ return sprintf( "%0.2d%s%0.2d%s%0.4d",
$self->{c}{month}, $sep,
$self->{c}{day}, $sep,
- $self->{c}{year} );
+ $self->year );
}
sub dmy {
my ( $self, $sep ) = @_;
$sep = '-' unless defined $sep;
- return sprintf( "%02d%s%02d%s%04d",
+ return sprintf( "%0.2d%s%0.2d%s%0.4d",
$self->{c}{day}, $sep,
$self->{c}{month}, $sep,
- $self->{c}{year} );
+ $self->year );
}
sub hour { $_[0]->{c}{hour} }
@@ -473,7 +470,7 @@ sub second { $_[0]->{c}{second} }
sub hms {
my ( $self, $sep ) = @_;
$sep = ':' unless defined $sep;
- return sprintf( "%02d%s%02d%s%02d",
+ return sprintf( "%0.2d%s%0.2d%s%0.2d",
$self->{c}{hour}, $sep,
$self->{c}{minute}, $sep,
$self->{c}{second} );
@@ -483,7 +480,12 @@ sub hms {
sub iso8601 {
my $self = shift;
- return join 'T', $self->ymd('-'), $self->hms(':');
+
+ # ISO 8601 uses astronomical years
+ my $ymd = sprintf( '%0.4d-%0.2d-%0.2d',
+ @{ $self->{c} }{ 'year', 'month', 'day' } );
+
+ return join 'T', $ymd, $self->hms(':');
}
*datetime = \&iso8601;
@@ -608,16 +610,19 @@ sub strftime {
sub epoch {
my $self = shift;
- # timegm may die if given components outside of the ranges it
- # can handle. In that case return undef.
- return
- eval { Time::Local::timegm( $self->second,
- $self->minute,
- $self->hour,
- $self->day,
- $self->month_0,
- $self->year - 1900,
+ return $self->{c}{epoch} if exists $self->{c}{epoch};
+
+ my ( $year, $month, $day ) = $self->_rd2greg( $self->{utc_rd_days} );
+ my @hms = $self->_seconds_as_components( $self->{utc_rd_secs} );
+
+ $self->{c}{epoch} =
+ eval { Time::Local::timegm( ( reverse @hms ),
+ $day,
+ $month - 1,
+ $year - 1900,
) };
+
+ return $self->{c}{epoch};
}
sub add { shift->add_duration( DateTime::Duration->new(@_) ) }
@@ -881,7 +886,9 @@ DateTime - Reference implementation for Perl DateTime objects
$day_name = $dt->day_name # Monday, Tuesday, ...
$day_abbr = $dt->day_abbr # Mon, Tue, ...
- $epoch_time = $dt->epoch; # may return undef for non-epoch times
+ $epoch_time = $dt->epoch;
+ # may return undef if the datetime is outside the range that is
+ # representable by your OS's epoch system.
$dt2 = $dt + $duration_object;
@@ -982,15 +989,16 @@ The time_zone parameter can be either a scalar or a
C<DateTime::TimeZone> object. A string will simply be passed to the
C<< DateTime::TimeZone->new >> method as its "name" parameter. This
string may be an Olson DB time zone name ("America/Chicago"), an
-offset string ("+0630"), an offset in seconds (-21600), or the words
-"floating" or "local". See the C<DateTime::TimeZone> documentation
-for more details.
+offset string ("+0630"), or the words "floating" or "local". See the
+C<DateTime::TimeZone> documentation for more details.
=item * from_epoch( epoch => $epoch, ... )
This class method can be used to construct a new DateTime object from
an epoch time instead of components. Just as with the C<new()>
-method, it accepts "language" and "time_zone" parameters.
+method, it accepts a "language" parameter. The time zone will always
+be "UTC" for any object created from an epoch. This can be changed
+once the object is created.
=item * now( ... )
@@ -1029,7 +1037,8 @@ month/week/year, are 1-based. Any method that is one based also has
an equivalent 0-based method ending in "_0". So for example, this
class provides both C<day_of_week()> and C<day_of_week_0()> methods.
-The C<year_0> method treats the year 1 CE as year 0.
+The C<year_0> method treats the year -1 BCE as year 0, as is
+conventional in astronomy.
The C<day_of_week_0> method still treats Monday as the first day of
the week.
@@ -1084,8 +1093,7 @@ L<LANGUAGES|/LANGUAGES> section for more details.
=item * day_of_year, doy
-Returns the day of the year. Analogous to the yday attribute of
-gmtime (or localtime) except that it works outside of the epoch.
+Returns the day of the year.
=item * ymd( $optional_separator ), date
@@ -1124,6 +1132,9 @@ This method is equivalent to:
$dt->ymd('-') . 'T' . $dt->hms(':')
+I<except> that the year is the year as returned by the C<year_0()>
+method.
+
=item * is_leap_year
This method returns a true or false indicating whether or not the
@@ -1209,8 +1220,8 @@ start of the epoch will be returned as a negative number.
Since epoch times cannot represent many dates on most platforms, this
method may simply return undef in some cases.
-Using epochs is not recommended, since they have such a limited range,
-at least on 32-bit machines.
+Using your system's epoch time is not recommended, since they have
+such a limited range, at least on 32-bit machines.
=back
@@ -1234,9 +1245,9 @@ then the I<local> time is adjusted accordingly.
For example:
- my $dt = DateTime::TimeZone->new( year => 2000, month => 5, day => 10,
- hour => 15, minute => 15,
- time_zone => '-0600', );
+ my $dt = DateTime->new( year => 2000, month => 5, day => 10,
+ hour => 15, minute => 15,
+ time_zone => '-0600', );
print $dt->hour; # prints 15
@@ -1456,8 +1467,7 @@ seconds, see %T below.
=item * %s
-The number of seconds since the Epoch, i.e., since 1970-01-01 00:00:00
-UTC.
+The number of seconds since the epoch.
=item * %S
@@ -1509,7 +1519,7 @@ The year as a decimal number including the century.
=item * %z
-The time-zone as hour offset from GMT. Required to emit
+The time-zone as hour offset from UTC. Required to emit
RFC822-conformant dates (using "%a, %d %b %Y %H:%M:%S %z").
=item * %Z
View
@@ -1,53 +1,20 @@
use strict;
-use Test::More tests => 25;
+use Test::More tests => 12;
use DateTime;
-#======================================================================
-# BASIC INITIALIZATION TESTS
-#======================================================================
-
-my $t1 = DateTime->from_epoch( epoch => 0 );
-is( $t1->epoch, 0, "Epoch time of 0" );
-
-# Make sure epoch time is being handled sanely.
-# FIXME: This will only work on unix systems.
-is( $t1->ymd('-'), '1970-01-01', "When does the epoch start?" );
-
-is( $t1->year, 1970, "Year accessor, start of epoch" );
-is( $t1->month, 1, "Month accessor, start of epoch" );
-is( $t1->day, 1, "Day accessor, start of epoch" );
-
-# Dates in December are giving a month of 0. Test for this
-my $dec = new DateTime( year => 1996, month => 12, day => 22, time_zone => 0 );
-is( $dec->month, 12, 'Date should be in December' );
-$dec->add( weeks => 4 );
-is( $dec->month, 1, '4 weeks later, it is January' );
-
-my $t3 = new DateTime( year => 2001, month => 2, day => 3,
- hour => 18, minute => 30, second => 20,
- time_zone => 0 );
-
-is( $t3->year, 2001, "Year accessor" );
-is( $t3->month, 2, "Month accessor" );
-is( $t3->day, 3, "Day accessor" );
-is( $t3->hour, 18, "Hour accessor" );
-is( $t3->minute, 30, "Minute accessor" );
-is( $t3->second, 20, "Second accessor" );
-# XXX Round-off error could make this 19 ?????
-
my $t4 = new DateTime( year => 1870, month => 10, day => 21,
hour => 12, minute => 10, second => 45,
- time_zone => 0 );
+ time_zone => 'UTC' );
is( $t4->year, '1870', "Year accessor, outside of the epoch" );
is( $t4->month, '10', "Month accessor, outside the epoch" );
is( $t4->day, '21', "Day accessor, outside the epoch" );
is( $t4->hour, '12', "Hour accessor, outside the epoch" );
is( $t4->minute, '10', "Minute accessor, outside the epoch" );
is( $t4->second, '45', "Second accessor, outside the epoch" );
-my $t5 = DateTime->from_object( object => $t4 );
+my $t5 = DateTime->from_object( object => $t4, time_zone => 'UTC' );
is( $t5->year, '1870', "Year should be identical" );
is( $t5->month, '10', "Month should be identical" );
is( $t5->day, '21', "Day should be identical" );
View
@@ -12,6 +12,7 @@ foreach my $month (1..12)
{
my $dt = DateTime->last_day_of_month( year => 2001,
month => $month,
+ time_zone => 'UTC',
);
is( $dt->year, 2001, 'check year' );
@@ -23,6 +24,7 @@ foreach my $month (1..12)
{
my $dt = DateTime->last_day_of_month( year => 2004,
month => $month,
+ time_zone => 'UTC',
);
is( $dt->year, 2004, 'check year' );
Oops, something went wrong.

0 comments on commit 34494b6

Please sign in to comment.