0

Hey I encounter a weird exception and don't know how to fix my code:

def start():
    try:
        from datetime import datetime
        import pytz
        import time
        from pytz import country_timezones
        
        productLaunchDate = "2023-03-22 09:00:00"
        globalCountry = "GB"
        
        timezone = country_timezones(globalCountry)[0]
        currentTimezone = pytz.timezone(timezone) 
        time1 = datetime.now(currentTimezone)
        print(time1)
        time2 = datetime.strptime(productLaunchDate, '%Y-%m-%d %H:%M:%S')
        print(time2)
        if time1 > time2:
            return
        td = time2 - time1
        for i in range(int(td.total_seconds()), 0, -1):
            print(f'{i//3600} hours, {(i//60)%60} minutes, and {i%60} seconds')
            time.sleep(1)
            
    except Exception as e:
        print(f"Error at wait -> {e}")

start()

Basically what I want to do is use a different country iso get the timezone and wait until the date, the productLaunchDate is already in the right timezone.

  • See [Aware and Naive Objects](https://docs.python.org/3/library/datetime.html#aware-and-naive-objects) - you cannot compare date/time with and without a time zone. In your code, `time1` has a timezone, while `time2` has not. – FObersteiner Mar 12 '23 at 16:47
  • 1
    "the productLaunchDate is already in the right timezone." - right, you know that as background knowledge, but you have to tell Python explicitly. `time2 = currentTimezone.localize(time2)` is one way to do it. – slothrop Mar 12 '23 at 16:51
  • "productLaunchDate is already in the right timezone" shouldn't it be `"2023-03-22 09:00:00 BST"` or something, in that case? – njzk2 Mar 12 '23 at 17:11

1 Answers1

-1

This is my simple fix I guess :)

def start():
    try:
        from datetime import datetime
        import pytz
        import time
        from pytz import country_timezones
        
        productLaunchDate = "2023-03-22 09:00:00"
        globalCountry = "DE"
        
        timezone = country_timezones(globalCountry)[0]
        currentTimezone = pytz.timezone(timezone) 
        time1 = datetime.now(currentTimezone)
        print(time1)
        time2 = datetime.strptime(productLaunchDate, '%Y-%m-%d %H:%M:%S')
        time2 = time2.replace(tzinfo=currentTimezone)
        
        print(time2)
        if time1 > time2:
            return
        td = time2 - time1
        for i in range(int(td.total_seconds()), 0, -1):
            print(f'{i//3600} hours, {(i//60)%60} minutes, and {i%60} seconds')
            time.sleep(1)
            
    except Exception as e:
        print(f"Error at wait -> {e}")

start()
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '23 at 00:16
  • You do not want to use `replace` with a pytz.timezone, see [Weird timezone issue with pytz](https://stackoverflow.com/q/11473721/10197418). – FObersteiner Mar 13 '23 at 07:54