original_value = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = original_value..strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_value = datetime.datetime.strptime(current_schedule,"%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
Now theoretically original_value.timestamp() should be equal to new_value variable, but one will notice a time difference of equivalent or at least close to time zone their machine is in.
For me it was around 17000 seconds or 19000 seconds, That was also not consistent.
How to consistently find the difference between both of them to be 0 seconds as ideally they should be.
Function I used:
import datetime
import pytz
ab = (datetime.datetime.now(pytz.utc)) + datetime.timedelta(minutes=30)
current_schedule = ab.strftime("%Y-%m-%dT%H:%M:%S.%fZ")
new_time = datetime.datetime.strptime(current_schedule,"%Y-%m-%dT%H:%M:%S.%fZ").timestamp()
print("Value Initially: {}".format(ab.timestamp()))
print("Value post converstion: {}".format(new_time))
time_diff = new_time - ab.timestamp()
print(time_diff)
Output:
Value Initially: 1680806908.778667
Value post converstion: 1680787108.778667
-19800.0
Please explain why this happened? Post time should be same as pre time as they are same only being transformed from one to other!
How to fix this issue?
abhas time with timezone data where aswon't be having the timezone details which make it to use your local timezone as the reference while calling timestamp method. So,
ab.timestamp()will be in UTC and the value new_time will be in local timezone. The difference you get will be the difference between the local timezone and UTC in seconds.To resolve this, specify the timezone before using
timestamp(). This can be done either by usingreplace(tzinfo=pytz.UTC)or better use the format"%Y-%m-%dT%H:%M:%S.%f%z"in bothstrftimeandstrptime.