0

I want to show expiry dates for SSL certificates to the user. These expiry dates are in UTC, so when the expiry date is today at noon, it will show 12/08/2020 12:00:00. However, since I am in the Berlin timezone, that means the certificate will actually expire at 14:00:00 localtime, which is what I want to show to the user. I tried the following:

end_date = certificate_end_date.replace(tzinfo=timezone.utc).astimezone(tz=None)

But since the certificate is valid until 2045, this produces the following error message:

OverflowError: timestamp out of range for platform time_t

I searched and people suggested just using a timedelta, but that is complicated again due to daylight savings time. There has to be a proper way to do this? I am using Python3.7.

Gasp0de
  • 1,199
  • 2
  • 12
  • 30
  • what is `certificate_end_date`? a datetime object? what is its `repr`? – FObersteiner Aug 18 '20 at 18:09
  • Yes, it's a datetime object. I don't understand exactly what you mean by what its `repr` is. If I print it, it's just the datetime without any timezone information: `>>> certificate_end_date datetime.datetime(2045, 1, 1, 12, 0) >>> print(certificate_end_date) 2045-01-01 12:00:00 ` – Gasp0de Aug 19 '20 at 10:05
  • by `repr` I mean [this](https://stackoverflow.com/questions/7784148/understanding-repr-function-in-python) - the first part of what you posted in the comment ;-) I think your line of code is perfectly fine, however I'm confused that it throws this error. On my system, `datetime(2045, 1, 1, 12, 0).replace(tzinfo=timezone.utc).astimezone()` works fine... (Python 3.8.5 x64 on Windows 10). – FObersteiner Aug 19 '20 at 10:33

1 Answers1

0

I solved it by calculating the timedelta as recommended elsewhere, although it still looks kind of ugly to me:

 from datetime import datetime, timezone

 now = datetime.now()
 offset = now.replace(tzinfo=timezone.utc) - now.astimezone(timezone.utc)

 localtime = utc_time + offset
Gasp0de
  • 1,199
  • 2
  • 12
  • 30
  • are you looking for your local time's UTC offset? you can obtain that via `datetime.now().astimezone().utcoffset()` – FObersteiner Aug 18 '20 at 19:17
  • Yes this is basically what I was looking for, however I thought there was a way to tell python that this datetime object is in UTC and that I want it to be shown in localtime (i.e. not adding the offset manually before returning it to the user). – Gasp0de Aug 19 '20 at 10:02
  • ok you could create an aware datetime object then as `dt = datetime.now(tz=timezone.utc)`. if you want to show (print) its local time representation, use `print(dt.astimezone())`. the thing with Python is that it treats naive datetime objects as local time while in many other languages, it's UTC. Kind of a pitfall. I suggest to define the time zone whenever you can. – FObersteiner Aug 19 '20 at 10:28