Search code examples
linkedin-api

What format are the timestamps returned by the LinkedIn API?


LinkedIn's API returns the following value:

[creationTimestamp] => 1407247548000

It looks similar to a UNIX timestamp, but there are three "extra" zeros at the end. What format is this in, and how can I decode it?


Solution

  • It is a timestamp in milliseconds. Handling this is language dependent. Some languages may expect a timestamp in milliseconds, while others may expect it in seconds. Python 3, for example, expects seconds, but also handles microseconds (1000 milliseconds).

    from datetime import datetime
    ts = 1407247548124
    dt = datetime.utcfromtimestamp(ts / 1000)
    print(dt)  # datetime(2014, 8, 5, 14, 5, 48, 124000)
    

    Python 2 doesn't handle milliseconds directly (it ignores the fractional part), so you need to split the milliseconds out separately.

    from datetime import datetime
    ts = 14072475481234
    secs, millis = divmod(ts, 1000)
    dt = datetime.utcfromtimestamp(secs).replace(microsecond=millis * 1000)
    print(dt)  # datetime(2014, 8, 5, 14, 5, 48, 124000)