What is the best way to implement a function to produce the END OF DAY timestamp as “yyyy-MM-dd 23:59:59” for a timestamp that is safe for Daylight Savings transitions ?
I had been using a simple offset from midnight of +24hrs and -1 second as:
eod_ts = system.date.addSeconds(system.date.addHours(system.date.midnight(input_ts),24), -1)
But that breaks for a Date where Daylight savings commences or ends.
So I added more logic to adjust the hours offset as follows:
def get_endOfDay(input_ts):
# check if Daylight Savings transition occurs on input date
amInDls = system.date.isDaylightTime(system.date.midnight(input_ts))
pmInDls = system.date.isDaylightTime(system.date.addSeconds(system.date.addHours(system.date.midnight(input_ts),12), -1))
if not amInDls and pmInDls :
# "Spring forward"
offsetHours = 23
elif amInDls and not pmInDls :
# "Fall back"
offsetHours = 25
else:
offsetHours = 24
return system.date.addSeconds(system.date.addHours(system.date.midnight(input_ts),offsetHours), -1)
It works, but is there a better way to do this?