0

I have this code, that returns UTC offset from given date:

>>> import datetime
>>> import pytz
>>> cet = pytz.timezone("Europe/Moscow")
>>> cet.localize(datetime.datetime(2000, 6, 1))
datetime.datetime(2000, 6, 1, 0, 0, tzinfo=<DstTzInfo 'Europe/Moscow' MSD+4:00:00 DST>)
>>> int(cet.localize(datetime.datetime(2000, 6, 1)).utcoffset().seconds/60)
240

Ok, do it in JS using this code ( http://jsfiddle.net/nvn1fef0/ )

new Date(2000, 5, 1).getTimezoneOffset(); // -180

Maybe i doing something wrong? And how i can get plus-minus before offset (like in JS result)?

2 Answers 2

3

On my system both Python and Javascript produce the same result (modulo sign):

>>> from datetime import datetime, timedelta
>>> import pytz
>>> tz = pytz.timezone('Europe/Moscow')
>>> dt = tz.localize(datetime(2000, 6, 1), is_dst=None)
>>> print(dt)
2000-06-01 00:00:00+04:00
>>> dt.utcoffset() // timedelta(minutes=1)
240

And new Date(2000, 6, 1).getTimezoneOffset() returns -240 (different sign, same value).

Python uses: local time = utc time + utc offset definition. While Javascript uses a different definition: utc offset = utc time - local time i.e., both results are correct and have correct signs for the corresponding definitions.

For a portable Javascript solution, you could use momentjs library that provides access to the same tz database as pytz Python module:

> var moscow = moment.tz("2000-06-01", "Europe/Moscow");
undefined
> moscow.format()
"2000-06-01T00:00:00+04:00"
Sign up to request clarification or add additional context in comments.

Comments

0

If you print the result for the following -

print(cet.localize(datetime.datetime(2000, 6, 1)).utcoffset())

You will notice that it gives a datetime.timedelta() object which has both days as well as second.

So for timezones that are UTC - <something> , this actually gives days as -1 and then the remaining in seconds. Example -

In [84]: cet = pytz.timezone("America/Los_Angeles")

In [87]: cet.localize(datetime.datetime(2000, 6, 1)).utcoffset()
Out[87]: datetime.timedelta(-1, 61200)

To get the info about the actual offset , you need to use both days as well as seconds , using a code like (For the above timezone - America/Los_Angeles) -

In [88]: int((cet.localize(datetime.datetime(2000, 6, 1)).utcoffset().days*60*60*24 + cet.localize(datetime.datetime(2000, 6, 1)).utcoffset().seconds)/60)
Out[88]: -420

Also, I believe when you are doing - new Date(2000, 5, 1).getTimezoneOffset(); in javascript, I think it is giving you the timezone offset from UTC for today's date, rather than the date 2000/05/01 (Because for the date - 2000/05/01 the correct offset is what you are getting from python - 240 ) . You may checkout TimezoneJS for getting the timezone specific as well as date specific offsets, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.