问题描述
您好,我是编程的新手,因此,如果问题不重要,请原谅。 我正在尝试将日期时间对象保存为Unix纪元格式,以便以后在计算时进行比较。我尝试了两种方法来做同样的事情。
import datetime
import pytz
print ('Method 1\n***********')
# Here I am using deltatiem = 3 hrs and removing 10800sec from unix epoch,But result is not matching with second method.
dt1 = datetime.datetime.strptime('13-Nov-2019 11:30','%d-%b-%Y %H:%M')
deltatime = datetime.timedelta(hours=3)
fdt1_UTC = dt - deltatime
print ('Kuwait date time:\t',dt1)
print ('UTC date time:\t\t',fdt1_UTC)
print ('Kuwait unix epoch:\t',dt1.timestamp())
print ('UTC unix epch:\t\t',fdt1_UTC.timestamp())
print ('\n\nMethod 2\n***********')
dt = datetime.datetime.strptime('13-Nov-2019 11:30','%d-%b-%Y %H:%M')
kw_tz = pytz.timezone('Asia/Kuwait')
fdt = kw_tz.localize(dt) # localize just assumes that the naive datetime you pass it is "right" (except for not kNowing about the timezone!) and so just sets the timezone,no other adjustments.
fdt_UTC = fdt.astimezone(pytz.timezone('UTC'))
print ('Kwt date time (naive):\t',dt)
print ('kwt date time (aware):\t',fdt)
print ('UTC date time (aware):\t',fdt_UTC)
print ('Kuwait unix epoch:\t',dt.timestamp())
print ('UTC unix epoch:\t\t',fdt_UTC.timestamp())
但是方法1和方法2的结果不匹配。在方法2中,UTC Unix时代与科威特Unix时代难道不应该吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)