]> dxcluster.net Git - spider.git/blob - perl/IsoTime.pm
fix xmlout
[spider.git] / perl / IsoTime.pm
1 #
2 # Utility routines for handling Iso 8601 date time groups
3 #
4 # $Id$
5 #
6 # Copyright (c) Dirk Koopman, G1TLH
7 #
8
9 use strict;
10
11 package IsoTime;
12
13 use Date::Parse;
14
15 use vars qw($VERSION $BRANCH $year $month $day $hour $min $sec @days @ldays);
16 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
17 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/  || (0,0));
18 $main::build += $VERSION;
19 $main::branch += $BRANCH;
20
21 @days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
22 @ldays = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
23
24 # is it a leap year?
25 sub _isleap
26 {
27         my $year = shift;
28         return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0; 
29 }
30
31 sub full
32 {
33         return sprintf "%04d%02d%02dT%02d%02d%02d", $year, $month, $day, $hour, $min, $sec; 
34 }
35
36 sub dayminsec
37 {
38         return sprintf "%02dT%02d%02d%02d", $day, $hour, $min, $sec; 
39 }
40
41 sub daymin
42 {
43         return sprintf "%02dT%02d%02d", $day, $hour, $min; 
44 }
45
46 sub update
47 {
48         my $t = shift || time;
49         ($sec,$min,$hour,$day,$month,$year) = gmtime($t);
50         $month++;
51         $year += 1900;
52 }
53
54 sub unixtime
55 {
56         my $iso = shift;
57         
58         # get the correct month and year if it is a short date
59         if (my ($d) = $iso =~ /^(\d\d)T\d\d\d\d/) {
60                 if ($d == $day) {
61                         $iso = sprintf("%04d%02d", $year, $month) . $iso;
62                 } else {
63                         my $days = _isleap($year) ? $ldays[$month-1] : $days[$month-1];
64                         my ($y, $m) = ($year, $month);
65                         if ($d < $day) {
66                                 if ($day - $d > $days / 2) {
67                                         if ($month == 1) {
68                                                 $y = $year - 1;
69                                                 $m = 12;
70                                         } else {
71                                                 $m = $month - 1;
72                                         }
73                                 } 
74                         } else {
75                                 if ($d - $day > $days / 2) {
76                                         if ($month == 12) {
77                                                 $y = $year + 1;
78                                                 $m = 1;
79                                         } else {
80                                                 $m = $month + 1;
81                                         }
82                                 }
83                         }
84                         $iso = sprintf("%04d%02d", $y, $m) . $iso;
85                 }
86         }
87         return str2time($iso);
88 }
89 1;