gpst: print_gpx() truncates values instead of round in pgwtab and pgwupd
[gpstools.git] / GPSTdate.pm
blobab8fa566c9ceb14b8cdf4734bcd0b0dc2b74fdae
1 package GPSTdate;
3 #=======================================================================
4 # GPSTdate.pm
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 (@ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
19 @ISA = qw(Exporter);
20 @EXPORT = qw(&sec_to_string &sec_to_readable);
21 %EXPORT_TAGS = ();
23 our @EXPORT_OK;
25 sub sec_to_string {
26 # Convert seconds since 1970 to "yyyy-mm-dd hh:mm:ss" with optional
27 # separator
28 # {{{
29 my ($Seconds, $Sep) = @_;
30 length($Seconds) || return(undef);
31 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
32 my $Secfrac = ($Seconds =~ /^([\-\d]*)(\.\d+)$/) ? 1.0*$2 : "";
33 $Secfrac =~ s/^0//;
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);
40 return($DateString);
41 # }}}
42 } # sec_to_string()
44 sub sec_to_readable {
45 # Convert seconds since 1970 to human-readable format (d:hh:mm:ss)
46 # {{{
47 my $Seconds = shift;
48 my ($Day, $Hour, $Min, $Sec) =
49 ( 0, 0, 0, 0);
51 length($Seconds) || ($Seconds = 0);
52 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
53 my $Secfrac = ($Seconds =~ /^(\d*)(\.\d+)$/) ? 1.0*$2 : "";
54 $Secfrac =~ s/^0//;
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;
65 $Sec = $Seconds;
67 return(sprintf("%u:%02u:%02u:%02u%s",
68 $Day, $Hour, $Min, $Sec, $Secfrac));
69 # }}}
70 } # sec_to_readable()