欺骗 Python 中正在进行的日期时间

huangapple go评论51阅读模式
英文:

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

您可以为 todayutcnow 编写类似的实现。

在线尝试

英文:

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

You can write similar implementations for today and utcnow

Try it online

huangapple
  • 本文由 发表于 2023年2月19日 22:55:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定