line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# $Id: Date.pm,v 1.3 2003/01/16 14:29:10 eserte Exp $ |
5
|
|
|
|
|
|
|
# Author: Slaven Rezic |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# Copyright (C) 2001 Online Office Berlin. All rights reserved. |
8
|
|
|
|
|
|
|
# Copyright (C) 2002 Slaven Rezic. |
9
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under the |
10
|
|
|
|
|
|
|
# terms of the GNU General Public License, see the file COPYING. |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# |
13
|
|
|
|
|
|
|
# Mail: slaven@rezic.de |
14
|
|
|
|
|
|
|
# WWW: http://we-framework.sourceforge.net |
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
package WE::Util::Date; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 NAME |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
WE::Util::Date - date-specific functions |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 SYNOPSIS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
use WE::Util::Date; |
26
|
|
|
|
|
|
|
print scalar localtime isodate2epoch("2001-12-12 12:23:00"); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=head1 DESCRIPTION |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
This is a helper class for date functions. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
=cut |
33
|
|
|
|
|
|
|
|
34
|
16
|
|
|
16
|
|
258
|
use base qw(Exporter); |
|
16
|
|
|
|
|
33
|
|
|
16
|
|
|
|
|
1415
|
|
35
|
|
|
|
|
|
|
|
36
|
16
|
|
|
16
|
|
103
|
use strict; |
|
16
|
|
|
|
|
151
|
|
|
16
|
|
|
|
|
492
|
|
37
|
16
|
|
|
16
|
|
128
|
use vars qw($VERSION @EXPORT @EXPORT_OK); |
|
16
|
|
|
|
|
43
|
|
|
16
|
|
|
|
|
1924
|
|
38
|
|
|
|
|
|
|
$VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
@EXPORT = qw(isodate2epoch epoch2isodate); |
41
|
|
|
|
|
|
|
@EXPORT_OK = qw(short_readable_time); |
42
|
|
|
|
|
|
|
|
43
|
16
|
|
|
16
|
|
88
|
use constant T_FMT => "%04d-%02d-%02d %02d:%02d:%02d"; |
|
16
|
|
|
|
|
33
|
|
|
16
|
|
|
|
|
8265
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# some methods stolen from DateHelper.pm elsewhere |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head1 FUNCTIONS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head2 isodate2epoch($isodate) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Return time in seconds since UNIX epoch for the given ISO 8601 string |
52
|
|
|
|
|
|
|
(YYYY-MM-DD HH:MM:SS or YYYY-MM-DD). If Date::Manip is installed, more |
53
|
|
|
|
|
|
|
ISO formats are allowed. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=cut |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub isodate2epoch { |
58
|
0
|
|
|
0
|
1
|
0
|
my $isodate = shift; |
59
|
0
|
|
|
|
|
0
|
require Time::Local; |
60
|
0
|
|
|
|
|
0
|
my($y,$m,$d,$H,$M,$S); |
61
|
0
|
0
|
|
|
|
0
|
if ($isodate =~ /^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})$/) { |
|
|
0
|
|
|
|
|
|
62
|
0
|
|
|
|
|
0
|
($y,$m,$d,$H,$M,$S) = ($1,$2,$3,$4,$5,$6); |
63
|
|
|
|
|
|
|
} elsif ($isodate =~ /^(\d{4})-(\d{2})-(\d{2})$/) { # crippled without time |
64
|
0
|
|
|
|
|
0
|
($y,$m,$d,$H,$M,$S) = ($1,$2,$3,0,0,0); |
65
|
|
|
|
|
|
|
} else { |
66
|
0
|
|
|
|
|
0
|
require Date::Manip; |
67
|
0
|
|
|
|
|
0
|
return Date::Manip::ParseDate($isodate); |
68
|
|
|
|
|
|
|
} |
69
|
0
|
|
|
|
|
0
|
Time::Local::timelocal($S,$M,$H,$d,$m-1,$y-1900); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 epoch2isodate($time) |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Return time as ISO 8601 date from given time in seconds since UNIX epoch. |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub epoch2isodate { |
79
|
1
|
50
|
|
1
|
1
|
153
|
my @l = @_ ? localtime $_[0] : localtime; |
80
|
1
|
|
|
|
|
10
|
sprintf(T_FMT, |
81
|
|
|
|
|
|
|
$l[5]+1900, $l[4]+1, $l[3], |
82
|
|
|
|
|
|
|
$l[2], $l[1], $l[0]); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head2 short_readable_time($epoch) |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Return the short time E la "ls -al". (Not exported by default). |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=cut |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub short_readable_time { |
92
|
0
|
|
|
0
|
1
|
|
my $epoch = shift; |
93
|
0
|
|
|
|
|
|
my @l = localtime $epoch; |
94
|
0
|
|
|
|
|
|
my @now = localtime; |
95
|
0
|
|
|
|
|
|
my $s = sprintf "%3s %2d ", |
96
|
|
|
|
|
|
|
[qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/]->[$l[4]], |
97
|
|
|
|
|
|
|
$l[3]; |
98
|
0
|
0
|
|
|
|
|
if ($l[5] == $now[5]) { |
99
|
0
|
|
|
|
|
|
$s .= sprintf "%02d:%02d", $l[2], $l[1]; |
100
|
|
|
|
|
|
|
} else { |
101
|
0
|
|
|
|
|
|
$s .= sprintf "%5d", $l[5]+1900; |
102
|
|
|
|
|
|
|
} |
103
|
0
|
|
|
|
|
|
$s; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
__END__ |