Skip to content

Instantly share code, notes, and snippets.

@brentmaxwell
Created April 1, 2015 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brentmaxwell/60c3b3896212304aab15 to your computer and use it in GitHub Desktop.
Save brentmaxwell/60c3b3896212304aab15 to your computer and use it in GitHub Desktop.
Extensions for GPS time
namespace System
{
public static class GpsTime
{
public static int ToGpsTimeWeekNumber(this DateTime datetime)
{
DateTime datum = new DateTime(1980, 1, 6, 0, 0, 0);
TimeSpan difference = datetime.Subtract(datum);
return (int)(difference.TotalDays / 7);
}
public static int ToGpsTimeSeconds(this DateTime datetime)
{
DateTime datum = new DateTime(1980, 1, 6, 0, 0, 0);
DateTime secondsdiff = datetime.AddDays(datetime.ToGpsTimeWeekNumber() * -7);
TimeSpan difference = secondsdiff.Subtract(datum);
return (int)difference.TotalSeconds;
}
public static DateTime FromGpsTime(int weeknumber, int seconds)
{
return new DateTime(1980, 1, 6, 0, 0, 0).AddDays(weeknumber * 7).AddSeconds(seconds);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment