7

i would like to know how to convert datetime Wed Apr 24 19:25:06 2013 GMT into Linux timestamp 1366831506000 (13 digits)] and reverse using python.

for example:

Wed Apr 24 19:25:06 2013 GMT

to

1366831506000

and from

1366831506000

to

Wed Apr 24 19:25:06 2013 GMT
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
sanjayan ravi
  • 143
  • 1
  • 1
  • 8
  • `import time` `str(time.strftime("%a %b %d %H:%M:%S %Y GMT", time.gmtime()))` to get `Wed Apr 24 19:25:06 2013 GMT` from the program directly – Santhosh Apr 11 '18 at 06:52

1 Answers1

12

From string to timestamp, use time.strptime(), passing the resulting struct_time tuple to time.mktime(); your timestamp uses milliseconds, not the UNIX seconds-as-floating-point value, so you need to multiply by 1000:

import time

datestr = "Wed Apr 24 19:25:06 2013 GMT"
time.mktime(time.strptime(datestr, "%a %b %d %H:%M:%S %Y %Z")) * 1000

In the other direction, use time.strptime(), passing in a struct_time tuple created by time.gmtime(), dividing the timestamp by 1000 first:

timestamp = 1366831506000
time.strftime("%a %d %b %Y %H:%M:%S GMT", time.gmtime(timestamp / 1000.0))

Demo:

>>> datestr = "Wed Apr 24 19:25:06 2013 GMT"
>>> time.mktime(time.strptime(datestr, "%a %b %d %H:%M:%S %Y %Z")) * 1000
1366827906000.0
>>> timestamp = 1366831506000
>>> time.strftime("%a %d %b %Y %H:%M:%S GMT", time.gmtime(timestamp / 1000.0))
'Wed 24 Apr 2013 19:25:06 GMT'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • thank you so much for your help Martijn Pieters – sanjayan ravi Apr 25 '13 at 07:55
  • hi i am really sorry i was not clear with my question before, i am trying to convert 'datestr = "Wed Apr 24 19:25:06 2013 +7200"' [+7200 = offset in seconds for GMT+2 (Europe/Berlin Time Zone)] into linux timestamp but this didn't work with the demo you have given above – sanjayan ravi Apr 25 '13 at 09:37
  • @sanjayanravi: No, to handle timezones you need an external library. Use [`python-dateutil`](https://pypi.python.org/pypi/python-dateutil). – Martijn Pieters Apr 25 '13 at 09:39
  • @sanjayanravi: `+7200` is a *weird* offset, expressed in seconds instead of hours and minutes. `+0200` would be the correct UTC offset. – Martijn Pieters Apr 25 '13 at 09:55
  • python program is not accepting hours and minutes +0200 for GMT+2 it is accepting only the Epoch (reference date) format according to the following link http://www.epochconverter.com/epoch/timezones.php?epoch=1366888990 – sanjayan ravi Apr 25 '13 at 11:31
  • You could also parse the date minus the `+7200` offset with `datetime.datetime.strptime`, parse the `+7200` part manually into an integer, then use [`dateutil.tz.tzoffset`](http://labix.org/python-dateutil#head-8bf499d888b70bc300c6c8820dc123326197c00f) to turn that into a timezone. *Then* turn the `datetime.datetime` object into a timestamp. – Martijn Pieters Apr 25 '13 at 14:23
  • @sanjayanravi: The other way around, timestamp to datetime string with timezone is not possible without separately specifying what timezone should be used. – Martijn Pieters Apr 25 '13 at 14:24
  • Will it assume the date is today if not given in the input string? – Luke Taylor Dec 28 '15 at 21:38
  • Never mind, it assumes Jan 1 1900 on my raspberry pi. – Luke Taylor Dec 28 '15 at 21:47