草庐IT

DateTime2

全部标签

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时区,为什么会出错,如何解决? 最佳

python - 如何从 datetime.datetime 对象中提取小时和分钟?

我需要从datetime.datetime中提取一天中的时间created_at返回的对象属性,但我该怎么做呢?这是我获取datetime.datetime对象的代码。fromdatetimeimport*importtweepyconsumer_key=''consumer_secret=''access_token=''access_secret=''auth=tweepy.OAuthHandler(consumer_key,consumer_secret)auth.set_access_token(access_token,access_secret)api=tweepy.API

python - 如何从 datetime.datetime 对象中提取小时和分钟?

我需要从datetime.datetime中提取一天中的时间created_at返回的对象属性,但我该怎么做呢?这是我获取datetime.datetime对象的代码。fromdatetimeimport*importtweepyconsumer_key=''consumer_secret=''access_token=''access_secret=''auth=tweepy.OAuthHandler(consumer_key,consumer_secret)auth.set_access_token(access_token,access_secret)api=tweepy.API

python - 将 unixtime 转换为 datetime 对象并再次返回(时间转换函数对)

我正在尝试编写一对函数,dt和ut,它们在正常的unix时间(自1970-01-0100以来的秒数)之间来回转换:00:00UTC)和一个Python日期时间对象。如果dt和ut是正确的倒数,那么这段代码将打印两次相同的时间戳:importtime,datetime#Convertaunixtimeutoadatetimeobjectd,andviceversadefdt(u):returndatetime.datetime.fromtimestamp(u)defut(d):returntime.mktime(d.timetuple())u=1004260000printu,"-->"