Home > Blockchain >  Why timezone-aware datetime is different when running program on MacOS and on Ubuntu?
Why timezone-aware datetime is different when running program on MacOS and on Ubuntu?

Time:12-27

The below code produces different results when it is running on MacOS and on Ubuntu (AWS Ubuntu 2.0 EC2 instance)

import datetime
import pytz
dt = datetime.datetime(year=2020, month=12, day=3, hour=8, minute=30)
dt_aware = dt.astimezone(pytz.timezone('Europe/Paris'))
print(dt_aware)

On MacOS BigSur 11.6, it returns: 2020-12-03 00:30:00 01:00
On Ubuntu 2.0, it returns: 2020-12-03 09:30:00 01:00.

In both cases:

  • Python 3.8 is used
  • pytz version 2020.4 is used

What is the reason which explains that results are different?


EDIT: I want to represent '2020/12/03 08:30' as being the datetime in Paris. So I exepect both returning 2020-12-03 08:30:00 01:00

CodePudding user response:

This is probably because the AWS instance is not in the same timezone as your MacOS Machine. When you create a dateTime without a specified timezone it will use a default timezone based on your machine/OS.

Then you adjust it to Europe Paris and it changes from the default timezone to that.

So the AWS instance is probably in UTC and differs only by one from Paris, so the 8 becomes a 9.

Your MacOS is probably somewhere in the US (Pacific?) and is off by 8 hours from UTC and 9 from Paris. So when set to Paris it ends up with a 9 hour total difference.

  • Related