Skip to content

Commit

Permalink
fix timezone problem in strftime test (GitHub #6)
Browse files Browse the repository at this point in the history
primary fix was to make the datetime UTC
however, this exposed an oddity (maybe a bug?) in Time::Piece:
-	see Dual-Life/Time-Piece#24 for a full explanation
so now we have our own `strftime` implementation which is not just a pass-through
-	now, for us, `->strftime("%s")` is always equivalent to `->epoch`
(note that my use of look-behind means you have to have at least Perl 5.005)
(but that doesn't seem like too much of a hardship)
  • Loading branch information
barefootcoder committed May 26, 2016
1 parent 0a04fa0 commit 57c17eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Revision history for Date-Easy

0.01_02 2016-05-22 01:48:55PDT-0700 America/Los_Angeles (TRIAL RELEASE)

[Bug Fixes]
* The "%s" format specifier now always returns the same value as the
epoch method, regardless of what the underlying implementation thinks

[Interface Changes]
* ::Date and ::Datetime now have accessors that follow the UI of
DateTime rather than Time::Piece
Expand All @@ -12,6 +16,8 @@ Revision history for Date-Easy
[Distro Fixes]
* Fixed multiple cases of datetimes not matching due to clock rolling over
github #4 (thx Slaven Rezić (SREZIC))
* Fixed `strftime` unit tests being locked to the author's timezone
github #6 (thx Slaven Rezić (SREZIC))
* Fixed out-of-range tests on 32-bit machines
github #3 (thx Slaven Rezić (SREZIC))
* Fixed bug where List::Util might be too low a version during testing
Expand Down
11 changes: 10 additions & 1 deletion lib/Date/Easy/Datetime.pm
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,16 @@ sub epoch { shift->{impl}->epoch }
sub day_of_week { shift->{impl}->day_of_week || 7 } # change Sunday from 0 to 7
sub quarter { int(shift->{impl}->_mon / 3) + 1 } # calc quarter from (zero-based) month

sub strftime { shift->{impl}->strftime(@_) }
sub strftime
{
my ($self, $format) = @_;
return $self->{impl}->strftime unless defined $format;

# Handle the %s format specifier ourselves because otherwise our users may get a nasty shock.
# See https://github.com/rjbs/Time-Piece/issues/24 for full details.
$format =~ s/(?<!%)%s/$self->epoch/eg;
return $self->{impl}->strftime($format);
}


########################
Expand Down
12 changes: 9 additions & 3 deletions t/format.t
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use Date::Easy;


# test date: 3 Feb 2001, 04:05:06
my $dt = Date::Easy::Datetime->new(2001, 2, 3, 4, 5, 6);
my $dt = Date::Easy::Datetime->new(UTC => 2001, 2, 3, 4, 5, 6);
# epoch works out to:
my $epoch = 981201906;
my $epoch = 981173106;
# dow is Sat


Expand All @@ -18,13 +18,19 @@ my %FORMATS =
"%Y/%m/%d %H:%M:%S" => "2001/02/03 04:05:06",
"%l:%M:%S%p" => " 4:05:06AM",
"%u" => "6",
"%s" => $epoch,
"%s %%s %s" => "$epoch %s $epoch",
);

foreach (keys %FORMATS)
{
is $dt->strftime($_), $FORMATS{$_}, "strftime format: $_";
}

# make sure I didn't bork the empty format call
# note also that I avoid the locale-specific parts by using a regex with \w+
my $str;
warning_is { $str = $dt->strftime } undef, "no uninitialized warning on empty format";
like $str, qr/^\w+, 03 \w+ 2001 04:05:06 UTC$/, "empty format produces default format";


done_testing;

0 comments on commit 57c17eb

Please sign in to comment.