Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 108 lines (82 sloc) 2.659 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
#!perl -w

# Test compatibility with Perl's time stuff.

use perl5i::latest;

use Test::More 'no_plan';
use Test::Output;


#localtime in scalar context
like localtime(), qr{
^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
}x;

my $beg = time();

SKIP: {
    local $ENV{TZ};

    # Two time zones, different and likely to exist
    my $tz1 = "America/Los_Angeles";
    my $tz2 = "America/Chicago";

    # If the core localtime doesn't respond to TZ, we don't have to.
    skip "localtime does not respect TZ env", 1
      unless do {
          # check that localtime respects changes to $ENV{TZ}
          $ENV{TZ} = $tz1;
          my $hour = (CORE::localtime($beg))[2];
          $ENV{TZ} = $tz2;
          my $hour2 = (CORE::localtime($beg))[2];
          $hour != $hour2;
      };

    # check that localtime respects changes to $ENV{TZ}
    $ENV{TZ} = $tz1;
    my $hour = (localtime($beg))[2];
    $ENV{TZ} = $tz2;
    my $hour2 = (localtime($beg))[2];
    isnt $hour, $hour2, "localtime() honors TZ";
}


sleep 1;
my $now = time();

{
    my( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = gmtime($beg);
    my( $xsec, $foo ) = localtime($now);

    isnt( $sec, $xsec );
    ok $mday;
    ok $year;

    my $localyday = (localtime)[7];
    my $day_diff = $localyday - $yday;
    ok(
        grep( { $day_diff == $_ } ( 0, 1, -1, 364, 365, -364, -365 ) ),
        'gmtime() and localtime() agree what day of year'
    );


    # This could be stricter.
    like(
        gmtime(), qr/^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
/x,
        'gmtime(), scalar context'
    );
}


# Make sure the return from gmtime() compares as a string
{
    my $dt = gmtime(123);
    my $string = CORE::gmtime(123);

    # Don't use is() or cmp_ok() as they can strip off overloading.
    # We want to explicitly check eq
    ok $dt eq $string, 'gmtime eq';
}


# Make sure time compares as a number
{
    my $dt = time;
    my $num = $dt+0;

    ok $dt == $num;
}


# Test times honor say and print
{
    # Due to a bug in 5.10's tie, the newline on say gets lost, but
    # it will be back in 5.12. So we can't test for it.
    stdout_like { time->say; } qr/^\d+$/;
    stdout_like { time->print; } qr/^\d+$/;

    my $time = int rand 2**31;
    my $date = gmtime($time);
    stdout_like { gmtime($time)->say; } qr{^\Q$date\E$};
    stdout_like { gmtime($time)->print; } qr{^\Q$date\E$};
}
Something went wrong with that request. Please try again.