1

I'm writing a backend web API in Python. It the database, I have a datetime value, expressed in UTC (example: 2020-12-04 18:50+00:00), which represents the date/time that an event happend. In the backend, I want to convert this to local time and return that to the UI.

The UI has included in the request headers the browser's current timezone, expressed as a string containing the timezone name (in this example, it's "PDT"), as well as its current UTC offset in hours (-7)", which is correct, as it's currently Daylight Saving Time here.

I can convert that date/time to PDT easily by subtracting 7 hours. So, this gives me 2020-12-04 11:50-07:00.

But wait! That event occurred back in December, when it WASN'T Daylight Saving time. So, what I really want to return is 2020-12-04 10:50-08:00, because that represents that time it was in the browser's timezone at the time the "event" occurred. It was actually PST then, not PDT.

So, the answer I'm really trying to get is:

"At 2020-12-04 18:50 UTC, what time was it in the timezone region that 'PDT' represents, accounting for whether or not it was actually Daylight Saving time there on that date".

I suppose this could be broken down into 3 steps/questions:

Q1: What geographic (DST-naive) timezone does "PDT" represent?

A: America/Los_Angeles

Q2: What was the UTC offset in "America/Los_Angeles" on 2020-12-04?

A: UTC-8

Q3: What is 2020-12-04 18:50+00:00 in UTC-8?

A: 2020-12-04 10:50-08:00

Q3 is easy.

How can I achieve Q1 and Q2 using the Python libraries?

(BTW, we're using Python 3.7.12 and can't upgrade right now, so we don't have access to the zoneinfo library)

JoeMjr2
  • 3,804
  • 4
  • 34
  • 62
  • Up-to-date option shown in https://stackoverflow.com/a/63628816/10197418 And maybe an interesting read: https://codeblog.jonskeet.uk/2019/03/27/storing-utc-is-not-a-silver-bullet/ – FObersteiner Jul 12 '22 at 05:56
  • If you're sending `PDT` from the browser, you've got a problem right there. What if you had sent (for example) `CST`? You wouldn't have any way to know if that meant Central Standard Time, China Standard Time, or Cuba Standard Time. There are lots of other ambiguities. Avoid them by getting the time zone ID directly from the browser. See https://stackoverflow.com/a/44935836/634824 – Matt Johnson-Pint Jul 14 '22 at 23:10

1 Answers1

0

I unfortunately cannot add this as a comment. A common solution seems to be to use the pytz package. This answer on a similar thread might help - https://stackoverflow.com/a/55735278/6444294

  • 2
    It won't take you too long until you have sufficient rep ;-) But please suggest an up-to-date option (Python 3.9 standard library: zoneinfo), pytz is deprecated. – FObersteiner Jul 12 '22 at 05:53
  • 1
    @FObersteiner oh wow, I was not aware of that, sorry. However, the OP mentioned they don't have access to zoneinfo, hence my choice to avoid suggesting it. – Bharath Radhakrishnan Jul 12 '22 at 09:48
  • 1
    I'd probably use backports.zoneinfo then, or dateutil, see second part of my [answer here](https://stackoverflow.com/a/63628816/10197418) – FObersteiner Jul 12 '22 at 09:51
  • 1
    see also https://stackoverflow.com/a/62142178/10197418 – FObersteiner Jul 12 '22 at 09:53