From a253a7d3820e9fb58bca0c2633ba958778ad4c52 Mon Sep 17 00:00:00 2001 From: minima Date: Sat, 21 Jan 2006 00:37:12 +0000 Subject: [PATCH] make just the time the default. Fix IsoTime so it can handle it. --- perl/DXXml.pm | 2 +- perl/IsoTime.pm | 72 ++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/perl/DXXml.pm b/perl/DXXml.pm index c40e3be0..9d0a5969 100644 --- a/perl/DXXml.pm +++ b/perl/DXXml.pm @@ -190,7 +190,7 @@ sub toxml unless (exists $self->{'-xml'}) { $self->{o} ||= $main::mycall; - $self->{t} ||= IsoTime::dayminsec(); + $self->{t} ||= IsoTime::hourminsec(); $self->{id} ||= nextid(); my ($name) = (ref $self) =~ /::(\w+)$/; diff --git a/perl/IsoTime.pm b/perl/IsoTime.pm index 422f9912..0bcf7995 100644 --- a/perl/IsoTime.pm +++ b/perl/IsoTime.pm @@ -43,6 +43,17 @@ sub daymin return sprintf "%02dT%02d%02d", $day, $hour, $min; } +sub hourmin +{ + return sprintf "%02d%02d", $hour, $min; + +} + +sub hourminsec +{ + return sprintf "%02d%02d%02d", $hour, $min, $sec; +} + sub update { my $t = shift || time; @@ -54,9 +65,23 @@ sub update sub unixtime { my $iso = shift; - - # get the correct month and year if it is a short date - if (my ($d) = $iso =~ /^(\d\d)T\d\d\d\d/) { + + # get the correct day, if required + if (my ($h) = $iso =~ /^([012]\d)[0-5]\d(?:[0-5]\d)?$/) { + my ($d, $m, $y) = ($day, $month, $year); + if ($h != $hour) { + if ($hour < 12 && $h - $hour >= 12) { + # yesterday + ($d, $m, $y) = _yesterday($d, $m, $y); + } elsif ($hour >= 12 && $hour - $h > 12) { + # tomorrow + ($d, $m, $y) = _tomorrow($d, $m, $y); + } + } + $iso = sprintf("%04d%02d%02dT", $y, $m, $d) . $iso; + } elsif (my ($d) = $iso =~ /^(\d\d)T\d\d\d\d/) { + + # get the correct month and year if it is a short date if ($d == $day) { $iso = sprintf("%04d%02d", $year, $month) . $iso; } else { @@ -83,7 +108,46 @@ sub unixtime } $iso = sprintf("%04d%02d", $y, $m) . $iso; } - } + } + return str2time($iso); } + +sub _tomorrow +{ + my ($d, $m, $y) = @_; + + $d++; + my $days = _isleap($y) ? $ldays[$month-1] : $days[$month-1]; + if ($d > $days) { + $d = 1; + $m++; + if ($m > 12) { + $m = 1; + $y++; + } else { + $y = $year; + } + } + + return ($d, $m, $y); +} + +sub _yesterday +{ + my ($d, $m, $y) = @_; + + $d--; + if ($d <= 0) { + $m--; + $y = $year; + if ($m <= 0) { + $m = 12; + $y--; + } + $d = _isleap($y) ? $ldays[$m-1] : $days[$m-1]; + } + + return ($d, $m, $y); +} 1; -- 2.43.0