用strftime将python datetime转换为epoch。

时间:2022-01-25 02:47:15

I have a time in UTC from which I want the number of seconds since epoch.

我在UTC有一个时间,我想要从那个时代开始的秒数。

I am using strftime to convert it to the number of seconds. Taking 1st April 2012 as an example.

我使用strftime将它转换为秒数。以2012年4月1日为例。

>>>datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'

1st of April 2012 UTC from epoch is 1333238400 but this above returns 1333234800 which is different by 1 hour.

2012年4月1日,UTC时间为1333238400,但上面的数字是1333234800,不同的时间是1小时。

So it looks like that strftime is taking my system time into account and applies a timezone shift somewhere. I thought datetime was purely naive?

所以看起来strftime会把我的系统时间考虑进去并且在某个地方应用时区转换。我认为datetime纯粹是天真的?

How can I get around that? If possible avoiding to import other libraries unless standard. (I have portability concerns).

我该怎么做呢?如有可能,避免进口其他图书馆,除非标准。(我可移植性问题)。

6 个解决方案

#1


215  

Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone.

Python实际上并不支持%s作为strftime的参数(如果您检查http://docs.python.org/library/datetime.html# strftimeand -strptime-behavior,它不在列表中),它工作的唯一原因是Python将信息传递给您的系统的strftime,它使用您的本地时区。

If you want to convert a python datetime to seconds since epoch you should do it explicitly:

如果您想要将python datetime转换为秒,那么您应该明确地做到这一点:

>>> datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
>>> (datetime.datetime(2012,04,01,0,0) - datetime.datetime(1970,1,1)).total_seconds()
1333238400.0

In Python 3.3+ you can use timestamp() instead:

在Python 3.3+中,您可以使用timestamp()代替:

>>> datetime.datetime(2012,4,1,0,0).timestamp()
1333234800.0

#2


72  

I had serious issues with Timezones and such. The way Python handles all that happen to be pretty confusing (to me). Things seem to be working fine using the calendar module (see links 1, 2, 3 and 4).

我在时区等问题上遇到了严重的问题。Python处理所有这些事情的方式让我很困惑(对我来说)。使用日历模块(参见链接1、2、3和4)可以很好地工作。

>>> import datetime
>>> import calendar
>>> aprilFirst=datetime.datetime(2012, 04, 01, 0, 0)
>>> calendar.timegm(aprilFirst.timetuple())
1333238400

#3


24  

import time
from datetime import datetime
now = datetime.now()

time.mktime(now.timetuple())

#4


14  

import time
from datetime import datetime
now = datetime.now()

# same as above except keeps microseconds
time.mktime(now.timetuple()) + now.microsecond * 1e-6

(Sorry, it wouldn't let me comment on existing answer)

(抱歉,它不会让我对已有的答案发表评论)

#5


1  

This works in Python 2 and 3:

这在python2和3中是有效的:

>>> import time
>>> import calendar
>>> calendar.timegm(time.gmtime())
1504917998

Just following the official docs... https://docs.python.org/2/library/time.html#module-time

就在官方文件后面……https://docs.python.org/2/library/time.html module-time

#6


0  

if you just need a timestamp in unix /epoch time, this one line works:

如果您只需要unix /epoch时间的时间戳,那么这一行是:

created_timestamp = int((datetime.datetime.now() - datetime.datetime(1970,1,1)).total_seconds())
>>> created_timestamp
1522942073L

and depends only on datetime works in python2 and python3

并且只依赖于python2和python3中的datetime。

#1


215  

Python doesn't actually support %s as an argument to strftime (if you check at http://docs.python.org/library/datetime.html#strftime-and-strptime-behavior it's not in the list), the only reason it's working is because Python is passing the information to your system's strftime, which uses your local timezone.

Python实际上并不支持%s作为strftime的参数(如果您检查http://docs.python.org/library/datetime.html# strftimeand -strptime-behavior,它不在列表中),它工作的唯一原因是Python将信息传递给您的系统的strftime,它使用您的本地时区。

If you want to convert a python datetime to seconds since epoch you should do it explicitly:

如果您想要将python datetime转换为秒,那么您应该明确地做到这一点:

>>> datetime.datetime(2012,04,01,0,0).strftime('%s')
'1333234800'
>>> (datetime.datetime(2012,04,01,0,0) - datetime.datetime(1970,1,1)).total_seconds()
1333238400.0

In Python 3.3+ you can use timestamp() instead:

在Python 3.3+中,您可以使用timestamp()代替:

>>> datetime.datetime(2012,4,1,0,0).timestamp()
1333234800.0

#2


72  

I had serious issues with Timezones and such. The way Python handles all that happen to be pretty confusing (to me). Things seem to be working fine using the calendar module (see links 1, 2, 3 and 4).

我在时区等问题上遇到了严重的问题。Python处理所有这些事情的方式让我很困惑(对我来说)。使用日历模块(参见链接1、2、3和4)可以很好地工作。

>>> import datetime
>>> import calendar
>>> aprilFirst=datetime.datetime(2012, 04, 01, 0, 0)
>>> calendar.timegm(aprilFirst.timetuple())
1333238400

#3


24  

import time
from datetime import datetime
now = datetime.now()

time.mktime(now.timetuple())

#4


14  

import time
from datetime import datetime
now = datetime.now()

# same as above except keeps microseconds
time.mktime(now.timetuple()) + now.microsecond * 1e-6

(Sorry, it wouldn't let me comment on existing answer)

(抱歉,它不会让我对已有的答案发表评论)

#5


1  

This works in Python 2 and 3:

这在python2和3中是有效的:

>>> import time
>>> import calendar
>>> calendar.timegm(time.gmtime())
1504917998

Just following the official docs... https://docs.python.org/2/library/time.html#module-time

就在官方文件后面……https://docs.python.org/2/library/time.html module-time

#6


0  

if you just need a timestamp in unix /epoch time, this one line works:

如果您只需要unix /epoch时间的时间戳,那么这一行是:

created_timestamp = int((datetime.datetime.now() - datetime.datetime(1970,1,1)).total_seconds())
>>> created_timestamp
1522942073L

and depends only on datetime works in python2 and python3

并且只依赖于python2和python3中的datetime。