Python Croniter获得24小时格式的时间

发布于 2025-01-18 09:26:02 字数 420 浏览 0 评论 0原文

我已经尝试了Python Croniter的GET_PREV()方法,以根据Cron字符串和当前时间获得最后的计划时间。

当我在Windows机器中尝试时,它以24小时格式返回该值。 但是,当我通过AWS胶(可能是Linux)尝试相同的代码时,它以12小时格式返回了无需AM/PM表示法的时间。

是否有任何方法可以迫使克朗特始终以24小时格式返回时间。

        currentTime=datetime.now()
        print(currentTime)
        cron = croniter(cron_str, currentTime)
        lastCronScheduleTime=cron.get_prev(datetime)
        print(lastCronScheduleTime)

I have tried the Python croniter's get_prev() method to get the last scheduled time based on a cron string and current time.

When I tried it in windows machine it returned the value in 24 hour format.
But when I tried the same code via AWS glue (possibly Linux) , it returned scheduled time in 12 hour format without AM/PM notation.

Is there any way to force the croniter to return time in 24 hour format always.

        currentTime=datetime.now()
        print(currentTime)
        cron = croniter(cron_str, currentTime)
        lastCronScheduleTime=cron.get_prev(datetime)
        print(lastCronScheduleTime)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小红帽 2025-01-25 09:26:02

在您的示例中,get_prev返回dateTime.dateTime实例。

DateTime传递给print()时,它首先通过调用其__ str __方法将其转换为字符串。 __ str __方法调用isoformat()请参阅Python Docs )。

换句话说,示例的输出应采用ISO 8601格式,该格式始终使用24小时的符号。

我的猜测是您看到了不同的小时值,因为您的Windows和Linux机器使用不同的时区,并且datetime.now()返回系统默认时区中的幼稚日期。

In your example, get_prev returns a datetime.datetime instance.

When a datetime is passed to print(), it is first converted to string by calling its __str__ method. The __str__ method calls isoformat() (see python docs).

In other words, the output from your example should be in ISO 8601 format, which always uses 24-hour notation.

My guess is you are seeing different hour values because your Windows and Linux machines use different timezones, and datetime.now() returns a naive datetime in system's default timezone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文