转换为Unix时间时如何指定时区(UTC)? (Python)

发布于 2024-07-26 11:21:37 字数 704 浏览 9 评论 0 原文

我有 IS8601 格式的 utc 时间戳,正在尝试将其转换为 unix 时间。 这是我的控制台会话:

In [9]: mydate
Out[9]: '2009-07-17T01:21:00.000Z'
In [10]: parseddate = iso8601.parse_date(mydate)

In [14]: ti = time.mktime(parseddate.timetuple())

In [25]: datetime.datetime.utcfromtimestamp(ti)
Out[25]: datetime.datetime(2009, 7, 17, 7, 21)
In [26]: datetime.datetime.fromtimestamp(ti)
Out[26]: datetime.datetime(2009, 7, 17, 2, 21)

In [27]: ti
Out[27]: 1247815260.0
In [28]: parseddate
Out[28]: datetime.datetime(2009, 7, 17, 1, 21, tzinfo=<iso8601.iso8601.Utc object at 0x01D74C70>)

如您所见,我无法获取正确的时间。 如果我使用 fromtimestamp() 则一小时提前一小时,如果我使用 utcfromtimestamp() 则提前六小时

有什么建议吗?

谢谢!

I have a utc timestamp in the IS8601 format and am trying to convert it to unix time. This is my console session:

In [9]: mydate
Out[9]: '2009-07-17T01:21:00.000Z'
In [10]: parseddate = iso8601.parse_date(mydate)

In [14]: ti = time.mktime(parseddate.timetuple())

In [25]: datetime.datetime.utcfromtimestamp(ti)
Out[25]: datetime.datetime(2009, 7, 17, 7, 21)
In [26]: datetime.datetime.fromtimestamp(ti)
Out[26]: datetime.datetime(2009, 7, 17, 2, 21)

In [27]: ti
Out[27]: 1247815260.0
In [28]: parseddate
Out[28]: datetime.datetime(2009, 7, 17, 1, 21, tzinfo=<iso8601.iso8601.Utc object at 0x01D74C70>)

As you can see, I can't get the correct time back. The hour is ahead by one if i use fromtimestamp(), and it's ahead by six hours if i use utcfromtimestamp()

Any advice?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞觅青森が 2024-08-02 11:21:38

您可以使用 struct_time >datetime.utctimetuple() ,然后使用 calendar.timegm()

calendar.timegm(parseddate.utctimetuple())

这也会处理任何夏令时偏移,因为 utctimetuple() 对此进行了标准化。

You can create an struct_time in UTC with datetime.utctimetuple() and then convert this to a unix timestamp with calendar.timegm():

calendar.timegm(parseddate.utctimetuple())

This also takes care of any daylight savings time offset, because utctimetuple() normalizes this.

瑾兮 2024-08-02 11:21:38

我只是猜测,但一小时的差异可能不是因为时区,而是因为夏令时的开启/关闭。

I am just guessing, but one hour difference can be not because of time zones, but because of daylight savings on/off.

忆梦 2024-08-02 11:21:38
naive_utc_dt = parseddate.replace(tzinfo=None)
timestamp = (naive_utc_dt - datetime(1970, 1, 1)).total_seconds()
# -> 1247793660.0

请参阅类似问题的另一个答案了解更多详细信息。

然后回来:

utc_dt = datetime.utcfromtimestamp(timestamp)
# -> datetime.datetime(2009, 7, 17, 1, 21)
naive_utc_dt = parseddate.replace(tzinfo=None)
timestamp = (naive_utc_dt - datetime(1970, 1, 1)).total_seconds()
# -> 1247793660.0

See more details in another answer to similar question.

And back:

utc_dt = datetime.utcfromtimestamp(timestamp)
# -> datetime.datetime(2009, 7, 17, 1, 21)
偏爱你一生 2024-08-02 11:21:38
import time
import datetime
import calendar

def date_time_to_utc_epoch(dt_utc):         #convert from utc date time object (yyyy-mm-dd hh:mm:ss) to UTC epoch
    frmt="%Y-%m-%d %H:%M:%S"
    dtst=dt_utc.strftime(frmt)              #convert datetime object to string
    time_struct = time.strptime(dtst, frmt) #convert time (yyyy-mm-dd hh:mm:ss) to time tuple
    epoch_utc=calendar.timegm(time_struct)  #convert time to to epoch
    return epoch_utc

#----test function --------
now_datetime_utc = int(date_time_to_utc_epoch(datetime.datetime.utcnow()))
now_time_utc = int(time.time())

print (now_datetime_utc)
print (now_time_utc)

if now_datetime_utc == now_time_utc : 
    print ("Passed")  
else : 
    print("Failed")
import time
import datetime
import calendar

def date_time_to_utc_epoch(dt_utc):         #convert from utc date time object (yyyy-mm-dd hh:mm:ss) to UTC epoch
    frmt="%Y-%m-%d %H:%M:%S"
    dtst=dt_utc.strftime(frmt)              #convert datetime object to string
    time_struct = time.strptime(dtst, frmt) #convert time (yyyy-mm-dd hh:mm:ss) to time tuple
    epoch_utc=calendar.timegm(time_struct)  #convert time to to epoch
    return epoch_utc

#----test function --------
now_datetime_utc = int(date_time_to_utc_epoch(datetime.datetime.utcnow()))
now_time_utc = int(time.time())

print (now_datetime_utc)
print (now_time_utc)

if now_datetime_utc == now_time_utc : 
    print ("Passed")  
else : 
    print("Failed")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文