Repository navigation
Wrong hour in timezone "Africa/Cairo" from 2023 #706
Description
Activity
This is caused by pendulum release 2.1.2 referencing (now) obsolete IANA zoneinfo, specifically 2022.1. As a temporary fix, to force pendulum 2.1.2 to use either the system zoneinfo, or (on Windows), the zoneinfo from the first party module tzdata, put the following into your python import path as (for example) pendulum_212_fix.py, and after you import pendulum, add the line import pendulum_212_fix, and you should be good.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This module has one purpose - to fix the behavior of pendulum release
2.1.2, which uses a hardcoded version of the IANA zoneinfo data, 2022.2.
Importing this module after importing pendulum should cause pendulum to
use either the OS provided zoneinfo data, or if that does not exist, to
use the zoneinfo from the first party module tzdata. Note this introduces
a dependence on tzdata, which must be separetely installed, typically via
pip install tzdata.
In theory, IMMEDIATELY AFTER IMPORTING pendulum, you should simply
import this module and pendulum will then use the local system
zoneinfo, if available, and if not, the zoneinfo from tzdata, when
creating aware datetimes.
"""
import pathlib
import sysconfig
import tzdata
LOCAL_TZPATH = sysconfig.get_config_var('TZPATH')
if LOCAL_TZPATH:
# This could be a colon separated list of paths; we take the first only
LOCAL_TZPATH = LOCAL_TZPATH.split(':')[0]
else:
LOCAL_TZPATH = pathlib.Path(tzdata.__path__) / 'zoneinfo'
import pendulum
import pytzdata
pytzdata.set_directory(LOCAL_TZPATH)
if __name__ == '__main__':
pass
Thanks for the solution! But, I am using the pendulum module built inside Airflow, specifically MWAA in AWS. So, I guess I have to wait for them to make this patch.
I think this is indeed a bug, but the example seems incorrect:
When local standard time was about to reach Friday, April 28, 2023, 12:00:00 midnight clocks were turned forward 1 hour to Friday, April 28, 2023, 1:00:00 am local daylight time instead.
So, Cairo (EET) being normally UTC+2, the correct test case would be 2023-4-27T22:00:00Z:
>>> pendulum.__version__
'2.1.2'
>>> pendulum.datetime(2023, 4, 27, 21).in_timezone('Africa/Cairo')
DateTime(2023, 4, 27, 23, 0, 0, tzinfo=Timezone('Africa/Cairo'))
>>> pendulum.datetime(2023, 4, 27, 22).in_timezone('Africa/Cairo')
DateTime(2023, 4, 28, 0, 0, 0, tzinfo=Timezone('Africa/Cairo')) # Should be 1 A.M.FWIW this does seem to be fixed in Pendulum 3.0.0b1:
>>> pendulum.__version__
'3.0.0b1'
>>> pendulum.datetime(2023, 4, 27, 21).in_timezone('Africa/Cairo')
DateTime(2023, 4, 27, 23, 0, 0, tzinfo=Timezone('Africa/Cairo'))
>>> pendulum.datetime(2023, 4, 27, 22).in_timezone('Africa/Cairo')
DateTime(2023, 4, 28, 1, 0, 0, tzinfo=Timezone('Africa/Cairo'))Fixed in Pendulum 3.0
Issue
In 2023, Cairo has started daylight saving in 2023 on 2023-04-27T21:00:00 UTC.
This 2023-04-27T21:00:00 UTC should be local time 2023-04-28T00:00:00 (Cairo time). However, in pendulumn this is still converted to 2023-04-27T23:00:00 (Cairo time)
Please see the replay below:
Basically, pendumlum is not implementing daylight saving for Africa/Cairo time.
This is related to this issue: #700 where Mexico has made the similar decision in 2023.