0

I'm trying to convert UTC time to Europe/Warsaw time. Warsaw time is now UTC + 2 hours. I don't know why I get result with time 30 minutes eariler. I'm parsing date from string similar to: 7:27:02 AM or 2:16:28 PM.

print(time_str) #6:47:46 PM
format = '%I:%M:%S %p'
time_obj = datetime.strptime(time_str, format)
utc_time = time_obj.replace(tzinfo=ZoneInfo('Etc/UTC'))
converted_time = utc_time.astimezone(tz=ZoneInfo('Europe/Warsaw')).time()

print(utc_time.time(), converted_time)

Output is

6:47:46 PM
18:47:46
20:11:46

While I expect:

6:47:46 PM
18:47:46
20:47:46

EDIT - this line fixed it:

time_obj = datetime.strptime(time_str, format).replace(year=2021, month=7, day=14)
user3565923
  • 153
  • 2
  • 12
  • 3
    Try printing the date as well... my guess is that it's using the UTC offset from before 1915. (I believe Python has some oddities around this...) – Jon Skeet Jul 14 '21 at 16:25
  • 1
    In general it makes little sense to convert between timezones without specifying a date part - how are you going to handle daylight saving times? – gimix Jul 14 '21 at 16:38
  • I wouldn't consider this an oddity - time zone conversion without a date just doesn't make much sense. If you don't want the default date 1900-1-1 being added, provide a specific date. – FObersteiner Jul 14 '21 at 17:12

0 Answers0