This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix bug 36267 - assigning to a tied hash shouldn't change the
[perl5.git] / t / op / time.t
1 #!./perl
2
3 if ( $does_gmtime = gmtime(time) ) { 
4     print "1..7\n" 
5 }
6 else { 
7     print "1..4\n" 
8 }
9
10
11 my $test = 1;
12 sub 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
24
25 ($beguser,$begsys) = times;
26
27 $beg = time;
28
29 while (($now = time) == $beg) { sleep 1 }
30
31 ok($now > $beg && $now - $beg < 10,             'very basic time test');
32
33 for ($i = 0; $i < 1_000_000; $i++) {
34     ($nowuser, $nowsys) = times;
35     $i = 2_000_000 if $nowuser > $beguser && ( $nowsys >= $begsys ||
36                                             (!$nowsys && !$begsys));
37     last if time - $beg > 20;
38 }
39
40 ok($i >= 2_000_000, 'very basic times test');
41
42 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($beg);
43 ($xsec,$foo) = localtime($now);
44 $localyday = $yday;
45
46 ok($sec != $xsec && $mday && $year,             'localtime() list context');
47
48 ok(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   );
54
55 exit 0 unless $does_gmtime;
56
57 ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($beg);
58 ($xsec,$foo) = localtime($now);
59
60 ok($sec != $xsec && $mday && $year,             'gmtime() list context');
61
62 my $day_diff = $localyday - $yday;
63 ok( grep({ $day_diff == $_ } (0, 1, -1, 364, 365, -364, -365)),
64                      'gmtime() and localtime() agree what day of year');
65
66
67 # This could be stricter.
68 ok(gmtime() =~ /^(Sun|Mon|Tue|Wed|Thu|Fri|Sat)[ ]
69                  (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[ ]
70                  ([ \d]\d)\ (\d\d):(\d\d):(\d\d)\ (\d{4})$
71                /x,
72    'gmtime(), scalar context'
73   );