草庐IT

Primitives-DateTime

全部标签

python - 如何在 python 中对 datetime.timedelta 执行除法?

我希望能够做到以下几点:num_intervals=(cur_date-previous_date)/interval_length或print(datetime.now()-(datetime.now()-timedelta(days=5)))/timedelta(hours=12)#won'trun,wouldlikeittoprint'10'但时间增量不支持除法运算。有没有办法可以为timedeltas实现除法?编辑:看起来这是在Python3.2中添加的(感谢rincewind!):http://bugs.python.org/issue2706 最

python - 如何在 python 中对 datetime.timedelta 执行除法?

我希望能够做到以下几点:num_intervals=(cur_date-previous_date)/interval_length或print(datetime.now()-(datetime.now()-timedelta(days=5)))/timedelta(hours=12)#won'trun,wouldlikeittoprint'10'但时间增量不支持除法运算。有没有办法可以为timedeltas实现除法?编辑:看起来这是在Python3.2中添加的(感谢rincewind!):http://bugs.python.org/issue2706 最

python - 如何使用 pytest 对 python datetime.datetime.now 进行猴子补丁?

我需要测试使用datetime.datetime.now()的函数。最简单的方法是什么? 最佳答案 您需要对datetime.now函数进行monkeypatch。在下面的示例中,我正在创建可以稍后在其他测试中重复使用的fixture:importdatetimeimportpytestFAKE_TIME=datetime.datetime(2020,12,25,17,5,55)@pytest.fixturedefpatch_datetime_now(monkeypatch):classmydatetime:@classmethod

python - 如何使用 pytest 对 python datetime.datetime.now 进行猴子补丁?

我需要测试使用datetime.datetime.now()的函数。最简单的方法是什么? 最佳答案 您需要对datetime.now函数进行monkeypatch。在下面的示例中,我正在创建可以稍后在其他测试中重复使用的fixture:importdatetimeimportpytestFAKE_TIME=datetime.datetime(2020,12,25,17,5,55)@pytest.fixturedefpatch_datetime_now(monkeypatch):classmydatetime:@classmethod

python - 有没有一种简单的方法可以在 Python 中增加一个 datetime 对象一个月?

这个问题在这里已经有了答案:HowdoIcalculatethedatesixmonthsfromthecurrentdateusingthedatetimePythonmodule?(47个回答)关闭6年前.所以我试图找到一种方法将日期时间对象增加一个月。然而,根据thisquestion的说法,这似乎并不那么简单。.我希望得到类似的东西:importdatetimeasdtnow=dt.datetime.now()later=now+dt.timedelta(months=1)但这不起作用。如果可能的话,我也希望能够在下个月的同一天(或最接近的替代方案)去。例如,在1月1日设置的日

python - 有没有一种简单的方法可以在 Python 中增加一个 datetime 对象一个月?

这个问题在这里已经有了答案:HowdoIcalculatethedatesixmonthsfromthecurrentdateusingthedatetimePythonmodule?(47个回答)关闭6年前.所以我试图找到一种方法将日期时间对象增加一个月。然而,根据thisquestion的说法,这似乎并不那么简单。.我希望得到类似的东西:importdatetimeasdtnow=dt.datetime.now()later=now+dt.timedelta(months=1)但这不起作用。如果可能的话,我也希望能够在下个月的同一天(或最接近的替代方案)去。例如,在1月1日设置的日

python - 如何在 Python 中将 `ctime` 转换为 `datetime`?

importtimet=time.ctime()目前对我来说,t是'SatApr2111:58:022012'。我有更多这样的数据。我的问题是:如何在Python中将t转换为datetime?有什么模块吗?我尝试制作时间dict然后转换t,但感觉这不是在Python中执行此操作的最佳方式。详情:我有一个ctime列表(例如['SatApr2111:56:482012','SatApr2111:56:482012']).我想将内容转换为datetime,然后用timestamp将其存储在db中。 最佳答案 您应该使用strptime:

python - 如何在 Python 中将 `ctime` 转换为 `datetime`?

importtimet=time.ctime()目前对我来说,t是'SatApr2111:58:022012'。我有更多这样的数据。我的问题是:如何在Python中将t转换为datetime?有什么模块吗?我尝试制作时间dict然后转换t,但感觉这不是在Python中执行此操作的最佳方式。详情:我有一个ctime列表(例如['SatApr2111:56:482012','SatApr2111:56:482012']).我想将内容转换为datetime,然后用timestamp将其存储在db中。 最佳答案 您应该使用strptime:

Python datetime 对象显示错误的时区偏移

我正在尝试使用datetime和pytz在python中创建一个datetime对象,显示的偏移量是错误的。importdatetimefrompytzimporttimezonestart=datetime.datetime(2011,6,20,0,0,0,0,timezone('Asia/Kolkata'))printstart显示的输出是datetime.datetime(2011,6,20,0,0,tzinfo=)请注意,“亚洲/加尔各答”是IST,即GMT+5:30而不是HMT+5:53。这是一个标准的linux时区,为什么会出错,如何解决? 最佳

Python datetime 对象显示错误的时区偏移

我正在尝试使用datetime和pytz在python中创建一个datetime对象,显示的偏移量是错误的。importdatetimefrompytzimporttimezonestart=datetime.datetime(2011,6,20,0,0,0,0,timezone('Asia/Kolkata'))printstart显示的输出是datetime.datetime(2011,6,20,0,0,tzinfo=)请注意,“亚洲/加尔各答”是IST,即GMT+5:30而不是HMT+5:53。这是一个标准的linux时区,为什么会出错,如何解决? 最佳