Course Content
Dealing with Dates and Times in Python
Dealing with Dates and Times in Python
Daylight Savings Time
Now we know how to set and adjust timezone to certain datetime
objects. But can we handle daylight saving time in Python?
If you work with data containing dates and times up to minutes, it can be a bit dangerous. Of course, we can manually add (or subtract) one hour from time. But there is no touchpoint in this question among countries. For example, the majority of Asian, African, and South American countries do not set their clock back (or forward) by one hour, while most European countries and US states do it. But even among them, there are differences: for example, Americans change their clocks on the second Sunday in March at 02:00, while European countries do it on the Last Sunday in March at 02:00 UTC. Here pytz
will help us one more time. To take into consideration possible DST shift use .normalize
method on timezone
object with timedelta
object with timezone
set as an argument. For example, in 2021 Ukraine set its clocks forward by one hour on 28 March at 03:00 (local time). So, it should be impossible to have 03:30 time on this date (since 03:00 became 04:00). Let's see how it works:
# Load libraries and classes import pytz from datetime import datetime from pytz import timezone tz = timezone('Europe/Kiev') # timezone object dt = datetime(2021, 3, 28, 3, 35) # datetime object # Set timezone to datetime dt_tz = tz.localize(dt) dt_tz_norm = tz.normalize(dt_tz) # adjust time to DST # Testing print(f"Original datetime: {dt_tz}") print(f"Normalized datetime: {dt_tz_norm}")
There is only one correct answer when we are talking about the 'spring forward' shift. But if we are talking about 'fall back' shift there is no correct answer, since one time happens twice. For example, in 2021 Ukraine set its clocks back by one hour on 31 October at 04:00 (local time). So, there was, for example, 03:30 happened twice (before and after setting clocks).
Task
Given three datetime
objects: dt1
, dt2
, and dt3
.
- Create
timezone
objecttz
with 'America/Los_Angeles' time zone assigned. - Apply
tz
timezone to alldatetime
objects. - Take into consideration possible DST shift.
Thanks for your feedback!
Daylight Savings Time
Now we know how to set and adjust timezone to certain datetime
objects. But can we handle daylight saving time in Python?
If you work with data containing dates and times up to minutes, it can be a bit dangerous. Of course, we can manually add (or subtract) one hour from time. But there is no touchpoint in this question among countries. For example, the majority of Asian, African, and South American countries do not set their clock back (or forward) by one hour, while most European countries and US states do it. But even among them, there are differences: for example, Americans change their clocks on the second Sunday in March at 02:00, while European countries do it on the Last Sunday in March at 02:00 UTC. Here pytz
will help us one more time. To take into consideration possible DST shift use .normalize
method on timezone
object with timedelta
object with timezone
set as an argument. For example, in 2021 Ukraine set its clocks forward by one hour on 28 March at 03:00 (local time). So, it should be impossible to have 03:30 time on this date (since 03:00 became 04:00). Let's see how it works:
# Load libraries and classes import pytz from datetime import datetime from pytz import timezone tz = timezone('Europe/Kiev') # timezone object dt = datetime(2021, 3, 28, 3, 35) # datetime object # Set timezone to datetime dt_tz = tz.localize(dt) dt_tz_norm = tz.normalize(dt_tz) # adjust time to DST # Testing print(f"Original datetime: {dt_tz}") print(f"Normalized datetime: {dt_tz_norm}")
There is only one correct answer when we are talking about the 'spring forward' shift. But if we are talking about 'fall back' shift there is no correct answer, since one time happens twice. For example, in 2021 Ukraine set its clocks back by one hour on 31 October at 04:00 (local time). So, there was, for example, 03:30 happened twice (before and after setting clocks).
Task
Given three datetime
objects: dt1
, dt2
, and dt3
.
- Create
timezone
objecttz
with 'America/Los_Angeles' time zone assigned. - Apply
tz
timezone to alldatetime
objects. - Take into consideration possible DST shift.
Thanks for your feedback!
Daylight Savings Time
Now we know how to set and adjust timezone to certain datetime
objects. But can we handle daylight saving time in Python?
If you work with data containing dates and times up to minutes, it can be a bit dangerous. Of course, we can manually add (or subtract) one hour from time. But there is no touchpoint in this question among countries. For example, the majority of Asian, African, and South American countries do not set their clock back (or forward) by one hour, while most European countries and US states do it. But even among them, there are differences: for example, Americans change their clocks on the second Sunday in March at 02:00, while European countries do it on the Last Sunday in March at 02:00 UTC. Here pytz
will help us one more time. To take into consideration possible DST shift use .normalize
method on timezone
object with timedelta
object with timezone
set as an argument. For example, in 2021 Ukraine set its clocks forward by one hour on 28 March at 03:00 (local time). So, it should be impossible to have 03:30 time on this date (since 03:00 became 04:00). Let's see how it works:
# Load libraries and classes import pytz from datetime import datetime from pytz import timezone tz = timezone('Europe/Kiev') # timezone object dt = datetime(2021, 3, 28, 3, 35) # datetime object # Set timezone to datetime dt_tz = tz.localize(dt) dt_tz_norm = tz.normalize(dt_tz) # adjust time to DST # Testing print(f"Original datetime: {dt_tz}") print(f"Normalized datetime: {dt_tz_norm}")
There is only one correct answer when we are talking about the 'spring forward' shift. But if we are talking about 'fall back' shift there is no correct answer, since one time happens twice. For example, in 2021 Ukraine set its clocks back by one hour on 31 October at 04:00 (local time). So, there was, for example, 03:30 happened twice (before and after setting clocks).
Task
Given three datetime
objects: dt1
, dt2
, and dt3
.
- Create
timezone
objecttz
with 'America/Los_Angeles' time zone assigned. - Apply
tz
timezone to alldatetime
objects. - Take into consideration possible DST shift.
Thanks for your feedback!
Now we know how to set and adjust timezone to certain datetime
objects. But can we handle daylight saving time in Python?
If you work with data containing dates and times up to minutes, it can be a bit dangerous. Of course, we can manually add (or subtract) one hour from time. But there is no touchpoint in this question among countries. For example, the majority of Asian, African, and South American countries do not set their clock back (or forward) by one hour, while most European countries and US states do it. But even among them, there are differences: for example, Americans change their clocks on the second Sunday in March at 02:00, while European countries do it on the Last Sunday in March at 02:00 UTC. Here pytz
will help us one more time. To take into consideration possible DST shift use .normalize
method on timezone
object with timedelta
object with timezone
set as an argument. For example, in 2021 Ukraine set its clocks forward by one hour on 28 March at 03:00 (local time). So, it should be impossible to have 03:30 time on this date (since 03:00 became 04:00). Let's see how it works:
# Load libraries and classes import pytz from datetime import datetime from pytz import timezone tz = timezone('Europe/Kiev') # timezone object dt = datetime(2021, 3, 28, 3, 35) # datetime object # Set timezone to datetime dt_tz = tz.localize(dt) dt_tz_norm = tz.normalize(dt_tz) # adjust time to DST # Testing print(f"Original datetime: {dt_tz}") print(f"Normalized datetime: {dt_tz_norm}")
There is only one correct answer when we are talking about the 'spring forward' shift. But if we are talking about 'fall back' shift there is no correct answer, since one time happens twice. For example, in 2021 Ukraine set its clocks back by one hour on 31 October at 04:00 (local time). So, there was, for example, 03:30 happened twice (before and after setting clocks).
Task
Given three datetime
objects: dt1
, dt2
, and dt3
.
- Create
timezone
objecttz
with 'America/Los_Angeles' time zone assigned. - Apply
tz
timezone to alldatetime
objects. - Take into consideration possible DST shift.