0

I'm trying to generate a time in a particular timezone. I am given, say "09:00:00" in America/New_York. I'd like to generate a datetime with todays date and the given time in the "America/New_York" timezone...

So I tried this..

import pytz
from datetime import datetime, time

nytz = pytz.timezone( "America/New_York" )

nynow = datetime.now().astimezone( nytz )

nine_am = time( 9, 0, 0, 0 )

ny9am = datetime.combine( nynow.date(), nine_am, nytz )

print( nynow )

print( ny9am )

But it prints out something strange and I can't work it out.

2022-03-25 08:53:45.984502-04:00
2022-03-25 09:00:00-04:56

I've no idea where the :56 is coming from. If I try now and convert that time into, say into UK time...

uk_ny9am = ny9am.astimezone( pytz.timezone( "Europe/London" ) )#
print( uk_ny9am )

I get

2022-03-25 13:56:00+00:00

which, at the present time, is 56 minutes out.. UK / US are only ever 4 or 5 hours different so I have no idea what python is doing..

Any ideas !!

ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43

1 Answers1

0

Actually, after doing some more searching I found this...

Weird timezone issue with pytz

Seems to sum it up nicely.

ScaryAardvark
  • 2,855
  • 4
  • 30
  • 43