This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
RE: [perl #26136] localtime(3) calls tzset(3), but localtime_r(3) may not.
[perl5.git] / t / op / time.t
CommitLineData
8d063cd8
LW
1#!./perl
2
26575770 3if ( $does_gmtime = gmtime(time) ) {
8572b25d 4 print "1..8\n"
26575770
MS
5}
6else {
8572b25d 7 print "1..5\n"
26575770
MS
8}
9
10
11my $test = 1;
12sub ok ($$) {
13 my($ok, $name) = @_;
14
15 # You have to do it this way or VMS will get confused.
16 print $ok ? "ok $test - $name\n" : "not ok $test - $name\n";
17
18 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
19
20 $test++;
21 return $ok;
22}
23
8d063cd8
LW
24
25($beguser,$begsys) = times;
26
27$beg = time;
28
463ee0b2 29while (($now = time) == $beg) { sleep 1 }
8d063cd8 30
26575770 31ok($now > $beg && $now - $beg < 10, 'very basic time test');
8d063cd8 32
5f80d426 33for ($i = 0; $i < 1_000_000; $i++) {
8d063cd8 34 ($nowuser, $nowsys) = times;
5f80d426 35 $i = 2_000_000 if $nowuser > $beguser && ( $nowsys >= $begsys ||
a0d0e21e 36 (!$nowsys && !$begsys));
8d063cd8
LW
37 last if time - $beg > 20;
38}
39
5f80d426 40ok($i >= 2_000_000, 'very basic times test');
8d063cd8
LW
41
42($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
43($xsec,$foo) = localtime($now);
44$localyday = $yday;
45
26575770
MS
46ok($sec != $xsec && $mday && $year, 'localtime() list context');
47
48ok(localtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
49 (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
50 ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
51 /x,
52 'localtime(), scalar context'
53 );
8d063cd8 54
8572b25d
BH
55# check that localtime respects changes to $ENV{TZ}
56$ENV{TZ} = "GMT-5";
57($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
58$ENV{TZ} = "GMT+5";
59($sec,$min,$hour2,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
60ok($hour != $hour2, 'changes to $ENV{TZ} respected');
61
a0d0e21e
LW
62exit 0 unless $does_gmtime;
63
8d063cd8
LW
64($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
65($xsec,$foo) = localtime($now);
66
26575770
MS
67ok($sec != $xsec && $mday && $year, 'gmtime() list context');
68
69my $day_diff = $localyday - $yday;
70ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
71 'gmtime() and localtime() agree what day of year');
8d063cd8 72
f5a29b03
RB
73
74# This could be stricter.
26575770
MS
75ok(gmtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
76 (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
77 ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
78 /x,
79 'gmtime(), scalar context'
80 );