3

i am tring to convert this unix timestamp 1491613677888 to readable date. found here (stackoverflow) that python script:

import datetime
print(
    datetime.datetime.fromtimestamp(
    int("1284101485")
    ).strftime('%Y-%m-%d %H:%M:%S')
)

but when i put my timestamp there, i got that error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

now i see that the timestamp that i am using is 3 chars longer. i checked it on this link: http://www.unixtimestamp.com/index.php

and saw that its get the time out of it. how can i do it using python? (i am using python 3.4)

5
  • 1
    Can't reproduce (python2.7 + 3) Commented Apr 10, 2017 at 8:42
  • 2
    Where did you get the final 888 in your timestamp? The first part 1491613677 is 2017-04-08T01:07:57+00:00, which is probably what you wanted. With it, your code works perfectly. Commented Apr 10, 2017 at 8:47
  • 2
    Did you get it from some function that outputs it in milliseconds? Commented Apr 10, 2017 at 8:48
  • 2
    When I use int("1284101485888") on Python 2 I get ValueError: year is out of range, on Python 3.6 I get 42661-07-22 17:11:28. Commented Apr 10, 2017 at 8:49
  • regarding to @ThierryLathuille comment. yes, i need the milliseconds. how can i use it in python 3.4? Commented Apr 10, 2017 at 13:30

2 Answers 2

7

Your timestamp is not the 'classical' Unix timestamp (number of seconds since Jan 1st, 1970), as it is expressed in milliseconds.

You can translate it like this:

import datetime

timestamp_with_ms = 1491613677888

# We separate the 'ordinary' timestamp and the milliseconds
timestamp, ms = divmod(timestamp_with_ms, 1000)
#1491613677 888

# We create the datetime from the timestamp, we must add the 
# milliseconds separately
dt = datetime.datetime.fromtimestamp(timestamp) + datetime.timedelta(milliseconds=ms)


formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')

print(formatted_time)
# 2017-04-08 03:07:57.888

Edit: I hadn't noticed that fromtimestamp accepts a float. So, we can simply do:

import datetime
timestamp_with_ms = 1491613677888

dt = datetime.datetime.fromtimestamp(timestamp_with_ms / 1000)

formatted_time = dt.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]
# With Python 3.6, you could use:
# formatted_time = dt.isoformat(sep=' ', timespec='milliseconds')

print(formatted_time)
# 2017-04-08 03:07:57.888
Sign up to request clarification or add additional context in comments.

Comments

2

Your timestamp is 3 characters longer AND is a standard unix timestamp? That would mean your timestamp is at least 40,000 years into the future from today. Otherwise, the last 3 characters may represent something else, like milliseconds but that doesn't explain the error you're seeing.

If they are the milliseconds, and seeing how you're not using them in the format string, I see no harm in simply stripping them.

standard_unix_ts = int("1284101485000"[:-3])

EDIT Taking into account the comment of @cdarke, I'd suggest this instead:

standard_unix_ts = int("1284101485000"[:10])

EDIT 2 Following Gils comment

import datetime

not_unix_ts = "1284101485088"
unix_ts, milliseconds = not_unix_ts[:10], not_unix_ts[10:]
dt = datetime.datetime.fromtimestamp(float(unix_ts))
FORMAT_STRING = '%Y-%m-%d %H:%M:%S'
print("%s and %s milliseconds" % (dt.strftime(FORMAT_STRING), milliseconds))

4 Comments

Just a thought, it might be safer to take the first 10 characters rather than stripping the last 3, in case there is a timestamp of the correct length.
Very valid comment, I entirely agree.
in the link that I added, I saw milliseconds - and i need them. that timestamp is part of a key. I need all of it. so how can i get the milliseconds too in python 3.4?
simply strip them from the string, giving you a real time stamp and the last 3 characters represent your milliseconds. to re-iterate: unix timestamps do not include milliseconds so it's understandable that libraries have problems parsing them. I've my answer to better demonstrate this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.