Skip to content

Wrong hour in timezone "Africa/Cairo" from 2023 #706

Description

@psyking841
  • OS version and name: OSX 13.3.1 (22E261)
  • Pendulum version: 2.1.2

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:

>>> dt = pendulum.datetime(2023, 4, 27, 21)
>>> dt
DateTime(2023, 4, 27, 21, 0, 0, tzinfo=Timezone('UTC'))
>>> dt.in_timezone('Africa/Cairo')
DateTime(2023, 4, 27, 23, 0, 0, tzinfo=Timezone('Africa/Cairo'))

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.

Activity

NickFabry commented on May 5, 2023

@NickFabry
Collaborator

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

psyking841 commented on May 17, 2023

@psyking841
Author

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.

edgarrmondragon commented on Dec 14, 2023

@edgarrmondragon
Contributor

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'))

Secrus commented on Dec 17, 2023

@Secrus
Collaborator

Fixed in Pendulum 3.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions