xref: /openbsd-src/gnu/usr.bin/perl/ext/POSIX/t/time.t (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1#!perl -w
2
3use strict;
4
5use Config;
6use POSIX;
7use Test::More tests => 9;
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
31# asctime and ctime...Let's stay below INT_MAX for 32-bits and
32# positive for some picky systems.
33
34is(asctime(localtime(0)), ctime(0), "asctime() and ctime() at zero");
35is(asctime(localtime(12345678)), ctime(12345678), "asctime() and ctime() at 12345678");
36
37# Careful!  strftime() is locale sensative.  Let's take care of that
38my $orig_loc = setlocale(LC_TIME, "C") || die "Cannot setlocale() to C:  $!";
39my $jan_16 = 15 * 86400;
40is(ctime($jan_16), strftime("%a %b %d %H:%M:%S %Y\n", localtime($jan_16)),
41        "get ctime() equal to strftime()");
42setlocale(LC_TIME, $orig_loc) || die "Cannot setlocale() back to orig: $!";
43
44# clock() seems to have different definitions of what it does between POSIX
45# and BSD.  Cygwin, Win32, and Linux lean the BSD way.  So, the tests just
46# check the basics.
47like(clock(), qr/\d*/, "clock() returns a numeric value");
48ok(clock() >= 0, "...and it returns something >= 0");
49
50SKIP: {
51    skip "No difftime()", 1 if $Config{d_difftime} ne 'define';
52    is(difftime(2, 1), 1, "difftime()");
53}
54
55SKIP: {
56    skip "No mktime()", 1 if $Config{d_mktime} ne 'define';
57    my $time = time();
58    is(mktime(localtime($time)), $time, "mktime()");
59}
60