* /trunk/vg
[gpstools.git] / GPSTdate.pm
bloba9f9cd1e650b003855c538cd8a4573369bf13c21
1 package GPSTdate;
3 #=======================================================================
4 # $Id$
5 # File ID: 624dab98-fafa-11dd-831c-000475e441b9
7 # Character set: UTF-8
8 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
9 # License: GNU General Public License, see end of file for legal stuff.
10 #=======================================================================
12 use strict;
13 use warnings;
15 BEGIN {
16 use Exporter ();
17 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
19 my $rcs_id = '$Id$';
20 push(@main::version_array, $rcs_id);
21 $VERSION = ($rcs_id =~ / (\d+) /, $1);
23 @ISA = qw(Exporter);
24 @EXPORT = qw(&sec_to_string &sec_to_readable);
25 %EXPORT_TAGS = ();
27 our @EXPORT_OK;
29 sub sec_to_string {
30 # Convert seconds since 1970 to "yyyy-mm-dd hh:mm:ss" with optional
31 # separator
32 # {{{
33 my ($Seconds, $Sep) = @_;
34 length($Seconds) || return(undef);
35 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
36 my $Secfrac = ($Seconds =~ /^([\-\d]*)(\.\d+)$/) ? 1.0*$2 : "";
37 $Secfrac =~ s/^0//;
39 defined($Sep) || ($Sep = " ");
40 my @TA = gmtime($Seconds);
41 my($DateString) = sprintf("%04u-%02u-%02u%s%02u:%02u:%02u%s",
42 $TA[5]+1900, $TA[4]+1, $TA[3], $Sep,
43 $TA[2], $TA[1], $TA[0], $Secfrac);
44 return($DateString);
45 # }}}
48 sub sec_to_readable {
49 # Convert seconds since 1970 to human-readable format (d:hh:mm:ss)
50 # {{{
51 my $Seconds = shift;
52 my ($Day, $Hour, $Min, $Sec) =
53 ( 0, 0, 0, 0);
55 length($Seconds) || ($Seconds = 0);
56 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
57 my $Secfrac = ($Seconds =~ /^(\d*)(\.\d+)$/) ? 1.0*$2 : "";
58 $Secfrac =~ s/^0//;
60 $Day = int($Seconds/86400);
61 $Seconds -= $Day * 86400;
63 $Hour = int($Seconds/3600);
64 $Seconds -= $Hour * 3600;
66 $Min = int($Seconds/60);
67 $Seconds -= $Min * 60;
69 $Sec = $Seconds;
71 return(sprintf("%u:%02u:%02u:%02u%s",
72 $Day, $Hour, $Min, $Sec, $Secfrac));
73 # }}}