Skip to content

timezone error in parse_rfc822 #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shortwinds opened this issue May 20, 2025 · 7 comments
Open

timezone error in parse_rfc822 #285

shortwinds opened this issue May 20, 2025 · 7 comments

Comments

@shortwinds
Copy link

shortwinds commented May 20, 2025

Parse datetime string error in parse_rfc822.
parsedate_to_datetime use datetime.timezone.utc in email.utils which depends local settings.

test_code:

from odps.utils import  parse_rfc822
rfc822_str = "Thu, 05 Oct 2023 14:30:00 +0000"
dt = parse_rfc822(rfc822_str)
print(dt)

mac output:

2023-10-05 22:30:00

docker output:

2023-10-06 06:30:00
@wjsi
Copy link
Collaborator

wjsi commented May 20, 2025

The internal utility parse_rfc822 converts string to local time, and this is by-design.

@shortwinds
Copy link
Author

shortwinds commented May 20, 2025

docker:

date +"%Z %z"
> CST +0800

 date
> Tue May 20 17:22:34 CST 2025

mac:

systemsetup -gettimezone
> Time Zone: Asia/Shanghai

 date
> Tue May 20 17:22:07 CST 2025

Mac timezone and docker timezone is same , but parse_rfc822 output is not same.

@shortwinds
Copy link
Author

UTC debug output:
docker_error

zdump -v /usr/share/zoneinfo/UTC | tail
/usr/share/zoneinfo/UTC  Sat Apr 14 17:59:59 1990 UT = Sun Apr 15 01:59:59 1990 CST isdst=0 gmtoff=28800
/usr/share/zoneinfo/UTC  Sat Apr 14 18:00:00 1990 UT = Sun Apr 15 03:00:00 1990 CDT isdst=1 gmtoff=32400
/usr/share/zoneinfo/UTC  Sat Sep 15 16:59:59 1990 UT = Sun Sep 16 01:59:59 1990 CDT isdst=1 gmtoff=32400
/usr/share/zoneinfo/UTC  Sat Sep 15 17:00:00 1990 UT = Sun Sep 16 01:00:00 1990 CST isdst=0 gmtoff=28800
/usr/share/zoneinfo/UTC  Sat Apr 13 17:59:59 1991 UT = Sun Apr 14 01:59:59 1991 CST isdst=0 gmtoff=28800
/usr/share/zoneinfo/UTC  Sat Apr 13 18:00:00 1991 UT = Sun Apr 14 03:00:00 1991 CDT isdst=1 gmtoff=32400
/usr/share/zoneinfo/UTC  Sat Sep 14 16:59:59 1991 UT = Sun Sep 15 01:59:59 1991 CDT isdst=1 gmtoff=32400
/usr/share/zoneinfo/UTC  Sat Sep 14 17:00:00 1991 UT = Sun Sep 15 01:00:00 1991 CST isdst=0 gmtoff=28800
/usr/share/zoneinfo/UTC  9223372036854689407 = NULL
/usr/share/zoneinfo/UTC  9223372036854775807 = NULL

docker_good

usr/share/zoneinfo/UTC  -9223372036854775808 = NULL
/usr/share/zoneinfo/UTC  -9223372036854689408 = NULL
/usr/share/zoneinfo/UTC  9223372036854689407 = NULL
/usr/share/zoneinfo/UTC  9223372036854775807 = NULL

The UTC file is not work well from the above result.
Is there other UTC method which is not depend /etc/localtime? like datetime.timezone.utc?

@wjsi
Copy link
Collaborator

wjsi commented May 20, 2025

Please show results returned by email.utils.parsedate_to_datetime. We does not change what it returns by now.

@shortwinds
Copy link
Author

test_code

from email.utils import parsedate_to_datetime
from datetime import timezone

s = "Thu, 05 Oct 2023 14:30:00 GMT"
dt = parsedate_to_datetime(s)
import zoneinfo

utc = zoneinfo.ZoneInfo("UTC")
dt = dt.astimezone(utc)

print(f"{dt} ({repr(dt.tzinfo)})") 

output

2023-10-05 22:30:00+08:00 (zoneinfo.ZoneInfo(key='UTC'))

utc file in os is not right, then python use the os utc produce the probem

resolve

from email.utils import parsedate_to_datetime
from datetime import timezone

s = "Thu, 05 Oct 2023 14:30:00 GMT"
dt = parsedate_to_datetime(s)
# import zoneinfo

# utc = zoneinfo.ZoneInfo("UTC")
# dt = dt.astimezone(utc)
if dt.tzinfo is not timezone.utc:
    if dt.utcoffset() == timezone.utc.utcoffset(None):
        dt = dt.replace(tzinfo=timezone.utc)

print(f"{dt} ({repr(dt.tzinfo)})") 

output

2023-10-05 14:30:00+00:00 (datetime.timezone.utc)

@wjsi
Copy link
Collaborator

wjsi commented May 20, 2025

Therefore replacing zoneinfo.ZoneInfo("UTC") with datetime.timezone.utc will work, OK?

@shortwinds
Copy link
Author

yes.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants