* /trunk/src/gpstools/postgres/create_funcs.sql
[gpstools.git] / GPSTdate.pm
bloba837efd30962c4226a4868e10e1094909caf0ebc
1 package GPSTdate;
3 #=======================================================================
4 # $Id$
6 # Character set: UTF-8
7 # ©opyleft 2002– Øyvind A. Holm <sunny@sunbase.org>
8 # License: GNU General Public License, see end of file for legal stuff.
9 #=======================================================================
11 use strict;
12 use warnings;
14 BEGIN {
15 use Exporter ();
16 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
18 my $rcs_id = '$Id$';
19 push(@main::version_array, $rcs_id);
20 $VERSION = ($rcs_id =~ / (\d+) /, $1);
22 @ISA = qw(Exporter);
23 @EXPORT = qw(&sec_to_string &sec_to_readable);
24 %EXPORT_TAGS = ();
26 our @EXPORT_OK;
28 sub sec_to_string {
29 # Convert seconds since 1970 to "yyyy-mm-dd hh:mm:ss" with optional
30 # separator
31 # {{{
32 my ($Seconds, $Sep) = @_;
33 length($Seconds) || return(undef);
34 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
35 my $Secfrac = ($Seconds =~ /^([\-\d]*)(\.\d+)$/) ? 1.0*$2 : "";
36 $Secfrac =~ s/^0//;
38 defined($Sep) || ($Sep = " ");
39 my @TA = gmtime($Seconds);
40 my($DateString) = sprintf("%04u-%02u-%02u%s%02u:%02u:%02u%s",
41 $TA[5]+1900, $TA[4]+1, $TA[3], $Sep,
42 $TA[2], $TA[1], $TA[0], $Secfrac);
43 return($DateString);
44 # }}}
47 sub sec_to_readable {
48 # Convert seconds since 1970 to human-readable format (d:hh:mm:ss)
49 # {{{
50 my $Seconds = shift;
51 my ($Day, $Hour, $Min, $Sec) =
52 ( 0, 0, 0, 0);
54 length($Seconds) || ($Seconds = 0);
55 ($Seconds =~ /^(\d*)(\.\d+)?$/) || return(undef);
56 my $Secfrac = ($Seconds =~ /^(\d*)(\.\d+)$/) ? 1.0*$2 : "";
57 $Secfrac =~ s/^0//;
59 $Day = int($Seconds/86400);
60 $Seconds -= $Day * 86400;
62 $Hour = int($Seconds/3600);
63 $Seconds -= $Hour * 3600;
65 $Min = int($Seconds/60);
66 $Seconds -= $Min * 60;
68 $Sec = $Seconds;
70 return(sprintf("%u:%02u:%02u:%02u%s",
71 $Day, $Hour, $Min, $Sec, $Secfrac));
72 # }}}