This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix ext/POSIX/t/time.t test 17's name to be a) unique, and b) factually accurate
[perl5.git] / ext / POSIX / t / time.t
CommitLineData
2da668d2
SP
1#!perl -w
2
3use strict;
4
5use Config;
6use POSIX;
351aaf90 7use Test::More tests => 28;
2da668d2 8
31b629d6
SP
9# go to UTC to avoid DST issues around the world when testing. SUS3 says that
10# null should get you UTC, but some environments want the explicit names.
11# Those with a working tzset() should be able to use the TZ below.
12$ENV{TZ} = "UTC0UTC";
2da668d2
SP
13
14SKIP: {
15 # It looks like POSIX.xs claims that only VMS and Mac OS traditional
31b629d6
SP
16 # don't have tzset(). Win32 works to call the function, but it doesn't
17 # actually do anything. Cygwin works in some places, but not others. The
18 # other Win32's below are guesses.
2da668d2 19 skip "No tzset()", 2
31b629d6
SP
20 if $^O eq "MacOS" || $^O eq "VMS" || $^O eq "cygwin" || $^O eq "djgpp" ||
21 $^O eq "MSWin32" || $^O eq "dos" || $^O eq "interix";
2da668d2
SP
22 tzset();
23 my @tzname = tzname();
0bcc7986 24 like($tzname[0], qr/(GMT|UTC)/i, "tzset() to GMT/UTC");
6f3614c8
SP
25 SKIP: {
26 skip "Mac OS X/Darwin doesn't handle this", 1 if $^O =~ /darwin/i;
0bcc7986 27 like($tzname[1], qr/(GMT|UTC)/i, "The whole year?");
6f3614c8 28 }
2da668d2
SP
29}
30
4c34a366
MB
31if ($^O eq "hpux" && $Config{osvers} >= 11.3) {
32 # HP does not support UTC0UTC and/or GMT0GMT, as they state that this is
33 # legal syntax but as it has no DST rule, it cannot be used. That is the
34 # conclusion of bug
35 # QXCR1000896916: Some timezone valuesfailing on 11.31 that work on 11.23
7faed49a 36 $ENV{TZ} = "UTC";
4c34a366
MB
37}
38
2da668d2
SP
39# asctime and ctime...Let's stay below INT_MAX for 32-bits and
40# positive for some picky systems.
41
b7fda7aa
NC
42is(asctime(CORE::localtime(0)), ctime(0), "asctime() and ctime() at zero");
43is(asctime(POSIX::localtime(0)), ctime(0), "asctime() and ctime() at zero");
44is(asctime(CORE::localtime(12345678)), ctime(12345678),
45 "asctime() and ctime() at 12345678");
46is(asctime(POSIX::localtime(12345678)), ctime(12345678),
47 "asctime() and ctime() at 12345678");
2da668d2 48
b7b1e41b 49# Careful! strftime() is locale sensitive. Let's take care of that
34dd738f 50my $orig_loc = setlocale(LC_TIME, "C") || die "Cannot setlocale() to C: $!";
018eeb12 51my $jan_16 = 15 * 86400;
b7fda7aa 52is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", CORE::localtime($jan_16)),
34dd738f 53 "get ctime() equal to strftime()");
b7fda7aa
NC
54is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", POSIX::localtime($jan_16)),
55 "get ctime() equal to strftime()");
56is(strftime("%Y\x{5e74}%m\x{6708}%d\x{65e5}", CORE::gmtime($jan_16)),
57 "1970\x{5e74}01\x{6708}16\x{65e5}",
58 "strftime() can handle unicode chars in the format string");
59is(strftime("%Y\x{5e74}%m\x{6708}%d\x{65e5}", POSIX::gmtime($jan_16)),
dc57de01
NC
60 "1970\x{5e74}01\x{6708}16\x{65e5}",
61 "strftime() can handle unicode chars in the format string");
62
63my $ss = chr 223;
64unlike($ss, qr/\w/, 'Not internally UTF-8 encoded');
b7fda7aa
NC
65is(ord strftime($ss, CORE::localtime), 223,
66 'Format string has correct character');
67is(ord strftime($ss, POSIX::localtime(time)),
68 223, 'Format string has correct character');
dc57de01
NC
69unlike($ss, qr/\w/, 'Still not internally UTF-8 encoded');
70
351aaf90
PE
71my @time = POSIX::strptime("2011-12-18 12:34:56", "%Y-%m-%d %H:%M:%S");
72is_deeply(\@time, [56, 34, 12, 18, 12-1, 2011-1900, 0, 351, 0], 'strptime() all 6 fields');
add6f7f9 73
351aaf90
PE
74@time = POSIX::strptime("2011-12-18", "%Y-%m-%d", 1, 23, 4);
75is_deeply(\@time, [1, 23, 4, 18, 12-1, 2011-1900, 0, 351, 0], 'strptime() all date fields with passed time');
add6f7f9 76
d3fdbcf9
PE
77# tm_year == 6 => 1906, which is a negative time_t. Lets use 106 as 2006 instead
78@time = POSIX::strptime("12:34:56", "%H:%M:%S", 1, 2, 3, 4, 5, 106);
c3991524 79is_deeply(\@time, [56, 34, 12, 4, 5, 106, 0, 154, 1], 'strptime() all time fields with passed date');
351aaf90
PE
80
81@time = POSIX::strptime("July 4", "%b %d");
82is_deeply([@time[3,4]], [4, 7-1], 'strptime() partial yields correct mday/mon');
add6f7f9
PE
83
84@time = POSIX::strptime("Foobar", "%H:%M:%S");
85is(scalar @time, 0, 'strptime() invalid input yields empty list');
86
73bb741d
PE
87my $str;
88@time = POSIX::strptime(\($str = "01:02:03"), "%H:%M:%S", -1,-1,-1, 1,0,70);
89is_deeply(\@time, [3, 2, 1, 1, 0, 70, 4, 0, 0], 'strptime() parses SCALAR ref');
90is(pos($str), 8, 'strptime() sets pos() magic on SCALAR ref');
91
92$str = "Text with 2012-12-01 datestamp";
93pos($str) = 10;
94@time = POSIX::strptime(\$str, "%Y-%m-%d", 0, 0, 0);
95is_deeply(\@time, [0, 0, 0, 1, 12-1, 2012-1900, 6, 335, 0], 'strptime() starts SCALAR ref at pos()');
96is(pos($str), 20, 'strptime() updates pos() magic on SCALAR ref');
97
34dd738f 98setlocale(LC_TIME, $orig_loc) || die "Cannot setlocale() back to orig: $!";
2da668d2 99
439787ad
SP
100# clock() seems to have different definitions of what it does between POSIX
101# and BSD. Cygwin, Win32, and Linux lean the BSD way. So, the tests just
102# check the basics.
2da668d2 103like(clock(), qr/\d*/, "clock() returns a numeric value");
a572b909 104cmp_ok(clock(), '>=', 0, "...and it returns something >= 0");
2da668d2
SP
105
106SKIP: {
107 skip "No difftime()", 1 if $Config{d_difftime} ne 'define';
108 is(difftime(2, 1), 1, "difftime()");
109}
110
111SKIP: {
b7fda7aa 112 skip "No mktime()", 2 if $Config{d_mktime} ne 'define';
2da668d2 113 my $time = time();
b7fda7aa
NC
114 is(mktime(CORE::localtime($time)), $time, "mktime()");
115 is(mktime(POSIX::localtime($time)), $time, "mktime()");
2da668d2 116}