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
|
|
53792
|
use strict; |
|
3
|
|
|
|
|
16
|
|
|
3
|
|
|
|
|
74
|
|
15
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
57
|
|
16
|
3
|
|
|
3
|
|
47
|
use 5.005; # need the four-argument form of substr() |
|
3
|
|
|
|
|
9
|
|
17
|
|
|
|
|
|
|
our $VERSION = "1.6"; |
18
|
|
|
|
|
|
|
our $REVISION = sprintf("%d.%02d", q$Revision: 1.13 $ =~ /(\d+)\.(\d+)/); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# used modules |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
|
24
|
3
|
|
|
3
|
|
1302
|
use HTTP::Date qw(str2time); |
|
3
|
|
|
|
|
10717
|
|
|
3
|
|
|
|
|
159
|
|
25
|
3
|
|
|
3
|
|
674
|
use No::Worries::Die qw(dief); |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
17
|
|
26
|
3
|
|
|
3
|
|
18
|
use No::Worries::Export qw(export_control); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
15
|
|
27
|
3
|
|
|
3
|
|
15
|
use Params::Validate qw(validate_pos :types); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
424
|
|
28
|
3
|
|
|
3
|
|
1264
|
use POSIX qw(strftime); |
|
3
|
|
|
|
|
15636
|
|
|
3
|
|
|
|
|
52
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# |
31
|
|
|
|
|
|
|
# constants |
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
|
34
|
3
|
|
|
3
|
|
3597
|
use constant STRFTIME_STRING_FORMAT => "%Y-%m-%dT%H:%M:%SZ"; |
|
3
|
|
|
|
|
34
|
|
|
3
|
|
|
|
|
264
|
|
35
|
3
|
|
|
3
|
|
20
|
use constant STRFTIME_STAMP_FORMAT => "%Y/%m/%d-%H:%M:%S"; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
211
|
|
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# |
38
|
|
|
|
|
|
|
# handle the given time that could be undef or '1.433330218094E9' |
39
|
|
|
|
|
|
|
# |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _time ($) { |
42
|
20
|
|
|
20
|
|
34
|
my($time) = @_; |
43
|
|
|
|
|
|
|
|
44
|
20
|
100
|
|
|
|
35
|
return(time(), 0) unless defined($time); |
45
|
18
|
|
|
|
|
22
|
eval { |
46
|
3
|
|
|
3
|
|
16
|
use warnings FATAL => qw(numeric); |
|
3
|
|
|
|
|
12
|
|
|
3
|
|
|
|
|
1452
|
|
47
|
18
|
|
|
|
|
53
|
$time += 0; |
48
|
|
|
|
|
|
|
}; |
49
|
18
|
100
|
|
|
|
36
|
dief("invalid time: %s", $time) if $@; |
50
|
16
|
100
|
|
|
|
130
|
if ($time =~ /^(\d+)$/) { |
|
|
50
|
|
|
|
|
|
51
|
10
|
|
|
|
|
35
|
return($1, 0); |
52
|
|
|
|
|
|
|
} elsif ($time =~ /^(\d+)\.(\d+)$/) { |
53
|
6
|
|
|
|
|
26
|
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
|
10370
|
my($string) = @_; |
65
|
14
|
|
|
|
|
19
|
my($time); |
66
|
|
|
|
|
|
|
|
67
|
14
|
|
|
|
|
248
|
validate_pos(@_, { type => SCALAR }); |
68
|
14
|
|
|
|
|
47
|
$time = str2time($string); |
69
|
14
|
100
|
|
|
|
1316
|
dief("invalid date: %s", $string) unless defined($time); |
70
|
13
|
|
|
|
|
28
|
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
|
3410
|
my($time) = @_; |
79
|
10
|
|
|
|
|
17
|
my($int, $frac, $string); |
80
|
|
|
|
|
|
|
|
81
|
10
|
100
|
|
|
|
99
|
validate_pos(@_, { type => SCALAR }) if @_; |
82
|
10
|
|
|
|
|
30
|
($int, $frac) = _time($time); |
83
|
9
|
|
|
|
|
330
|
$string = strftime(STRFTIME_STAMP_FORMAT, localtime($int)); |
84
|
9
|
100
|
|
|
|
35
|
$string .= ".$frac" if $frac; |
85
|
9
|
|
|
|
|
32
|
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
|
550
|
my($time) = @_; |
94
|
10
|
|
|
|
|
16
|
my($int, $frac, $string); |
95
|
|
|
|
|
|
|
|
96
|
10
|
100
|
|
|
|
115
|
validate_pos(@_, { type => SCALAR }) if @_; |
97
|
10
|
|
|
|
|
26
|
($int, $frac) = _time($time); |
98
|
9
|
|
|
|
|
416
|
$string = strftime(STRFTIME_STRING_FORMAT, gmtime($int)); |
99
|
9
|
100
|
|
|
|
40
|
substr($string, -1, 0, ".$frac") if $frac; |
100
|
9
|
|
|
|
|
32
|
return($string); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# |
104
|
|
|
|
|
|
|
# export control |
105
|
|
|
|
|
|
|
# |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub import : method { |
108
|
3
|
|
|
3
|
|
13
|
my($pkg, %exported); |
109
|
|
|
|
|
|
|
|
110
|
3
|
|
|
|
|
6
|
$pkg = shift(@_); |
111
|
3
|
|
|
|
|
20
|
grep($exported{$_}++, map("date_$_", qw(parse stamp string))); |
112
|
3
|
|
|
|
|
16
|
export_control(scalar(caller()), $pkg, \%exported, @_); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
1; |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
__DATA__ |