line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Time::Date; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
27601
|
use 5.006; |
|
1
|
|
|
|
|
4
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
48
|
|
6
|
1
|
|
|
1
|
|
984
|
use POSIX (); |
|
1
|
|
|
|
|
8768
|
|
|
1
|
|
|
|
|
33
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
11449
|
use overload '""' => \&str; |
|
1
|
|
|
|
|
1262
|
|
|
1
|
|
|
|
|
10
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
1
|
|
|
1
|
1
|
22
|
my ($class, $str) = @_; |
14
|
1
|
50
|
|
|
|
3
|
return undef if !$str; |
15
|
1
|
|
|
|
|
10
|
$str =~ /^(\d{4})(-(\d+)(-(\d+)((\s+|T)(\d+)(:(\d+)(:(\d+)(\.(\d+))?)?)?)?)?)?(.*)$/; |
16
|
1
|
|
|
|
|
4
|
my $year = $1; |
17
|
1
|
|
50
|
|
|
5
|
my $mon = $3 || 1; |
18
|
1
|
|
50
|
|
|
4
|
my $mday = $5 || 1; |
19
|
1
|
|
50
|
|
|
4
|
my $hour = $8 || 0; |
20
|
1
|
|
50
|
|
|
4
|
my $min = $10 || 0; |
21
|
1
|
|
50
|
|
|
7
|
my $sec = $12 || 0; |
22
|
1
|
|
50
|
|
|
5
|
my $msec = $14 || 0; |
23
|
1
|
|
|
|
|
2
|
my $ampm = $15; |
24
|
1
|
50
|
|
|
|
4
|
return undef if !defined $year; |
25
|
1
|
50
|
|
|
|
8
|
if ($ampm =~ /^\s*am?$/i) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
26
|
1
|
50
|
|
|
|
4
|
$hour -= 12 if $hour == 12; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
elsif ($ampm =~ /^\s*pm?$/i) { |
29
|
0
|
0
|
|
|
|
0
|
$hour += 12 if $hour != 12; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
elsif ($ampm !~ /^\s*$/) { |
32
|
0
|
|
|
|
|
0
|
return undef; |
33
|
|
|
|
|
|
|
} |
34
|
1
|
|
|
|
|
2
|
$mon -= 1; |
35
|
1
|
|
|
|
|
1
|
$year -= 1900; |
36
|
1
|
|
|
|
|
15081
|
my $epoch = POSIX::mktime($sec, $min, $hour, $mday, $mon, $year, 0, 0, -1); |
37
|
1
|
50
|
|
|
|
16
|
return undef if !defined $epoch; |
38
|
1
|
|
|
|
|
9
|
my $self = bless {epoch => $epoch}, $class; |
39
|
1
|
|
|
|
|
12
|
return $self; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub mktime { |
43
|
0
|
|
|
0
|
1
|
0
|
my ($class, $year, $mon, $mday, $hour, $min, $sec, $wday, $yday, $isdst) = @_; |
44
|
0
|
|
0
|
|
|
0
|
$sec ||= 0; |
45
|
0
|
|
0
|
|
|
0
|
$min ||= 0; |
46
|
0
|
|
0
|
|
|
0
|
$hour ||= 0; |
47
|
0
|
0
|
|
|
|
0
|
$mday = defined $mday ? $mday : 1; |
48
|
0
|
|
0
|
|
|
0
|
$mon ||= 0; |
49
|
0
|
|
0
|
|
|
0
|
$year ||= 0; |
50
|
0
|
|
0
|
|
|
0
|
$wday ||= 0; |
51
|
0
|
|
0
|
|
|
0
|
$yday ||= 0; |
52
|
0
|
0
|
|
|
|
0
|
$isdst = defined $isdst ? $isdst : -1; |
53
|
0
|
|
|
|
|
0
|
my $epoch = POSIX::mktime($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst); |
54
|
0
|
0
|
|
|
|
0
|
return undef if !defined $epoch; |
55
|
0
|
|
|
|
|
0
|
my $self = bless {epoch => $epoch}, $class; |
56
|
0
|
|
|
|
|
0
|
return $self; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub new_epoch { |
60
|
1
|
|
|
1
|
1
|
433
|
my ($class, $epoch) = @_; |
61
|
1
|
|
|
|
|
6
|
my $self = bless {epoch => $epoch}, $class; |
62
|
1
|
|
|
|
|
5
|
return $self; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub now { |
66
|
1
|
|
|
1
|
1
|
5
|
my ($class) = @_; |
67
|
1
|
|
|
|
|
8
|
my $self = bless {epoch => time}, $class; |
68
|
1
|
|
|
|
|
6
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub str { |
72
|
3
|
|
|
3
|
1
|
827
|
my ($self) = @_; |
73
|
3
|
|
|
|
|
2030
|
my $str = POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime($self->{epoch})); |
74
|
3
|
|
|
|
|
40
|
return $str; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub strftime { |
78
|
0
|
|
|
0
|
1
|
0
|
my ($self, $fmt) = @_; |
79
|
0
|
|
|
|
|
0
|
my $str = POSIX::strftime($fmt, localtime($self->{epoch})); |
80
|
0
|
|
|
|
|
0
|
return $str; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $now; |
84
|
|
|
|
|
|
|
sub natural { |
85
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
86
|
0
|
0
|
|
|
|
0
|
$now = time if !$now; |
87
|
0
|
|
|
|
|
0
|
my $delta = $now - $self->{epoch}; |
88
|
0
|
0
|
|
|
|
0
|
if ($delta < -32 * 24 * 60 * 60) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
89
|
0
|
|
|
|
|
0
|
return POSIX::strftime("%b %Y", localtime($self->{epoch})); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
elsif ($delta < -2 * 24 * 60 * 60) { |
92
|
0
|
|
|
|
|
0
|
return "in " . int(-$delta / (24 * 60 * 60)) . " days"; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
elsif ($delta < -24 * 60 * 60) { |
95
|
0
|
|
|
|
|
0
|
return "in 1 day"; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
elsif ($delta < -2 * 60 * 60) { |
98
|
0
|
|
|
|
|
0
|
return "in " . int(-$delta / (60 * 60)) . " hours"; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
elsif ($delta < -60 * 60) { |
101
|
0
|
|
|
|
|
0
|
return "in 1 hour"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
elsif ($delta < -2 * 60) { |
104
|
0
|
|
|
|
|
0
|
return "in " . int(-$delta / 60) . " minutes"; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
elsif ($delta < -60) { |
107
|
0
|
|
|
|
|
0
|
return "in 1 minute"; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
elsif ($delta < -1) { |
110
|
0
|
|
|
|
|
0
|
return "in $delta seconds"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
elsif ($delta < 0) { |
113
|
0
|
|
|
|
|
0
|
return "in 1 second"; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
elsif ($delta < 1) { |
116
|
0
|
|
|
|
|
0
|
return "right now"; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
elsif ($delta < 2) { |
119
|
0
|
|
|
|
|
0
|
return "1 second ago"; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
0
|
|
|
|
0
|
if ($delta < 60) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
122
|
0
|
|
|
|
|
0
|
return "$delta seconds ago"; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ($delta < 2 * 60) { |
125
|
0
|
|
|
|
|
0
|
return "1 minute ago"; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
elsif ($delta < 60 * 60) { |
128
|
0
|
|
|
|
|
0
|
return int($delta / 60) . " minutes ago"; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
elsif ($delta < 2 * 60 * 60) { |
131
|
0
|
|
|
|
|
0
|
return "1 hour ago"; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
elsif ($delta < 24 * 60 * 60) { |
134
|
0
|
|
|
|
|
0
|
return int($delta / (60 * 60)) . " hours ago"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
elsif ($delta < 2 * 24 * 60 * 60) { |
137
|
0
|
|
|
|
|
0
|
return "1 day ago"; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
elsif ($delta < 32 * 24 * 60 * 60) { |
140
|
0
|
|
|
|
|
0
|
return int($delta / (24 * 60 * 60)) . " days ago"; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
else { |
143
|
0
|
|
|
|
|
0
|
return POSIX::strftime("%b %Y", localtime($self->{epoch})); |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub time_zones { |
148
|
1
|
|
|
1
|
1
|
3
|
my %time_zones; |
149
|
1
|
50
|
|
|
|
152
|
open my $fh, "<", "/usr/share/zoneinfo/zone.tab" or return []; |
150
|
1
|
|
|
|
|
1143
|
while (my $line = <$fh>) { |
151
|
440
|
100
|
|
|
|
2261
|
$line =~ m{^[A-Z]{2}\s+\S+\s+(\w+)/(\S+)} or next; |
152
|
416
|
|
|
|
|
898
|
my $region = $1; |
153
|
416
|
|
|
|
|
883
|
my $name = $2; |
154
|
416
|
|
|
|
|
1046
|
my $zone = "$region/$name"; |
155
|
416
|
|
|
|
|
842
|
$name =~ s{_}{ }g; |
156
|
416
|
|
|
|
|
753
|
$name =~ s{/}{ - }g; |
157
|
416
|
|
|
|
|
630
|
push @{$time_zones{$region}}, {zone => $zone, name => $name}; |
|
416
|
|
|
|
|
3216
|
|
158
|
|
|
|
|
|
|
} |
159
|
1
|
|
|
|
|
11
|
close $fh; |
160
|
1
|
|
|
|
|
3
|
my @time_zones; |
161
|
1
|
|
|
|
|
5
|
push @time_zones, {name => "Local", zone => ""}; |
162
|
1
|
|
|
|
|
2
|
my $i = 0; |
163
|
1
|
|
|
|
|
12
|
for my $region (sort keys %time_zones) { |
164
|
10
|
|
|
|
|
26
|
push @time_zones, {region => $region, i => $i}; |
165
|
10
|
|
|
|
|
9
|
$i++; |
166
|
10
|
|
|
|
|
12
|
my @zones = sort {$a->{name} cmp $b->{name}} @{$time_zones{$region}}; |
|
2014
|
|
|
|
|
2378
|
|
|
10
|
|
|
|
|
46
|
|
167
|
10
|
|
|
|
|
69
|
push @time_zones, @zones; |
168
|
|
|
|
|
|
|
} |
169
|
1
|
|
|
|
|
5
|
push @time_zones, {region => "UTC", i => $i}; |
170
|
1
|
|
|
|
|
4
|
for my $offset (qw{ |
171
|
|
|
|
|
|
|
-12 -11:30 -11 -10:30 -10 -9:30 -8:30 -8 -7:30 -7 -6:30 -6 -5:30 |
172
|
|
|
|
|
|
|
-5 -4:30 -4 -3:30 -3 -2:30 -2 -1:30 -1 -0:30 +0 +0:30 +1 +1:30 |
173
|
|
|
|
|
|
|
+2 +2:30 +3 +3:30 +4 +4:30 +5 +5:30 +6 +6:30 +7 +7:30 +8 +8:30 |
174
|
|
|
|
|
|
|
+9 +9:30 +10 +10:30 +11 +11:30 +12}) { |
175
|
48
|
|
|
|
|
164
|
push @time_zones, {zone => "UTC$offset", name => "UTC$offset"}; |
176
|
|
|
|
|
|
|
} |
177
|
1
|
|
|
|
|
21
|
return \@time_zones; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
1; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
__END__ |