英文:
Spoof ongoing datetime in Python
问题
我正在寻找欺骗实时datetime
的最简单解决方案,具体来说,我希望它从特定时间开始,比如2023-01-03 15:29
,然后让时间继续流逝,就像时钟一样。
有很多方法可以欺骗当前的datetime
,但我还没有找到一种可以连续这样做的方法,以便虚假的时间不断前进。
英文:
I am looking for the simplest solution to spoof live datetime
, specifically, I would like it to start at a specific time, say 2023-01-03 15:29
, and make it go on, so that the clock is ticking, so to speak.
There are plenty of ways to spoof current datetime
, but I haven't find a way that would do so continuously, so the fake time keeps moving.
答案1
得分: 1
我的第一个想法是,而不是尝试“伪造”日期时间,只需执行一次“翻译”。
基本上,您只需要计算需要从当前datetime.now()中减去的日期时间,以达到所需的日期。
desired_time = datetime.now() - translation ==>
translation = datetime.now() - desired_time
之后,您只需调用datetime.now() - translation,这将有效地推进所需日期的时钟。
希望这有意义并对您有帮助!
英文:
My first thought is, instead of trying to 'spoof' datetime, just perform a "translation".
Basically, you just need to calculate the datetime you need to subtract from the current datatime.now() to reach your desired date.
desired_time = datatime.now - translation ==>
translation = datetime.now - desired_time
After that you can simply call datetime.now() - translation which will effectively progress the clock of your desired date.
Hope that makes sense and can work for you!
答案2
得分: 1
以下是代码的翻译部分:
首先,定义一个文件(比如 spoofdt.py
),其中包含一个继承自 datetime.datetime
的类,该类包含一个类方法 now
,用于记录第一次调用它的时间,并返回基于该时间与您所期望的时间差的值。
import datetime as dt
class datetime(dt.datetime):
_ftime = dt.datetime(2023, 1, 3, 15, 29)
_dtime = None
@classmethod
def now(cls, tz=None):
if cls._dtime is None:
cls._dtime = super(datetime, cls).now() - cls._ftime
t = super(datetime, cls).now() - cls._dtime
if tz is not None:
return t.astimezone(tz)
return t
然后,在实际代码中,不要导入 datetime
模块,而是导入 spoofdt
:
import time
# 代替
# from datetime import datetime
# 使用以下代码:
from spoofdt import datetime
for _ in range(10):
print(datetime.now())
time.sleep(1)
这将产生以下输出:
2023-01-03 15:29:00.000003
2023-01-03 15:29:01.001141
2023-01-03 15:29:02.001475
2023-01-03 15:29:03.002694
2023-01-03 15:29:04.003839
2023-01-03 15:29:05.005119
2023-01-03 15:29:06.005451
2023-01-03 15:29:07.006651
2023-01-03 15:29:08.007829
2023-01-03 15:29:09.009107
英文:
First, define a file (say spoofdt.py
) with a class that inherits datetime.datetime
and contains a classmethod now
that records the first time you call it, and returns a value based on the difference between that time and your desired time.
import datetime as dt
class datetime(dt.datetime):
_ftime = dt.datetime(2023, 1, 3, 15, 29)
_dtime = None
@classmethod
def now(cls, tz=None):
if cls._dtime is None:
cls._dtime = super(datetime, cls).now() - cls._ftime
t = super(datetime, cls).now() - cls._dtime
if tz is not None:
return t.astimezone(tz)
return t
Then instead of importing the datetime
module, import spoofdt
In your actual code:
import time
# instead of
# from datetime import datetime
# do this:
from spoofdt import datetime
for _ in range(10):
print(datetime.now())
time.sleep(1)
Which gives:
2023-01-03 15:29:00.000003
2023-01-03 15:29:01.001141
2023-01-03 15:29:02.001475
2023-01-03 15:29:03.002694
2023-01-03 15:29:04.003839
2023-01-03 15:29:05.005119
2023-01-03 15:29:06.005451
2023-01-03 15:29:07.006651
2023-01-03 15:29:08.007829
2023-01-03 15:29:09.009107
** Process exited - Return Code: 0 **
Press Enter to exit terminal
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论