Python Croniter Get Time in 24 Hour Format

Python Croniter Get Time in 24 Hour Format

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)
1

1 Answer

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.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.