小编Nio*_*ols的帖子

python3中的python3 datetime.timestamp?

我有一段python 3代码,它在22:00调用一个函数.

# Imports
from datetime import datetime, date, time, timedelta
import sched
import time as mod_time

# Find the next datetime corresponding to 22:00
first_run = datetime.combine(date.today(), time(22,0))
first_run = first_run if first_run > datetime.now() else first_run + timedelta(1)

# Dumb test function
def my_function():
    print('my_function')

# Run the function at 22:00
scheduler = sched.scheduler(mod_time.time, mod_time.sleep)
scheduler.enterabs(first_run.timestamp(), 1, my_function, ())
scheduler.run()
Run Code Online (Sandbox Code Playgroud)

此代码目前在python 3中工作.我希望它能在python 2中运行.我唯一的问题来自以下方面:

first_run.timestamp()
Run Code Online (Sandbox Code Playgroud)

我尝试用以下内容替换它:

(first_run - datetime(1970, 1, 1)).total_seconds()
Run Code Online (Sandbox Code Playgroud)

但是我的时区似乎有问题(UTC太容易了,我在UTC + 2).first_run中应该有tzinfo.也许我应该添加一些东西?

我很失落,任何帮助都会受到赞赏.非常感谢提前帮助我.

EDIT1: …

python python-2.x python-3.x

14
推荐指数
2
解决办法
1万
查看次数

标签 统计

python ×1

python-2.x ×1

python-3.x ×1