convert from GPS week number,time of week to datetime

Ramah picture Ramah · Jul 7, 2011 · Viewed 27.8k times · Source

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.

Answer

ZombieSheep picture ZombieSheep · Jul 7, 2011

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;
}