| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 5; |
| 4 |
use Date::Parse qw(str2time); |
| 5 |
|
| 6 |
# RT#64789: str2time() should accept an optional epoch as third argument |
| 7 |
# to use as the reference time instead of time() when filling in missing |
| 8 |
# date components (month, day, year). |
| 9 |
{ |
| 10 |
# Use a known fixed epoch as the reference time: |
| 11 |
# 2010-06-15 12:00:00 UTC = 1276603200 |
| 12 |
my $ref_epoch = 1276603200; # 2010-06-15 12:00:00 UTC |
| 13 |
|
| 14 |
# Parsing "21 feb" with no year: February (month 1) < June (month 5), |
| 15 |
# so February is in the past — most recent occurrence is current year (2010). |
| 16 |
my $t = str2time("21 feb 17:05 UTC", undef, $ref_epoch); |
| 17 |
ok(defined $t, "RT#64789: str2time with epoch arg returns defined"); |
| 18 |
my $parsed_year = 1900 + (gmtime($t))[5]; |
| 19 |
is($parsed_year, 2010, "RT#64789: 'feb' with epoch 2010-06 stays in 2010 (most recent occurrence)"); |
| 20 |
|
| 21 |
# Parsing a time-only string: "10:30:00 UTC" |
| 22 |
# With ref_epoch (2010-06-15), the date portion should come from ref_epoch |
| 23 |
my $t2 = str2time("10:30:00 UTC", undef, $ref_epoch); |
| 24 |
ok(defined $t2, "RT#64789: time-only str2time with epoch arg returns defined"); |
| 25 |
my @gm2 = gmtime($t2); |
| 26 |
is($gm2[3], 15, "RT#64789: time-only: day from ref_epoch"); |
| 27 |
is(1900 + $gm2[5], 2010, "RT#64789: time-only: year from ref_epoch"); |
| 28 |
} |