diff options
author | Paul \"LeoNerd\" Evans <leonerd@leonerd.org.uk> | 2011-12-18 23:25:03 +0000 |
---|---|---|
committer | Ævar Arnfjörð Bjarmason <avar@cpan.org> | 2011-12-23 18:38:54 +0000 |
commit | 8d6290715988de7dff3d363af26aaed1265857e9 (patch) | |
tree | ef3cd7d0382daa3927d28eb074591b0a52dc42bd | |
parent | 5f8b560c2ebd66afd8ccc9364f425281a1b966a2 (diff) | |
download | perl-8d6290715988de7dff3d363af26aaed1265857e9.tar.gz |
Initial hack at strptime(); just literal strings for now
-rw-r--r-- | ext/POSIX/POSIX.xs | 43 | ||||
-rw-r--r-- | ext/POSIX/lib/POSIX.pm | 2 | ||||
-rw-r--r-- | ext/POSIX/t/time.t | 14 |
3 files changed, 57 insertions, 2 deletions
diff --git a/ext/POSIX/POSIX.xs b/ext/POSIX/POSIX.xs index 27aabb5d83..18296b5c81 100644 --- a/ext/POSIX/POSIX.xs +++ b/ext/POSIX/POSIX.xs @@ -1829,6 +1829,49 @@ strftime(fmt, sec, min, hour, mday, mon, year, wday = -1, yday = -1, isdst = -1) } void +strptime(str, fmt, sec=-1, min=-1, hour=-1, mday=-1, mon=-1, year=-1, wday=-1, yday=-1, isdst=-1) + SV * str + SV * fmt + int sec + int min + int hour + int mday + int mon + int year + int wday + int yday + int isdst + PPCODE: + { + struct tm tm; + tm.tm_sec = sec; + tm.tm_min = min; + tm.tm_hour = hour; + tm.tm_mday = mday; + tm.tm_mon = mon; + tm.tm_year = year; + tm.tm_wday = wday; + tm.tm_yday = yday; + tm.tm_isdst = isdst; + + char *remains = strptime(SvPV_nolen(str), SvPV_nolen(fmt), &tm); + if (!remains || remains[0]) + /* failed parse */ + XSRETURN(0); + + EXTEND(SP, 9); + PUSHs(sv_2mortal(newSViv(tm.tm_sec))); + PUSHs(sv_2mortal(newSViv(tm.tm_min))); + PUSHs(sv_2mortal(newSViv(tm.tm_hour))); + PUSHs(sv_2mortal(newSViv(tm.tm_mday))); + PUSHs(sv_2mortal(newSViv(tm.tm_mon))); + PUSHs(sv_2mortal(newSViv(tm.tm_year))); + PUSHs(sv_2mortal(newSViv(tm.tm_wday))); + PUSHs(sv_2mortal(newSViv(tm.tm_yday))); + PUSHs(sv_2mortal(newSViv(tm.tm_isdst))); + } + +void tzset() PPCODE: my_tzset(aTHX); diff --git a/ext/POSIX/lib/POSIX.pm b/ext/POSIX/lib/POSIX.pm index e50ee235e6..a701f31bd3 100644 --- a/ext/POSIX/lib/POSIX.pm +++ b/ext/POSIX/lib/POSIX.pm @@ -361,7 +361,7 @@ our %EXPORT_TAGS = ( tcflow tcflush tcgetattr tcsendbreak tcsetattr )], time_h => [qw(CLK_TCK CLOCKS_PER_SEC NULL asctime clock ctime - difftime mktime strftime tzset tzname)], + difftime mktime strftime strptime tzset tzname)], unistd_h => [qw(F_OK NULL R_OK SEEK_CUR SEEK_END SEEK_SET STDERR_FILENO STDIN_FILENO STDOUT_FILENO W_OK X_OK diff --git a/ext/POSIX/t/time.t b/ext/POSIX/t/time.t index 90b54caa47..e27e9f0b58 100644 --- a/ext/POSIX/t/time.t +++ b/ext/POSIX/t/time.t @@ -4,7 +4,7 @@ use strict; use Config; use POSIX; -use Test::More tests => 19; +use Test::More tests => 23; # go to UTC to avoid DST issues around the world when testing. SUS3 says that # null should get you UTC, but some environments want the explicit names. @@ -68,6 +68,18 @@ is(ord strftime($ss, POSIX::localtime(time)), 223, 'Format string has correct character'); unlike($ss, qr/\w/, 'Still not internally UTF-8 encoded'); +my @time = (POSIX::strptime("2011-12-18 12:34:56", "%Y-%m-%d %H:%M:%S"))[0..5]; +is_deeply(\@time, [56, 34, 12, 18, 12-1, 2011-1900], 'strptime() all 6 fields'); + +@time = (POSIX::strptime("2011-12-18", "%Y-%m-%d", 1, 23, 4))[0..5]; +is_deeply(\@time, [1, 23, 4, 18, 12-1, 2011-1900], 'strptime() all date fields with passed time'); + +@time = (POSIX::strptime("12:34:56", "%H:%M:%S", 1, 2, 3, 4, 5, 6))[0..5]; +is_deeply(\@time, [56, 34, 12, 4, 5, 6], 'strptime() all date fields with passed time'); + +@time = POSIX::strptime("Foobar", "%H:%M:%S"); +is(scalar @time, 0, 'strptime() invalid input yields empty list'); + setlocale(LC_TIME, $orig_loc) || die "Cannot setlocale() back to orig: $!"; # clock() seems to have different definitions of what it does between POSIX |