The UTC time is formatted like this
"Tue 24 Dec 2013 5:53 AM UTC"
Is there a built-in function to convert it to milliseconds or I have to write my own scripts for it?
The UTC time is formatted like this
"Tue 24 Dec 2013 5:53 AM UTC"
Is there a built-in function to convert it to milliseconds or I have to write my own scripts for it?
The obvious answer, using the time
module with time.strptime()
doing the parsing:
import time
timetup = time.strptime(inputstring, '%a %d %b %Y %I:%M %p %Z')
milliseconds = time.mktime(timetup) * 1000
This does require that you don't set the locale in Python, leaving it at the default C
locale, or only set it to a English locale. The %a
and %b
formatters only look for strings that match the configured locale, and only the C
and English locales match the input strings.
Demo:
>>> import time
>>> inputstring = "Tue 24 Dec 2013 5:53 AM UTC"
>>> timetup = time.strptime(inputstring, '%a %d %b %Y %I:%M %p %Z')
>>> time.mktime(timetup) * 1000
1387864380000.0
This does assume that by milliseconds you meant milliseconds since the UNIX epoch.