python -带有时区到历元的时间

时间:2022-01-19 15:45:32

In the code below, I am calculating now epoch and beginning of current day epoch.

在下面的代码中,我正在计算现在的时代和开始的时代。

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')

print statements:

打印语句:

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

Converting epochs to timestamp with CDT timezone: 1346187141 - Aug 28 2012 15:52:21, 1346137200 - Aug 28 2012 02:00:00

CDT时区转换时间戳:1346187141—2012年8月28日15:52:21,1346137200—2012年8月28日02:00:00

I'd like the second epoch to be beginning of the day but it's 2 am. It looks like it is still using local timezone PST when converting to epoch.

我希望第二个纪元是一天的开始,但现在是凌晨2点。在转换到历元时,它似乎还在使用本地时区PST。

What am I doing wrong ? or can this be done a different way?

我做错了什么?或者可以用不同的方式来做?

Thanks!

谢谢!

3 个解决方案

#1


6  

NOTE: My answer is flat-out wrong. (I'd like to delete it, but am unable to do so until the accept flag is removed.)

注意:我的答案是完全错误的。(我想删除它,但在删除accept标志之前,我无法删除它。)

Please see J.F.Sebastian's answer.

请参考书籍赛巴斯蒂安的答案。

Here is code demonstrating a value of now_tz for which our two methods produce different results.

下面的代码演示了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

(Original answer redacted)

(原答案修订)

#2


22  

To convert a datetime with timezone to epoch (POSIX timestamp):

将带时区的日期时间转换为历元(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

It is how datetime.timestamp method is implemented for timezone-aware datetime objects in Python 3.

它是datetime。在Python 3中,时间戳方法用于支持时间区域的datetime对象。

To get "now epoch":

“现在时代”:

from datetime import datetime

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

Or (assuming time uses POSIX epoch):

或(假设时间使用POSIX纪元):

import time

now_epoch = time.time()

Getting "beginning of current day epoch" is more complex because current day may be different in different timezones:

“当前时代的开始”更为复杂,因为在不同的时区,当前的日子可能会有所不同:

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 

See How do I get the UTC time of “midnight” for a given timezone?.

看看如何获得给定时区的“午夜”UTC时间?

To get "beginning of current day epoch" assuming UTC date:

取“当前日开始”假设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

See Converting datetime.date/datetime.datetime to UTC timestamp in Python.

看到转换datetime.date / datetime。在Python中从datetime到UTC时间戳。

#3


0  

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

简单日期的最新版本(pypi上的0.2版本)将为您管理详细信息:

>>> 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')

#1


6  

NOTE: My answer is flat-out wrong. (I'd like to delete it, but am unable to do so until the accept flag is removed.)

注意:我的答案是完全错误的。(我想删除它,但在删除accept标志之前,我无法删除它。)

Please see J.F.Sebastian's answer.

请参考书籍赛巴斯蒂安的答案。

Here is code demonstrating a value of now_tz for which our two methods produce different results.

下面的代码演示了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

(Original answer redacted)

(原答案修订)

#2


22  

To convert a datetime with timezone to epoch (POSIX timestamp):

将带时区的日期时间转换为历元(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

It is how datetime.timestamp method is implemented for timezone-aware datetime objects in Python 3.

它是datetime。在Python 3中,时间戳方法用于支持时间区域的datetime对象。

To get "now epoch":

“现在时代”:

from datetime import datetime

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

Or (assuming time uses POSIX epoch):

或(假设时间使用POSIX纪元):

import time

now_epoch = time.time()

Getting "beginning of current day epoch" is more complex because current day may be different in different timezones:

“当前时代的开始”更为复杂,因为在不同的时区,当前的日子可能会有所不同:

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 

See How do I get the UTC time of “midnight” for a given timezone?.

看看如何获得给定时区的“午夜”UTC时间?

To get "beginning of current day epoch" assuming UTC date:

取“当前日开始”假设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

See Converting datetime.date/datetime.datetime to UTC timestamp in Python.

看到转换datetime.date / datetime。在Python中从datetime到UTC时间戳。

#3


0  

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

简单日期的最新版本(pypi上的0.2版本)将为您管理详细信息:

>>> 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')