0

I have a UTC date timezone aware, and I would like to add/substract hours which are UTC aware (+/- hours from GMT/UTC). Meaning -4:00 hours are 4 hours behind UTC date time etc. I'm running the bellow code which is working, but it feels like there is a better way. Please confirm/infirm.

PS: I saw a few urls with examples showing how to change the time zone which I want to avoid because of DST.

from datetime import datetime, timedelta
import pytz

# Date aware of time zone
today_date = datetime.today().utcnow().replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC)
print(today_date)

# UTC datetime (-) Hours from is defined as:
time_zone_to_utc_suffix: str = '-4:00' # I receive the data in str
time_zone_to_utc_hours:int = int(time_zone_to_utc_suffix.split(':')[0])

#### THE PART I FEEL CAN BE IMPROVED ######
result_date_adjusted_into_utc = today_date + timedelta(hours=time_zone_to_utc_hours)

print(f'Date adjusted by {time_zone_to_utc_hours} hours, is {result_date_adjusted_into_utc}')

Tx

Je Je
  • 508
  • 2
  • 8
  • 23

1 Answers1

1

You wrote "add/substract hours which are UTC aware" I think you are misunderstanding what "time zone aware" means. Your date is indeed time zone aware, you correctly constructed it like this:

today_date = datetime.today().utcnow().replace(hour=0, minute=0, second=0, microsecond=0, tzinfo=pytz.UTC)

There's other ways to achieve the same, but this is fine.

However, there is no such thing as a time zone aware interval of 4 hours. 4 hours is 4 hours, no matter where you are on the planet. Only the resulting time after you add or subtract an interval can be time zone aware, or not.

If you add 4 hours to a time zone aware time, it's still in the same time zone, just 4 hours later:

right_now = datetime.today().utcnow().replace(tzinfo=pytz.UTC)
four_hours_later = right_now + timedelta(hours=4)

The value of four_hours_later is still in the UTC zone, just 4 hours later.

If you want to compute what the same time is in a time zone that's 4 hours ahead, i.e. in the '+04:00' zone, you can do this:

from datetime import datetime, timezone
import pytz

right_now = datetime.today().utcnow().replace(tzinfo=pytz.UTC)
four_hours_later = right_now + timedelta(hours=4)
right_now_in_plus_4 = right_now.astimezone(timezone(timedelta(hours=4)))

print(right_now_in_plus_4)  # same time, different zone
print(four_hours_later)     # same zone, different time (4h later)

Typically, since you're already using pytz, you wouldn't want to use +4:00 or anything like that, but instead just the actual name of the timezone:

right_now_in_dubai = right_now.astimezone(pytz.timezone('asia/dubai'))
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • 1
    i thought there was a better way. tx – Je Je Apr 12 '22 at 03:56
  • You're welcome - if you feel this answers your question, please click the checkmark, so your question no longer appears as unanswered – Grismar Apr 12 '22 at 09:29
  • This is already what I was doing. I thought that there was a better way – Je Je Apr 12 '22 at 23:00
  • There isn't, I'm afraid - if you feel that doesn't answer your question, you could consider just removing the question, because I doubt a better answer will present itself. If you don't mind my asking: what's wrong with using `.astimezone()` for timezone shifts and adding `timedelta` for adding a time interval? I find it hard to think of a way that would be much better? – Grismar Apr 13 '22 at 03:21