line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#+############################################################################## |
2
|
|
|
|
|
|
|
# # |
3
|
|
|
|
|
|
|
# File: No/Worries/Date.pm # |
4
|
|
|
|
|
|
|
# # |
5
|
|
|
|
|
|
|
# Description: date handling without worries # |
6
|
|
|
|
|
|
|
# # |
7
|
|
|
|
|
|
|
#-############################################################################## |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# |
10
|
|
|
|
|
|
|
# module definition |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package No::Worries::Date; |
14
|
3
|
|
|
3
|
|
66043
|
use strict; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
101
|
|
15
|
3
|
|
|
3
|
|
35
|
use warnings; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
88
|
|
16
|
3
|
|
|
3
|
|
65
|
use 5.005; # need the four-argument form of substr() |
|
3
|
|
|
|
|
11
|
|
17
|
|
|
|
|
|
|
our $VERSION = "1.5"; |
18
|
|
|
|
|
|
|
our $REVISION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# used modules |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
|
24
|
3
|
|
|
3
|
|
1193
|
use HTTP::Date qw(str2time); |
|
3
|
|
|
|
|
12487
|
|
|
3
|
|
|
|
|
224
|
|
25
|
3
|
|
|
3
|
|
741
|
use No::Worries::Die qw(dief); |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
25
|
|
26
|
3
|
|
|
3
|
|
25
|
use No::Worries::Export qw(export_control); |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
19
|
|
27
|
3
|
|
|
3
|
|
22
|
use Params::Validate qw(validate_pos :types); |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
577
|
|
28
|
3
|
|
|
3
|
|
1174
|
use POSIX qw(strftime); |
|
3
|
|
|
|
|
18277
|
|
|
3
|
|
|
|
|
20
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
# constants |
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
3
|
|
4228
|
use constant STRFTIME_STRING_FORMAT => "%Y-%m-%dT%H:%M:%SZ"; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
260
|
|
35
|
3
|
|
|
3
|
|
23
|
use constant STRFTIME_STAMP_FORMAT => "%Y/%m/%d-%H:%M:%S"; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
260
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
# handle the given time that could be undef or '1.433330218094E9' |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _time ($) { |
42
|
20
|
|
|
20
|
|
42
|
my($time) = @_; |
43
|
|
|
|
|
|
|
|
44
|
20
|
100
|
|
|
|
48
|
return(time(), 0) unless defined($time); |
45
|
18
|
|
|
|
|
29
|
eval { |
46
|
3
|
|
|
3
|
|
22
|
use warnings FATAL => qw(numeric); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1616
|
|
47
|
18
|
|
|
|
|
55
|
$time += 0; |
48
|
|
|
|
|
|
|
}; |
49
|
18
|
100
|
|
|
|
42
|
dief("invalid time: %s", $time) if $@; |
50
|
16
|
100
|
|
|
|
141
|
if ($time =~ /^(\d+)$/) { |
|
|
50
|
|
|
|
|
|
51
|
10
|
|
|
|
|
48
|
return($1, 0); |
52
|
|
|
|
|
|
|
} elsif ($time =~ /^(\d+)\.(\d+)$/) { |
53
|
6
|
|
|
|
|
25
|
return($1, $2); |
54
|
|
|
|
|
|
|
} else { |
55
|
0
|
|
|
|
|
0
|
dief("invalid time: %s", $time); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# |
60
|
|
|
|
|
|
|
# convert a string to a time |
61
|
|
|
|
|
|
|
# |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub date_parse ($) { |
64
|
14
|
|
|
14
|
1
|
7990
|
my($string) = @_; |
65
|
14
|
|
|
|
|
25
|
my($time); |
66
|
|
|
|
|
|
|
|
67
|
14
|
|
|
|
|
169
|
validate_pos(@_, { type => SCALAR }); |
68
|
14
|
|
|
|
|
64
|
$time = str2time($string); |
69
|
14
|
100
|
|
|
|
1575
|
dief("invalid date: %s", $string) unless defined($time); |
70
|
13
|
|
|
|
|
35
|
return($time); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# |
74
|
|
|
|
|
|
|
# convert a time to human friendly string (local time) |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub date_stamp (;$) { |
78
|
10
|
|
|
10
|
1
|
3531
|
my($time) = @_; |
79
|
10
|
|
|
|
|
21
|
my($int, $frac, $string); |
80
|
|
|
|
|
|
|
|
81
|
10
|
100
|
|
|
|
131
|
validate_pos(@_, { type => SCALAR }) if @_; |
82
|
10
|
|
|
|
|
30
|
($int, $frac) = _time($time); |
83
|
9
|
|
|
|
|
259
|
$string = strftime(STRFTIME_STAMP_FORMAT, localtime($int)); |
84
|
9
|
100
|
|
|
|
34
|
$string .= ".$frac" if $frac; |
85
|
9
|
|
|
|
|
30
|
return($string); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# |
89
|
|
|
|
|
|
|
# convert a time to an ISO 8601 compliant string (UTC based) |
90
|
|
|
|
|
|
|
# |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub date_string (;$) { |
93
|
10
|
|
|
10
|
1
|
540
|
my($time) = @_; |
94
|
10
|
|
|
|
|
21
|
my($int, $frac, $string); |
95
|
|
|
|
|
|
|
|
96
|
10
|
100
|
|
|
|
129
|
validate_pos(@_, { type => SCALAR }) if @_; |
97
|
10
|
|
|
|
|
42
|
($int, $frac) = _time($time); |
98
|
9
|
|
|
|
|
258
|
$string = strftime(STRFTIME_STRING_FORMAT, gmtime($int)); |
99
|
9
|
100
|
|
|
|
36
|
substr($string, -1, 0, ".$frac") if $frac; |
100
|
9
|
|
|
|
|
35
|
return($string); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# export control |
105
|
|
|
|
|
|
|
# |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub import : method { |
108
|
3
|
|
|
3
|
|
17
|
my($pkg, %exported); |
109
|
|
|
|
|
|
|
|
110
|
3
|
|
|
|
|
8
|
$pkg = shift(@_); |
111
|
3
|
|
|
|
|
23
|
grep($exported{$_}++, map("date_$_", qw(parse stamp string))); |
112
|
3
|
|
|
|
|
23
|
export_control(scalar(caller()), $pkg, \%exported, @_); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__DATA__ |