7

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ramah
  • 455
  • 1
  • 8
  • 18
  • 1
    The referenced converter seems suspect. For example, `0,60` and `0,61` both give `1980/01/06 00:01:00`, but the first leap second after the GPS epoch wasn't until 1981/07/01. – jjlin Feb 14 '12 at 00:35
  • The converter really just converts between GPS time and TOW/WeekNo. It doesn't care for leap seconds. The column titles are wrong. – TJJ May 17 '19 at 00:53

2 Answers2

10

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;
}
user3360767
  • 868
  • 7
  • 18
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • yes .i find the error in my code..it was starting from 1980.1.1 – Ramah Jul 07 '11 at 09:39
  • 4
    Not a correction per se, but a note for future reference - the time signal from GPS satellites does not include [leap seconds](http://en.wikipedia.org/wiki/Leap_second), 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. – Phlucious Apr 07 '15 at 16:18
  • 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 ? – basilisk Nov 04 '19 at 14:58
  • @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. – Phlucious Nov 05 '19 at 21:05
1

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

John
  • 26
  • 2