pythondatetimepytz

use closing hours by default


Using this piece of the pytz script,

import dateutil
import pytz

utc = pytz.UTC
print(utc.localize(dateutil.parser.parse('2024-10-31')))

the output shows:

2024-10-31 00:00:00+00:00

We want to default to "closing" hours or end of the day, i.e 2024-10-31 23:59:59+00:00 in the example. Is there a way to do it?


Solution

  • datetime.timedelta is useful for this type of thing:

    import dateutil
    import pytz
    from datetime import timedelta
    utc = pytz.UTC
    print(utc.localize(dateutil.parser.parse('2024-10-31'))+timedelta(days=1)-timedelta(seconds=1))