1

I have tried two different scenarios:

  1. I fetched Current Datetime of UTC and Europe/Paris and then I just converted in string which shows 02 hours of gap which is correct.
from datetime import datetime
import datetime as dt
import pytz
from dateutil import tz

current_utc = datetime.utcnow()
current_europe = datetime.now(pytz.timezone('Europe/Paris'))

current_utc_str = datetime.strftime(current_utc, "%Y-%m-%d %H:%M")
current_europe_str = datetime.strftime(current_europe, "%Y-%m-%d %H:%M")

print('current_utc',current_utc_str)
print('current_europe',current_europe_str)

results:

current_utc 2023-03-30 07:01
current_europe 2023-03-30 09:01
  1. I created a custom UTC datetime object and then converted it to Europe/Paris timezone and here are the results with the Gap of 01 Hour.
from datetime import datetime
import datetime as dt
import pytz
from dateutil import tz

utc = datetime(2023, 3, 21, 23, 45).replace(tzinfo=dt.timezone.utc)
utc_str = datetime.strftime(utc, "%Y-%m-%d %H:%M")
print("utc_str", utc_str)

from_zone = tz.gettz("UTC")
to_zone = tz.gettz('Europe/Paris')
utc = utc.replace(tzinfo=from_zone)
new_time = utc.astimezone(to_zone)
new_time_str = datetime.strftime(new_time, "%Y-%m-%d %H:%M")
print("new_time_str", new_time_str)

results:

utc_str 2023-03-21 23:45
new_time_str 2023-03-22 00:45

What is the reason behind this 01 hour of variation while fetching current and creating custom datetime?

Edit How can we handle Daylight Saving Time (DST) for custom created Datetime objects?

I think Display the time in a different time zone this doesn't answer about handling Daylight Saving Time (DST).

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Hemal Patel
  • 878
  • 6
  • 16
  • 1
    concerning DST transitions: you don't handle those yourself, they are handled by the datetime + timezone libraries. The latter have to implement the rules of the [IANA tz database](https://www.iana.org/time-zones), which include DST transitions. – FObersteiner Mar 30 '23 at 16:50

1 Answers1

1

The reason behind the one-hour difference is due to the Daylight Saving Time (DST) adjustments. The Europe/Paris timezone observes Daylight Saving Time, while UTC does not.

When you fetched the current time in UTC and Europe/Paris using the datetime.now() function, the pytz module took care of the DST adjustment automatically. Therefore, the time difference between UTC and Europe/Paris was correctly calculated as two hours.

However, when you created a custom UTC datetime object, you explicitly set the timezone to UTC using the replace() method. Therefore, there was no DST adjustment applied, and the time difference between UTC and Europe/Paris was only one hour.

To handle DST adjustments correctly, you can use the pytz module to get the current UTC time and then convert it to the Europe/Paris timezone. For example:

import datetime
import pytz

utc_now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
paris_now = utc_now.astimezone(pytz.timezone('Europe/Paris'))

This code will give you the correct time difference of two hours between UTC and Europe/Paris, taking into account any DST adjustments.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Naga
  • 10,944
  • 2
  • 21
  • 38
  • yes it works if we fetch current time. how can i make it work with custom created objects? – Hemal Patel Mar 30 '23 at 07:20
  • 1
    @HemalPatel tz 'Europe/Paris' had +1 hour UTC offset on 2023-03-21 and 2023-03-22 but now (2023-03-30) has a UTC offset of +2 hours because DST is active since 2023-03-26, see https://www.timeanddate.com/time/change/france/paris – FObersteiner Mar 30 '23 at 16:57