moment icon indicating copy to clipboard operation
moment copied to clipboard

Conflict timezone results with datetime

Open veictry opened this issue 3 years ago • 4 comments

(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 avatar Mar 14 '22 19:03 veictry

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 avatar Mar 14 '22 19:03 veictry

Please take a look if free. Thanks!

veictry avatar Mar 14 '22 19:03 veictry

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

zachwill avatar Mar 14 '22 22:03 zachwill

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.

veictry avatar Mar 15 '22 02:03 veictry