在Python中检查当前时间是否小于特定时间?

Mo.*_*Mo. 2 python timezone datetime

在Python脚本中,我希望它在执行之前检查是否在UTC 9AM之前,以便它可以执行特定的操作。

我想知道执行此操作的最佳方法是检查时间以确保每天运行9AM之前的时间吗?请记住,代码可能在具有不同时区的不同计算机上运行。

谢谢

Bri*_*ien 6

datetime模块将对您非常有帮助。尝试如下操作:

>>> d = datetime.datetime.utcnow()
>>> print d
2015-06-17 11:39:48.585000
>>> d.hour
11
>>> if d.hour < 9:
        print "Run your code here"
# nothing happens, it's after 9:00 here.
>>> 
Run Code Online (Sandbox Code Playgroud)