Skip to content
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

Conflict timezone results with datetime #46

Open
veictry opened this issue Mar 14, 2022 · 4 comments
Open

Conflict timezone results with datetime #46

veictry opened this issue Mar 14, 2022 · 4 comments

Comments

@veictry
Copy link

veictry commented Mar 14, 2022

(venv) wangchengjie@wangchengjies-iMac coverity % python --version   
Python 3.6.8

The below code produced conflict results when I compared it with datetime

    from moment import Moment, now
    a = now()
    print(a)
    a.timezone('America/Los_Angeles')
    print(a)
    from datetime import datetime
    from dateutil import tz, zoneinfo
    tz_pst = tz.gettz('America/Los_Angeles')
    print(datetime.now())
    d = datetime.now(tz=tz_pst)
    print(d)

output:

2022-03-15T03:30:45+08.00
2022-03-14T20:30:45+08.00
2022-03-15 03:30:45.220210
2022-03-14 12:30:45.220222-07:00
@veictry
Copy link
Author

veictry commented Mar 14, 2022

from core.py

    def __str__(self):
        formatted = self._date.strftime("%Y-%m-%dT%H:%M:%S")
        tz = str.format("{0:+06.2f}", -float(timezone) / 3600)
        return formatted + tz

I'm not clear about why using constant VAR timezone which is from the module package .
I thought that it may need to changed from the timezone info

@veictry
Copy link
Author

veictry commented Mar 14, 2022

Please take a look if free. Thanks!

@zachwill
Copy link
Owner

Good catch -- I'm pretty sure this has to do with Daylight Saving Time going into effect this past weekend. I'm getting a similar error to your code:

from datetime import datetime
from dateutil import tz, zoneinfo
import moment

a = moment.utcnow()
print(a)
a.timezone('America/Los_Angeles')
print(a)

tz_pst = tz.gettz('America/Los_Angeles')
print(datetime.now())
d = datetime.now(tz=tz_pst)
print(d)

And the output is indeed wrong (-08.00 vs -07.00):

2022-03-14T22:20:11-08.00
2022-03-14T15:20:11-08.00
2022-03-14 15:20:11.095389
2022-03-14 15:20:11.095418-07:00

@veictry
Copy link
Author

veictry commented Mar 15, 2022

Thanks for your reply and confirmation.

Yes. I thought that the wrong timezone info in Moment may be caused by using the hard code float(timezone)

If possible, we may need to get the real timezone from self._date.

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