-1

The server logs are two hours behind for whatever reason. I'm trying to at 2 hours because of the local time. At the moment, my script outputs in the following format:

2023-06-07 15:10:50+02:00

I actually need it to be:

2023-06-07 17:10:50

Here is the script:

import datetime
import pytz

vcenter_timestamp = '2023-06-07T15:10:50.065397578Z'[:19]
rome_tz = pytz.timezone('Europe/Rome')
pattern = '%Y-%m-%dT%H:%M:%S'
# time in UTC
dt = datetime.datetime.strptime(vcenter_timestamp, pattern)
# convert to Rome time with daylight savings
dt_rome = dt.astimezone(rome_tz)
print(dt_rome)

Current output:

2023-06-07 15:10:50+02:00

Expected output:

2023-06-07 17:10:50
  • Wouldn't be the prettiest solution, but as a temporary fix maybe it's feasible to take that +X:00 value and add that to the hour attribute before printing? – Sand Jun 07 '23 at 17:22
  • Did you look at `strftime`? – Scott Hunter Jun 07 '23 at 17:23
  • 1
    @Sand Hey, thanks for chiming in. Yeah, I tried that last year, but when the daylight savings kicked in, I had to manually change several scripts and will have to continually do it forever. So, I'd rather find a permanent solution. – OverflowStack Jun 07 '23 at 17:25
  • 1
    This is really confusing. In ISO notation the `+02:00` suffix represents the timezone such that the preceding date/time is the **local** time in that timezone. By adding that many hours to the time, you actually create a time that would be local in the `+04:00` timezone. – trincot Jun 07 '23 at 17:27
  • 2
    Right now, it seems your code is taking in a UTC timestamp (hence `Z`), trimming off the timezone designation and parsing in as timezone-naive datetime, and then asserting that that timezone-naive datetime is actually for 'Europe/Rome' (which is wrong, it was UTC). That's different from parsing the UTC timestamp as a timezone-aware UTC datetime and converting that to a different represented timezone, which I think is what you want to do. Does that sound correct? – Brian61354270 Jun 07 '23 at 17:29
  • @trincot Yeah, this is all I could muster. I don't know what and how else to do. Perhaps you have some fresh new idea? – OverflowStack Jun 07 '23 at 17:29
  • When I run it, I get `2023-06-07 17:10:50+02:00` so I cannot reproduce what you claim in your question. – trincot Jun 07 '23 at 17:32
  • @Brian61354270 Yeah sounds about right. What's interesting to me as well is that the server outputs a 9-digit microseconds count, which I hadn't seen before. It's usually 6. But in all honesty, I don't know how to fix this. – OverflowStack Jun 07 '23 at 17:34
  • And I get `2023-06-07 16:10:50+02:00` (based in UK so 1h behind Rome time) – user19077881 Jun 07 '23 at 17:34
  • FYI `datetime.fromisoformat('2023-06-07T15:10:50.065397578Z').astimezone(pytz.timezone('Europe/Rome'))` gives `datetime.datetime(2023, 6, 7, 17, 10, 50, 65397, tzinfo=)` for me, You need to be on at least Python 3.11 for `Z` as an alias for `+00:00` to be supported by `fromisoformat` though – Brian61354270 Jun 07 '23 at 17:35
  • We had a previous attempt at this here: https://stackoverflow.com/a/75997685/765091 Did that run into problems? (I guess from the nanosecond-level timestamp?) – slothrop Jun 07 '23 at 18:06
  • Also, this answer handles nanoseconds: https://stackoverflow.com/a/76419227/765091. I just commented there with a fix for the lack of `datetime.UTC` in Python < 3.11. – slothrop Jun 07 '23 at 19:21

1 Answers1

1

You could try the following:

dt = datetime.datetime.strptime(vcenter_timestamp, pattern)
# convert to Rome time without considering daylight savings
dt_rome = dt.replace(tzinfo=pytz.UTC).astimezone(rome_tz)

# Remove time zone offset from the output
dt_rome = dt_rome.replace(tzinfo=None)

print(dt_rome)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 07 '23 at 19:19
  • It worked! Thank you! – OverflowStack Jun 08 '23 at 13:07