8

在下面的代码中,我正在计算现在的纪元和当前纪元的开始。

import time
import pytz
from datetime import datetime

tz1 = pytz.timezone('CST6CDT')
utc = pytz.timezone('UTC')
now = pytz.UTC.localize(datetime.utcnow())
now_tz = now.astimezone(tz1)
print now_tz
print now_tz.strftime('%s')

begin_day = now_tz.replace(hour=0, minute=0, second=0)
print begin_day

print begin_day.strftime('%s')

打印语句:

2012-08-28 13:52:21.595718-05:00
1346187141
2012-08-28 00:00:00.595718-05:00
1346137200

使用 CDT 时区将纪元转换为时间戳:1346187141 - 2012 年 8 月 28 日 15:52:21、1346137200 - 2012 年 8 月 28 日 02:00:00

我希望第二个时代是一天的开始,但现在是凌晨 2 点。转换为纪元时,它看起来仍在使用本地时区 PST。

我究竟做错了什么 ?或者这可以用不同的方式完成吗?

谢谢!

4

3 回答 3

31

要将带有时区的日期时间转换为纪元(POSIX 时间戳):

from datetime import datetime
import pytz

tz = pytz.timezone('CST6CDT')

# a datetime with timezone
dt_with_tz = tz.localize(datetime(2012, 8, 28, 19, 33, 50), is_dst=None)

# get timestamp
ts = (dt_with_tz - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
# -> 1346200430.0

这是Python 3datetime.timestamp中时区感知对象的方法实现方式。datetime

要获得“现在时代”:

from datetime import datetime

now_epoch = (datetime.utcnow() - datetime(1970, 1, 1)).total_seconds()

或者(假设time使用 POSIX 纪元):

import time

now_epoch = time.time()

获取“当前时代的开始”更为复杂,因为不同时区的当前日期可能不同:

from datetime import datetime, time
import pytz

tz = pytz.timezone('CST6CDT')

# get current date in given timezone
today = datetime.now(tz).date()
# -> datetime.date(2013, 6, 22)

# get beginning of current day in given timezone as a datetime with timezone
midnight = tz.localize(datetime.combine(today, time(0, 0)), is_dst=None)
# -> datetime.datetime(2013, 6, 22, 0, 0, tzinfo=<DstTzInfo 'CST6CDT'...>)

# get timestamp
ts = (midnight - datetime(1970, 1, 1, tzinfo=pytz.utc)).total_seconds()
# -> 1371877200.0 

请参阅如何获取给定时区的“午夜”UTC 时间?.

假设UTC日期获得“当前时代的开始”:

from datetime import datetime, date

# get current date in UTC
utc_date = datetime.utcnow().date()
# -> datetime.date(2013, 6, 23)

# get timestamp
ts = (utc_date - date(1970, 1, 1)).days * 86400
# -> 1371945600

请参阅在 Python 中将 datetime.date/datetime.datetime 转换为 UTC 时间戳

于 2013-06-23T02:22:41.347 回答
7

注意:我的回答完全错误。(我想删除它,但在删除接受标志之前我无法这样做。)

请参阅JFSebastian 的回答

下面的代码演示了now_tz我们的两种方法产生不同结果的值。

import calendar
import pytz
import datetime as dt

tz1 = pytz.timezone('US/Eastern')
utc = pytz.timezone('UTC')
now = utc.localize(dt.datetime(2002, 10, 28), is_dst=None)
now_tz = now.astimezone(tz1)
now_epoch = calendar.timegm(now_tz.utctimetuple())
begin_day = tz1.normalize(now_tz.replace(hour=0, minute=0, second=0))

midnight = tz1.localize(dt.datetime.combine(now_tz, dt.time(0, 0)), is_dst=None)
if begin_day != midnight:
    print(begin_day)
    # 2002-10-27 01:00:00-04:00  # my result -- is not midnight
    print(midnight)
    # 2002-10-27 00:00:00-04:00  # J.F.Sebastian's result is correct

(原始答案已编辑)

于 2012-08-28T19:42:22.853 回答
0

the latest release of simple-date (version 0.2 on pypi) will manage the details for you:

>>> from simpledate import *
>>> now_utc = SimpleDate(tz='UTC')
>>> now_tz = now_utc.convert(tz='CST6CDT')
>>> begin_day = now_tz.replace(hour=0, minute=0, second=0, microsecond=0)
>>> now_utc.timestamp
1371950295.777453
>>> now_tz.timestamp
1371950295.777453
>>> begin_day.timestamp
1371877200.0

we can go backwards to check the timestamps (although it's clear above that switching timezone didn't change the epoch, while moving to start of day did):

>>> SimpleDate(1371877200.0, tz='CST6CDT')
SimpleDate('2013-06-22 00:00:00.000000 CDT', tz='CST6CDT')
>>> SimpleDate(1371877200.0, tz='UTC')
SimpleDate('2013-06-22 05:00:00.000000 UTC')
于 2013-06-23T01:21:04.113 回答