xref: /openbsd/gnu/usr.bin/perl/ext/POSIX/t/time.t (revision 097a140d)
1#!perl -w
2
3BEGIN {
4    unshift @INC, "../../t";
5    require 'loc_tools.pl';
6}
7
8use strict;
9
10use Config;
11use POSIX;
12use Test::More tests => 19;
13
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";
18
19SKIP: {
20    # It looks like POSIX.xs claims that only VMS and Mac OS traditional
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.
24    skip "No tzset()", 2
25       if $^O eq "VMS" || $^O eq "cygwin" || $^O eq "djgpp" ||
26          $^O eq "MSWin32" || $^O eq "dos" || $^O eq "interix";
27    tzset();
28    my @tzname = tzname();
29    like($tzname[0], qr/(GMT|UTC)/i, "tzset() to GMT/UTC");
30    SKIP: {
31        skip "Mac OS X/Darwin doesn't handle this", 1 if $^O =~ /darwin/i;
32        like($tzname[1], qr/(GMT|UTC)/i, "The whole year?");
33    }
34}
35
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
41    $ENV{TZ} = "UTC";
42}
43
44# asctime and ctime...Let's stay below INT_MAX for 32-bits and
45# positive for some picky systems.
46
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");
53
54# Careful!  strftime() is locale sensitive.  Let's take care of that
55my $orig_time_loc = 'C';
56my $orig_ctype_loc = 'C';
57if (locales_enabled('LC_TIME')) {
58    $orig_time_loc = setlocale(LC_TIME) || die "Cannot get time locale information:  $!";
59    setlocale(LC_TIME, "C") || die "Cannot setlocale() to C:  $!";
60}
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}
65my $jan_16 = 15 * 86400;
66is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", CORE::localtime($jan_16)),
67        "get ctime() equal to strftime()");
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)),
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');
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');
83unlike($ss, qr/\w/, 'Still not internally UTF-8 encoded');
84
85if (locales_enabled('LC_TIME')) {
86    setlocale(LC_TIME, $orig_time_loc) || die "Cannot setlocale(LC_TIME) back to orig: $!";
87}
88if (locales_enabled('LC_CTYPE')) {
89    setlocale(LC_CTYPE, $orig_ctype_loc) || die "Cannot setlocale(LC_CTYPE) back to orig: $!";
90}
91
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.
95like(clock(), qr/\d*/, "clock() returns a numeric value");
96cmp_ok(clock(), '>=', 0, "...and it returns something >= 0");
97
98SKIP: {
99    skip "No difftime()", 1 if $Config{d_difftime} ne 'define';
100    is(difftime(2, 1), 1, "difftime()");
101}
102
103SKIP: {
104    skip "No mktime()", 2 if $Config{d_mktime} ne 'define';
105    my $time = time();
106    is(mktime(CORE::localtime($time)), $time, "mktime()");
107    is(mktime(POSIX::localtime($time)), $time, "mktime()");
108}
109