2 # Utility routines for handling Iso 8601 date time groups
6 # Copyright (c) Dirk Koopman, G1TLH
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;
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);
28 return ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 1 : 0;
33 return sprintf "%04d%02d%02dT%02d%02d%02d", $year, $month, $day, $hour, $min, $sec;
38 return sprintf "%02dT%02d%02d%02d", $day, $hour, $min, $sec;
43 return sprintf "%02dT%02d%02d", $day, $hour, $min;
48 return sprintf "%02d%02d", $hour, $min;
54 return sprintf "%02d%02d%02d", $hour, $min, $sec;
59 my $t = shift || time;
60 ($sec,$min,$hour,$day,$month,$year) = gmtime($t);
69 # get the correct day, if required
70 if (my ($h) = $iso =~ /^([012]\d)[0-5]\d(?:[0-5]\d)?$/) {
71 my ($d, $m, $y) = ($day, $month, $year);
73 if ($hour < 12 && $h - $hour >= 12) {
75 ($d, $m, $y) = _yesterday($d, $m, $y);
76 } elsif ($hour >= 12 && $hour - $h > 12) {
78 ($d, $m, $y) = _tomorrow($d, $m, $y);
81 $iso = sprintf("%04d%02d%02dT", $y, $m, $d) . $iso;
82 } elsif (my ($d) = $iso =~ /^(\d\d)T\d\d\d\d/) {
84 # get the correct month and year if it is a short date
86 $iso = sprintf("%04d%02d", $year, $month) . $iso;
88 my $days = _isleap($year) ? $ldays[$month-1] : $days[$month-1];
89 my ($y, $m) = ($year, $month);
91 if ($day - $d > $days / 2) {
100 if ($d - $day > $days / 2) {
109 $iso = sprintf("%04d%02d", $y, $m) . $iso;
113 return str2time($iso);
118 my ($d, $m, $y) = @_;
121 my $days = _isleap($y) ? $ldays[$month-1] : $days[$month-1];
138 my ($d, $m, $y) = @_;
148 $d = _isleap($y) ? $ldays[$m-1] : $days[$m-1];