I wrote this script to capture the current time from the server but forgot to take into account Daylight Savings Time (DST). The server outputs the time in GMT, so after a few shenanigans I was able to create a cleaner output. The problem is that it's giving me the time two hours behind my time zone in Rome, Italy.
I'm currently using timedelta(hours=1)
, and if I change hours=1
to hours=2
I get the corret time, but I was wondering if anyone knows an easy, hassle-free way to automate this, so I won't have to change this twice a year.
Here is what I have so far:
This is how the timestamp comes out of the server:
logstash_firewall_timestamp = '2023-04-11T14:37:27.840Z'
Here is where I convert it and have been adding one hour:
import datetime
from datetime import datetime,timedelta
pattern = '%Y-%m-%dT%H:%M:%S.%fZ'
dt = datetime.strptime(logstash_firewall_timestamp, pattern)
new_timestamp = str(dt + timedelta(hours=1))[:-3] + 'Z'
And here is a final cleanup:
formatted_time = (''.join(letter for letter in new_timestamp if not letter.isalpha()))[:19]
To summarize:
GMT Time:
2023-04-11 14:37:27
Current Script Output:
2023-04-11 15:37:27
Intended Output:
2023-04-11 16:37:27