wiki.pl: Port some fixes from upstream
[Orgmuse.git] / ts.pl
blob398e96729a946d3728939f5c0a2434b87ba58fff
1 # some routines taken from wiki.pl to help debug: it translates Perl
2 # times into human readable times
4 sub CalcDay {
5 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
6 return sprintf('%4d-%02d-%02d', $year+1900, $mon+1, $mday);
9 sub CalcTime {
10 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
11 return sprintf('%02d:%02d:%02d UTC', $hour, $min, $sec);
14 sub CalcTimeSince {
15 my $total = shift;
16 if ($total >= 7200) {
17 return Ts('%s hours ago',int($total/3600));
18 } elsif ($total >= 3600) {
19 return T('1 hour ago');
20 } elsif ($total >= 120) {
21 return Ts('%s minutes ago',int($total/60));
22 } elsif ($total >= 60) {
23 return T('1 minute ago');
24 } elsif ($total >= 2) {
25 return Ts('%s seconds ago',int($total));
26 } elsif ($total == 1) {
27 return T('1 second ago');
28 } else {
29 return T('just now');
33 sub TimeToText {
34 my $t = shift;
35 return CalcDay($t) . ' ' . CalcTime($t);
38 # Complete date plus hours and minutes: YYYY-MM-DDThh:mmTZD (eg
39 # 1997-07-16T19:20+01:00)
40 sub TimeToW3 {
41 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime(shift);
42 # use special UTC designator ("Z")
43 return sprintf('%4d-%02d-%02dT%02d:%02dZ', $year+1900, $mon+1, $mday, $hour, $min);
46 sub TimeToRFC822 {
47 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime(shift);
48 # Sat, 07 Sep 2002 00:00:01 GMT
49 return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT",
50 qw(Sun Mon Tue Wed Thu Fri Sat)[$wday], $mday,
51 qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)[$mon],
52 $year+1900, $hour, $min, $sec);
55 while(<>) {
56 s/(\d\d\d\d\d+)/TimeToText($1)/ge;
57 print;