convert from GPS week number,time of week to datetime

27,749

Solution 1

If you know the DateTime that represents the week, just call AddSeconds to find the DateTime you need.

According to the calculator you linked to above, week 1643, 377505 should equate to 2011/07/07 07:51:44, not 10:51:44 (maybe it is a time zone offset?) Anyway, the following snippet will give you what you the same result as the calculator in the link when GMT is selected - for different timezones, you'll have to apply your own offsets.

DateTime GetFromGps(int weeknumber, double seconds)
{
    DateTime datum = new DateTime(1980,1,6,0,0,0);
    DateTime week = datum.AddDays(weeknumber * 7);
    DateTime time = week.AddSeconds(seconds);
    return time;
}

Solution 2

This works in VB6

Dim dtt As Date
dtt = "6 / 1 / 1980"
dtt = DateAdd("d", (1837 * 7), dtt)
dtt = DateAdd("s", 129414, dtt)

1837 is gps week 129414 is seconds of week

Share:
27,749
Ramah
Author by

Ramah

software developer working for Inotek systems in smart village ,Cairo ,Egypt

Updated on January 17, 2020

Comments

  • Ramah
    Ramah over 4 years

    I have a GPS device that sends some objects including a GPS time to my server.
    It's coming in format of week number,seconds into week.

    I want to convert that to a datetime format. I found some codes for that but all I found is how to convert week number to date, not including the second into week.

    I found this web page, which is doing that but I can't find out how to do that in c# windows app code.

    Sample data:

    GPS Week number 1643
    GPS second into week 377505
    

    That should be 2011/07/07 10:51:44.

  • Ramah
    Ramah almost 13 years
    yes .i find the error in my code..it was starting from 1980.1.1
  • Phlucious
    Phlucious about 9 years
    Not a correction per se, but a note for future reference - the time signal from GPS satellites does not include leap seconds, which means that your time from this calculation will be a few seconds off from standard UTC. Right now that number is 16 seconds, but it'll be 17 seconds in June 2015.
  • basilisk
    basilisk over 4 years
    why does the time start at (1980, 1, 6) ?? @Phlucious: so should I just add leap seconds to that results to find the final UTC ?
  • Phlucious
    Phlucious over 4 years
    @basilisk Well I know they picked 1980-01-06 instead of 1980-01-01 because that was a Sunday and GPS time is tracked by week# and seconds-of-the-week, starting at midnight 0:00:00 Sunday morning. But why 1980? I assume it's around the advent of Navstar, but I'm not sure. Re: adding leap seconds - yes, as long as it's after summer 2017 you'll add 18 seconds to GPST to get UTC. Refer to the wiki page for a table of leap seconds and dates they were added. They keep it very current.