[time-nuts] GPS seconds conversion on an Arduino

Tom Van Baak tvb at LeapSecond.com
Sun May 14 11:51:31 EDT 2017

Mark, Ben, Paul,

32-bit integers is sufficient. Use days instead of seconds. Use MJD instead of JD. Here's an example:

Step 1, convert calendar date to MJD, and then to GPS day:

// Convert year, month (1-12), and day (1-31) to Modified Julian Day (MJD).
// Adapted from sci.astro FAQ (valid for Gregorian dates from 17-Nov-1858).
int32 ymd_to_mjd (int32 yyyy, int32 mm, int32 dd)
        367 * yyyy - 7 * (yyyy + (mm + 9) / 12) / 4
        - 3 * ((yyyy + (mm - 9) / 7) / 100 + 1) / 4
        + 275 * mm / 9 + dd + 1721028 - 2400000;

#define MJD_TO_GPS_DAY(mjd) ((mjd) + 44244)

Step 2, apply leap seconds since 1980 using table look-up.


----- Original Message ----- 
From: "Mark Sims" <holrum at hotmail.com>
To: <time-nuts at febo.com>
Sent: Saturday, May 13, 2017 6:58 PM
Subject: [time-nuts] GPS seconds conversion on an Arduino

> Converting GPS seconds to Gregorian date/time on the Arduino will be an arduous task.  You take GPS seconds and add it to the GPS starring epoch to get a Julian date.  Then add in the number of leap seconds as a fraction of a day to get UTC and possibly add in a time zone offset for local time.  Don't forget to do daylight savings time conversion...  Then convert the result to Gregorian date/time for display.  
> The problem is the Arduino floating point library is single precision only and does not have the resolution needed to handle the numbers involved.  Doing it with integer arithmetic (long longs) opens up a whole new can of worms.

More information about the time-nuts mailing list