如何将格式为“%d/%m/%Y”的字符串转换为时间戳?
"01/12/2011" -> 1322697600
如何将格式为“%d/%m/%Y”的字符串转换为时间戳?
"01/12/2011" -> 1322697600
当前回答
将字符串转换为日期对象:
from datetime import date, datetime
date_string = "01/12/2011"
date_object = date(*map(int, reversed(date_string.split("/"))))
assert date_object == datetime.strptime(date_string, "%d/%m/%Y").date()
将date对象转换为POSIX时间戳的方法取决于时区。从转换datetime。在Python中,date到UTC时间戳:
date对象表示UTC的午夜 导入日历 Timestamp1 = calendar.timegm(utc_date.timetuple()) Timestamp2 = (utc_date.toordinal() - date(1970, 1,1).toordinal()) * 24*60*60 断言timestamp1 == timestamp2 Date对象表示本地时间的午夜 导入的时间 timestamp = time.mktime(local_date.timetuple()) 断言timestamp3 != timestamp1或(time.gmtime() == time.localtime())
时间戳是不同的,除非UTC的午夜和本地时间是相同的时间实例。
其他回答
答案也取决于你输入的日期和时区。如果你的日期是本地日期,那么你可以像katrielalex说的那样使用mktime() -只是我不明白为什么他使用datetime而不是这个更短的版本:
>>> time.mktime(time.strptime('01/12/2011', "%d/%m/%Y"))
1322694000.0
但是请注意,我的结果与他的结果不同,因为我可能在不同的TZ中(结果是无时区的UNIX时间戳)
现在如果输入日期已经是UTC,那么我认为正确的解决方案是:
>>> calendar.timegm(time.strptime('01/12/2011', '%d/%m/%Y'))
1322697600
>>> int(datetime.datetime.strptime('01/12/2011', '%d/%m/%Y').strftime("%s"))
1322683200
将字符串转换为日期对象:
from datetime import date, datetime
date_string = "01/12/2011"
date_object = date(*map(int, reversed(date_string.split("/"))))
assert date_object == datetime.strptime(date_string, "%d/%m/%Y").date()
将date对象转换为POSIX时间戳的方法取决于时区。从转换datetime。在Python中,date到UTC时间戳:
date对象表示UTC的午夜 导入日历 Timestamp1 = calendar.timegm(utc_date.timetuple()) Timestamp2 = (utc_date.toordinal() - date(1970, 1,1).toordinal()) * 24*60*60 断言timestamp1 == timestamp2 Date对象表示本地时间的午夜 导入的时间 timestamp = time.mktime(local_date.timetuple()) 断言timestamp3 != timestamp1或(time.gmtime() == time.localtime())
时间戳是不同的,除非UTC的午夜和本地时间是相同的时间实例。
我使用ciso8601,它比datetime的strptime快62倍。
t = "01/12/2011"
ts = ciso8601.parse_datetime(t)
# to get time in seconds:
time.mktime(ts.timetuple())
你可以在这里了解更多。
很多答案都没有考虑到日期一开始就很天真
为了正确,您需要首先将初始日期设置为具有时区意识的datetime
import datetime
import pytz
# naive datetime
d = datetime.datetime.strptime('01/12/2011', '%d/%m/%Y')
>>> datetime.datetime(2011, 12, 1, 0, 0)
# add proper timezone
pst = pytz.timezone('America/Los_Angeles')
d = pst.localize(d)
>>> datetime.datetime(2011, 12, 1, 0, 0,
tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
# convert to UTC timezone
utc = pytz.UTC
d = d.astimezone(utc)
>>> datetime.datetime(2011, 12, 1, 8, 0, tzinfo=<UTC>)
# epoch is the beginning of time in the UTC timestamp world
epoch = datetime.datetime(1970,1,1,0,0,0,tzinfo=pytz.UTC)
>>> datetime.datetime(1970, 1, 1, 0, 0, tzinfo=<UTC>)
# get the total second difference
ts = (d - epoch).total_seconds()
>>> 1322726400.0
另外:
小心,在datetime中使用pytz for tzinfo。datetime不适用于许多时区。参见datetime with pytz timezone。不同的偏移量取决于tzinfo的设置方式
# Don't do this:
d = datetime.datetime(2011, 12, 1,0,0,0, tzinfo=pytz.timezone('America/Los_Angeles'))
>>> datetime.datetime(2011, 1, 12, 0, 0,
tzinfo=<DstTzInfo 'America/Los_Angeles' LMT-1 day, 16:07:00 STD>)
# tzinfo in not PST but LMT here, with a 7min offset !!!
# when converting to UTC:
d = d.astimezone(pytz.UTC)
>>> datetime.datetime(2011, 1, 12, 7, 53, tzinfo=<UTC>)
# you end up with an offset
https://en.wikipedia.org/wiki/Local_mean_time