Python Croniter获得24小时格式的时间
我已经尝试了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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的示例中,
get_prev
返回dateTime.dateTime
实例。当
DateTime
传递给print()
时,它首先通过调用其__ str __
方法将其转换为字符串。__ str __
方法调用isoformat()
(请参阅Python Docs )。换句话说,示例的输出应采用ISO 8601格式,该格式始终使用24小时的符号。
我的猜测是您看到了不同的小时值,因为您的Windows和Linux机器使用不同的时区,并且
datetime.now()
返回系统默认时区中的幼稚日期。In your example,
get_prev
returns adatetime.datetime
instance.When a
datetime
is passed toprint()
, it is first converted to string by calling its__str__
method. The__str__
method callsisoformat()
(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.