pettinato
pettinato

Reputation: 1542

Convert Human Readable Timestamp to Milliseconds from the Epoch in Python

I have a timestamp like "2016-05-24 11:30 PST".

How do I get the milliseconds since the epoch?

Upvotes: 0

Views: 1278

Answers (2)

unutbu
unutbu

Reputation: 879849

Consider the following:

import datetime as DT
import dateutil.parser as DP
import pytz

t = "2016-05-24 11:30 PST"
date = DP.parse(t)
DATE_FORMAT = '%Y-%m-%d %H:%M:%S %Z%z'

wrong_tzone = pytz.timezone('PST8PDT')
wrong_date = wrong_tzone.localize(date)
print('{:16}: {}'.format(str(wrong_tzone), wrong_date.strftime(DATE_FORMAT)))
# PST8PDT         : 2016-05-24 11:30:00 PDT-0700

right_tzone = pytz.timezone('Pacific/Pitcairn')
right_date = right_tzone.localize(date)
print('{:16}: {}'.format(str(right_tzone), right_date.strftime(DATE_FORMAT)))
# Pacific/Pitcairn: 2016-05-24 11:30:00 PST-0800

This shows that the local time 2016-05-24 11:30 has the abbreviation PST in the Pacific/Pitcairn timezone, but has the abbreviation PDT in PST8PDT. So it doesn't make sense to use the PST8PDT timezone to localize 2016-05-24 11:30.

Choosing the right timezone influences the timestamp:

In [274]: wrong_date.timestamp()
Out[274]: 1464114600.0

In [275]: right_date.timestamp()
Out[275]: 1464118200.0

(Calling d.timestamp(), by the way, is equivalent to (d - epoch).total_seconds()). The timestamp returns a quantity in seconds.


The Pacific/Pitcairn timezone was found using

import datetime as DT
import dateutil.parser as DP
import pytz
import collections

t = "2016-05-24 11:30 PST"
date = DP.parse(t)

tzones = collections.defaultdict(set)
for name in pytz.all_timezones:
    tzone = pytz.timezone(name)
    tzabbrev = tzone.normalize(tzone.localize(date)).tzname()
    tzones[tzabbrev].add(name)
print(tzones['PST'])
# {'Pacific/Pitcairn'}

We are lucky that there is only one timezone where the localtime 2016-05-24 11:30 uses the abbreviation PST. In general, many timezones could be associated with the same abbreviation. For example, 2016-05-24 11:30 CST could refer to any of these timezones:

In [283]: tzones['CST']
Out[283]: 
{'America/Belize',
 'America/Costa_Rica',
 'America/El_Salvador',
 'America/Guatemala',
 'America/Managua',
 'America/Regina',
 'America/Swift_Current',
 'America/Tegucigalpa',
 'Asia/Chongqing',
 'Asia/Chungking',
 'Asia/Harbin',
 'Asia/Macao',
 'Asia/Macau',
 'Asia/Shanghai',
 'Asia/Taipei',
 'Canada/East-Saskatchewan',
 'Canada/Saskatchewan',
 'PRC',
 'ROC'}

2016-05-24 11:30 CST is ambiguous. And so in general, the timestamp you are seeking may not have a unique solution (or any solution).

Upvotes: 2

pettinato
pettinato

Reputation: 1542

import datetime
import pytz
import dateutil

t = "2016-05-24 11:30 PST"

#Convert the timestamp to a datetime object with timezone information
d = pytz.timezone("PST8PDT").localize(dateutil.parser.parse(t))

#In order to subtract a datetime object with timezone information is required.
#Build the epoch datetime object with a timezone
epoch = pytz.timezone("UTC").localize(datetime.datetime.utcfromtimestamp(0))

#Subtract the epoch from the human enocoded datetime object 
(d - epoch).total_seconds() * 1000

Upvotes: 0

Related Questions