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