| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
use Test::More tests => 27; |
| 4 |
use Date::Parse qw(strptime str2time); |
| 5 |
|
| 6 |
# Tests for strptime() with ISO 8601 dates (GitHub issue #44) |
| 7 |
# |
| 8 |
# strptime() returns: ($ss, $mm, $hh, $day, $month, $year, $zone, $century) |
| 9 |
# |
| 10 |
# The return format follows Perl's localtime/gmtime convention: |
| 11 |
# $month : 0-indexed (0=January, 1=February, ..., 11=December) |
| 12 |
# $year : years since 1900 (e.g. 2015 => 115, 1995 => 95) |
| 13 |
# $century: defined only when a 4-digit year was parsed; |
| 14 |
# the full year = $year + 1900 |
| 15 |
# $zone : timezone offset in seconds from UTC, or undef if not specified |
| 16 |
|
| 17 |
# --- Basic ISO 8601: YYYY-MM-DDTHH:MM:SS --- |
| 18 |
{ |
| 19 |
my @t = strptime("2015-01-01T08:51:00"); |
| 20 |
ok(defined $t[0], "ss defined"); |
| 21 |
cmp_ok($t[0]+0, '==', 0, "ss=0"); |
| 22 |
cmp_ok($t[1]+0, '==', 51, "mm=51"); |
| 23 |
cmp_ok($t[2]+0, '==', 8, "hh=8"); |
| 24 |
cmp_ok($t[3]+0, '==', 1, "day=1"); |
| 25 |
cmp_ok($t[4]+0, '==', 0, "month=0 (January is 0-indexed)"); |
| 26 |
cmp_ok($t[5]+0, '==', 115, "year=115 (2015-1900, following localtime convention)"); |
| 27 |
ok(!defined $t[6], "zone=undef (no timezone in input)"); |
| 28 |
cmp_ok($t[7]+0, '==', 20, "century=20"); |
| 29 |
} |
| 30 |
|
| 31 |
# --- ISO 8601 with fractional seconds --- |
| 32 |
{ |
| 33 |
my @t = strptime("1995-01-24T09:08:17.1823213"); |
| 34 |
cmp_ok($t[3]+0, '==', 24, "day=24"); |
| 35 |
cmp_ok($t[4]+0, '==', 0, "month=0 (January)"); |
| 36 |
cmp_ok($t[5]+0, '==', 95, "year=95 (1995-1900)"); |
| 37 |
cmp_ok($t[2]+0, '==', 9, "hh=9"); |
| 38 |
cmp_ok($t[1]+0, '==', 8, "mm=8"); |
| 39 |
cmp_ok(abs($t[0] - 17.1823213), '<', 0.000001, "ss=17.1823213 (fractional seconds preserved)"); |
| 40 |
cmp_ok($t[7]+0, '==', 19, "century=19"); |
| 41 |
} |
| 42 |
|
| 43 |
# --- ISO 8601 with Z (UTC) --- |
| 44 |
{ |
| 45 |
my @t = strptime("2015-03-15T12:00:00Z"); |
| 46 |
cmp_ok($t[4]+0, '==', 2, "month=2 (March is index 2)"); |
| 47 |
cmp_ok($t[3]+0, '==', 15, "day=15"); |
| 48 |
cmp_ok($t[6]+0, '==', 0, "zone=0 (UTC)"); |
| 49 |
} |
| 50 |
|
| 51 |
# --- The full year can be recovered as: $year + 1900 --- |
| 52 |
{ |
| 53 |
my @t = strptime("2015-01-01T08:51:00"); |
| 54 |
my $full_year = $t[5] + 1900; |
| 55 |
is($full_year, 2015, "full year = year + 1900 = 2015"); |
| 56 |
} |
| 57 |
|
| 58 |
# --- str2time() correctly converts ISO 8601 to Unix timestamp --- |
| 59 |
{ |
| 60 |
my $ts = str2time("2015-01-01T08:51:00Z"); |
| 61 |
ok(defined $ts, "str2time parses ISO 8601 with Z"); |
| 62 |
is($ts, 1420102260, "str2time returns correct Unix timestamp"); |
| 63 |
} |
| 64 |
|
| 65 |
# --- December: month index 11 --- |
| 66 |
{ |
| 67 |
my @t = strptime("2015-12-31T23:59:59"); |
| 68 |
cmp_ok($t[4]+0, '==', 11, "month=11 (December is 0-indexed 11)"); |
| 69 |
cmp_ok($t[3]+0, '==', 31, "day=31"); |
| 70 |
cmp_ok($t[2]+0, '==', 23, "hh=23"); |
| 71 |
} |
| 72 |
|
| 73 |
# --- Colon as date separator (non-standard, but accepted by the parser) --- |
| 74 |
{ |
| 75 |
my @t = strptime("1995:01:24T09:08:17"); |
| 76 |
cmp_ok($t[4]+0, '==', 0, "colon-separated: month=0 (January)"); |
| 77 |
cmp_ok($t[3]+0, '==', 24, "colon-separated: day=24"); |
| 78 |
} |