xref: /openbsd-src/gnu/usr.bin/perl/ext/POSIX/t/time.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!perl -w
2
3use strict;
4
5use Config;
6use POSIX;
7use Test::More tests => 19;
8
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";
13
14SKIP: {
15    # It looks like POSIX.xs claims that only VMS and Mac OS traditional
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.
19    skip "No tzset()", 2
20       if $^O eq "MacOS" || $^O eq "VMS" || $^O eq "cygwin" || $^O eq "djgpp" ||
21          $^O eq "MSWin32" || $^O eq "dos" || $^O eq "interix";
22    tzset();
23    my @tzname = tzname();
24    like($tzname[0], qr/(GMT|UTC)/i, "tzset() to GMT/UTC");
25    SKIP: {
26        skip "Mac OS X/Darwin doesn't handle this", 1 if $^O =~ /darwin/i;
27        like($tzname[1], qr/(GMT|UTC)/i, "The whole year?");
28    }
29}
30
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
36    $ENV{TZ} = "UTC";
37}
38
39# asctime and ctime...Let's stay below INT_MAX for 32-bits and
40# positive for some picky systems.
41
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");
48
49# Careful!  strftime() is locale sensitive.  Let's take care of that
50my $orig_loc = 'C';
51if ( $Config{d_setlocale} ) {
52    $orig_loc = setlocale(LC_TIME) || die "Cannot get locale information:  $!";
53    setlocale(LC_TIME, "C") || die "Cannot setlocale() to C:  $!";
54}
55my $jan_16 = 15 * 86400;
56is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", CORE::localtime($jan_16)),
57        "get ctime() equal to strftime()");
58is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", POSIX::localtime($jan_16)),
59        "get ctime() equal to strftime()");
60is(strftime("%Y\x{5e74}%m\x{6708}%d\x{65e5}", CORE::gmtime($jan_16)),
61   "1970\x{5e74}01\x{6708}16\x{65e5}",
62   "strftime() can handle unicode chars in the format string");
63is(strftime("%Y\x{5e74}%m\x{6708}%d\x{65e5}", POSIX::gmtime($jan_16)),
64   "1970\x{5e74}01\x{6708}16\x{65e5}",
65   "strftime() can handle unicode chars in the format string");
66
67my $ss = chr 223;
68unlike($ss, qr/\w/, 'Not internally UTF-8 encoded');
69is(ord strftime($ss, CORE::localtime), 223,
70   'Format string has correct character');
71is(ord strftime($ss, POSIX::localtime(time)),
72   223, 'Format string has correct character');
73unlike($ss, qr/\w/, 'Still not internally UTF-8 encoded');
74
75if ( $Config{d_setlocale} ) {
76    setlocale(LC_TIME, $orig_loc) || die "Cannot setlocale() back to orig: $!";
77}
78
79# clock() seems to have different definitions of what it does between POSIX
80# and BSD.  Cygwin, Win32, and Linux lean the BSD way.  So, the tests just
81# check the basics.
82like(clock(), qr/\d*/, "clock() returns a numeric value");
83cmp_ok(clock(), '>=', 0, "...and it returns something >= 0");
84
85SKIP: {
86    skip "No difftime()", 1 if $Config{d_difftime} ne 'define';
87    is(difftime(2, 1), 1, "difftime()");
88}
89
90SKIP: {
91    skip "No mktime()", 2 if $Config{d_mktime} ne 'define';
92    my $time = time();
93    is(mktime(CORE::localtime($time)), $time, "mktime()");
94    is(mktime(POSIX::localtime($time)), $time, "mktime()");
95}
96