summaryrefslogtreecommitdiff
path: root/lib/Time/Local.t
blob: d70383a640fed95faaf03782e81a05f665bf48b1 (plain)
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
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}

use Time::Local;

# Set up time values to test
@time =
  (
   #year,mon,day,hour,min,sec 
   [1970,  1,  2, 00, 00, 00],
   [1980,  2, 28, 12, 00, 00],
   [1980,  2, 29, 12, 00, 00],
   [1999, 12, 31, 23, 59, 59],
   [2000,  1,  1, 00, 00, 00],
   [2010, 10, 12, 14, 13, 12],
   [2020,  2, 29, 12, 59, 59],
   [2030,  7,  4, 17, 07, 06],
   [2038,  1, 17, 23, 59, 59],     # last full day in any tz
  );

# use vmsish 'time' makes for oddness around the Unix epoch
if ($^O eq 'VMS') { $time[0][2]++ }

print "1..", @time * 2 + 5, "\n";

$count = 1;
for (@time) {
    my($year, $mon, $mday, $hour, $min, $sec) = @$_;
    $year -= 1900;
    $mon --;
    if ($^O eq 'vos' && $count == 1) {
     print "ok $count -- skipping 1970 test on VOS.\n";
    } else {
     my $time = timelocal($sec,$min,$hour,$mday,$mon,$year);
     # print scalar(localtime($time)), "\n";
     my($s,$m,$h,$D,$M,$Y) = localtime($time);

     if ($s == $sec &&
	 $m == $min &&
	 $h == $hour &&
	 $D == $mday &&
	 $M == $mon &&
	 $Y == $year
        ) {
	 print "ok $count\n";
     } else {
      print "not ok $count\n";
     }
    }
    $count++;

    # Test gmtime function
    if ($^O eq 'vos' && $count == 2) {
        print "ok $count -- skipping 1970 test on VOS.\n";
    } else {
     $time = timegm($sec,$min,$hour,$mday,$mon,$year);
     ($s,$m,$h,$D,$M,$Y) = gmtime($time);

     if ($s == $sec &&
	 $m == $min &&
	 $h == $hour &&
	 $D == $mday &&
	 $M == $mon &&
	 $Y == $year
        ) {
	 print "ok $count\n";
     } else {
      print "not ok $count\n";
     }
    }
    $count++;
}

#print "Testing that the differences between a few dates makes sense...\n";

timelocal(0,0,1,1,0,90) - timelocal(0,0,0,1,0,90) == 3600
  or print "not ";
print "ok ", $count++, "\n";

timelocal(1,2,3,1,0,100) - timelocal(1,2,3,31,11,99) == 24 * 3600 
  or print "not ";
print "ok ", $count++, "\n";

# Diff beween Jan 1, 1980 and Mar 1, 1980 = (31 + 29 = 60 days)
timegm(0,0,0, 1, 2, 80) - timegm(0,0,0, 1, 0, 80) == 60 * 24 * 3600
  or print "not ";
print "ok ", $count++, "\n";


#print "Testing timelocal.pl module too...\n";
package test;
require 'timelocal.pl';
timegm(0,0,0,1,0,80) == main::timegm(0,0,0,1,0,80) or print "not ";
print "ok ", $main::count++, "\n";

timelocal(1,2,3,4,5,88) == main::timelocal(1,2,3,4,5,88) or print "not ";
print "ok ", $main::count++, "\n";