![](/img/trans.png)
[英]Python: Printing all hours between two datetime accounting for daylight saving
[英]Python datetime and daylight saving
我的代码使用datetime.now()
来获取当前日期和时间。 问题是由于夏令时,时间延迟了1个小时。
如何获得“实际”当前时间(与系统时钟相同)
谢谢
如果仅需要当前系统时间,则可以尝试使用time
模块。 time.time()
将为您提供当前系统时间的unix时间戳。
编辑 :回应OP的评论
使用time.strftime()
和time.localtime()
与datetime
对象和数据库进行接口
您应该阅读python的datetime文档,直到第8.1节 ,其中给出了有关如何实现所需内容的示例。
from datetime import tzinfo, timedelta, datetime
ZERO = timedelta(0)
HOUR = timedelta(hours=1)
# A UTC class.
class UTC(tzinfo):
"""UTC"""
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return ZERO
utc = UTC()
datetime.now(utc)
我可能已经完全离开了,但是如果您的BIOS中的时间设置为UTC(因此与本地时间相差一小时)
datetime.datetime.utcnow()
应该产生您想要的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.