3 #=======================================================================
5 # File ID: 624dab98-fafa-11dd-831c-000475e441b9
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License, see end of file for legal stuff.
10 #=======================================================================
17 our (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
20 @EXPORT = qw(&sec_to_string &sec_to_readable);
26 # Convert seconds since 1970 to "yyyy-mm-dd hh:mm:ss" with optional
29 my ($Seconds, $Sep) = @_;
30 length($Seconds) || return(undef);
31 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
32 my $Secfrac = ($Seconds =~ /^([\-\d]*)(\.\d+)$/) ?
1.0*$2 : "";
35 defined($Sep) || ($Sep = " ");
36 my @TA = gmtime($Seconds);
37 my($DateString) = sprintf("%04u-%02u-%02u%s%02u:%02u:%02u%s",
38 $TA[5]+1900, $TA[4]+1, $TA[3], $Sep,
39 $TA[2], $TA[1], $TA[0], $Secfrac);
45 # Convert seconds since 1970 to human-readable format (d:hh:mm:ss)
48 my ($Day, $Hour, $Min, $Sec) =
51 length($Seconds) || ($Seconds = 0);
52 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
53 my $Secfrac = ($Seconds =~ /^(\d*)(\.\d+)$/) ?
1.0*$2 : "";
56 $Day = int($Seconds/86400);
57 $Seconds -= $Day * 86400;
59 $Hour = int($Seconds/3600);
60 $Seconds -= $Hour * 3600;
62 $Min = int($Seconds/60);
63 $Seconds -= $Min * 60;
67 return(sprintf("%u:%02u:%02u:%02u%s",
68 $Day, $Hour, $Min, $Sec, $Secfrac));