Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Converting GPS seconds to readable time&date

by flamey (Scribe)
on Jan 11, 2009 at 14:23 UTC ( [id://735502]=perlquestion: print w/replies, xml ) Need Help??

flamey has asked for the wisdom of the Perl Monks concerning the following question:

How can I convert GPS time in seconds to something readable, say UTC time? For example I have GPS time in seconds = 915725119. I use http://www.mapshots.com/tools/gps_time.asp and get "1/7/1999 4:05:19 PM" -- the time matches what I see on my device (i can't confirm date). I tried Time::GPS and DateTime::Format::Epoch::MJD as follows:
use Time::GPS qw(gps_instant_to_mjd gps_mjd_to_instant); use Math::BigRat; my $x = Math::BigRat->new('915717017'); my $mjd = gps_instant_to_mjd($x); print "$mjd\n"; # 2021871299/43200 use DateTime::Format::Epoch::MJD; my $dt = DateTime::Format::Epoch::MJD->parse_datetime( $mjd ); print $dt; # 1987-01-06T00:00:00
Obviously something there is wrong, but what? What's the best way to do such conversion, anyway? Many thanks in advance.

Replies are listed 'Best First'.
Re: Converting GPS seconds to readable time&date
by derby (Abbot) on Jan 11, 2009 at 14:46 UTC

    gmtime in scalar context should do the trick:

    perl -e 'print scalar( gmtime( 915725119 ) ),"\n"'
    prints
    Thu Jan 7 16:05:19 1999

    -derby

    Update:There's a lot of talk about which CPAN date module to use but for whatever reason, I always fall back on Date::Manip. With that module, you have tons of control over lots of most aspects of time:

    perl -MDate::Manip -e 'print UnixDate( ParseDate(scalar(gmtime(9157251 +19))), "%m/%d/%Y %i:%M:%S %p"), "\n"'
    (there's probably tons of ways to simplify that). Or if you just need a pretty string, you could always use strftime:
    #!/usr/bin/perl use POSIX qw( strftime ); print strftime( "%m/%d/%Y %l:%M:%S %p", gmtime( 915725119 ) ), "\n";
    (format strings to strftime tend to be platform dependent)

      thnaks for your help, derby. now i don't understand why they called it GPS time in the serial output :-/ , but that's a different issue.. thanks

        Yikes! Found this at leapsecond.com

        GPS, Global Positioning System time, is the atomic time scale implemen +ted by the atomic clocks in the GPS ground control stations and the G +PS satellites themselves. GPS time was zero at 0h 6-Jan-1980 and sinc +e it is not perturbed by leap seconds GPS is now ahead of UTC by 15 s +econds.
        I'm not sure if your hardware is compensating for this diff and/or if it's just outputting standard UTC and saying it's GPS. You're better off listening to someone more GPS aware than I am (that is if you're really worried about that 15 second gap).

        -derby

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://735502]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 01:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found