user375566
user375566

Reputation:

What is this time format, and how do I convert it to an epoch?

I have data with timestamps in 2 formats, where 3F536DDA -> 1378397085768000 and 3F52C3C3 -> 1378353476238000. I can not seem to figure out what format 3F536DDA is in, nor how to convert it to the epoch in question. Any help would be appreciated!

Update

I have found this:

8-digit hexadecimal representation of the time of fix in seconds since January 6, 1980 at time 00:00:00 GMT.

Which appears to be GPS Time.

I may be missing something from the conversion here, where I just subtract the difference between Jan 6 1980 and Jan 1 1970...

time = (1378397085 - 315964800)

console.log '3F536DDA' # What I expected
console.log time.toString(16) # What I get: 3F536D1D 

Upvotes: 1

Views: 1381

Answers (2)

David Calhoun
David Calhoun

Reputation: 8581

You would think it would just be as easy as diffing between the GPS epoch (midnight January 6, 1980) and the Unix epoch (midnight January 1, 1970). That answer will get you close but not exact, because GPS time doesn't take into account leap seconds. There will be drift in the calculation for every leap second added since 1980.

I wrote a helper library that you can use here: https://github.com/davidcalhoun/gps-time.js

If you're interested, the algorithm is essentially this: 1) convert from GPS to Unix epoch 2) for any given date, determine the number of leap seconds that have been added 3) make time adjustment based on how many leap seconds have passed

For this to work with any arbitrary time, including times in the past, this basically requires iterating through lookup table with all the exact times of leap seconds.

Importantly, it also means that to prevent drift, the code needs to be updated every single time a leap second is added in the future. Luckily it's just as easy as adding the leap second to the lookup table!

Upvotes: 0

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241563

Well, these just look like hexadecimal numbers, which would be seconds since 1/1/1970 UTC.

3F536DDA hex == 1062432218 dec == Mon, 01 Sep 2003 16:03:38 GMT
3F52C3C3 hex == 1062388675 dec == Mon, 01 Sep 2003 03:57:55 GMT

The other numbers look like decimal microseconds since 1/1/1970 UTC, but they aren't equal to those other numbers:

1378397085768000 == Thu, 05 Sep 2013 16:04:45 GMT
1378353476238000 == Thu, 05 Sep 2013 03:57:56 GMT

If those aren't the dates you had in mind, then there is some other epoch involved. For example, if they originated in Microsoft Windows, they will certainly be different.

Where did you get them from? That knowledge will help in their interpretation.

UPDATE

Since you said they are based on Jan 6 1980 epoch, then you can do something like this:

var d = new Date(Date.UTC(1980,0,6) + parseInt('3F536DDA', 16) * 1000);
d.toISOString();  // 2013-09-05T16:03:38.000Z

Or better yet, using moment.js

var m = moment.utc("1980-01-06").add('seconds', parseInt('3F536DDA', 16));
m.toISOString();  // 2013-09-05T16:03:38.000Z

Upvotes: 4

Related Questions