# End of Day timestamp with Daylight Saving transitions

**URL:** <https://forum.inductiveautomation.com/t/end-of-day-timestamp-with-daylight-saving-transitions/20080>\
**Category:** General Discussion\
**Tags:** scripting\
**Created:** [October 11, 2018, 1:39am UTC](https://forum.inductiveautomation.com/t/end-of-day-timestamp-with-daylight-saving-transitions/20080 "2018-10-11T01:39:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![huon.kendall](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@huon.kendall](https://forum.inductiveautomation.com/u/huon.kendall)\
**Post date:** [October 11, 2018, 1:39am UTC](https://forum.inductiveautomation.com/t/end-of-day-timestamp-with-daylight-saving-transitions/20080/1 "2018-10-11T01:39:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 11, 2018, 1:54am UTC](https://forum.inductiveautomation.com/t/end-of-day-timestamp-with-daylight-saving-transitions/20080/2 "2018-10-11T01:54:19Z")

</div>

I would use something like this:

```auto
from java.util import Date
def getEndOfDay(input_ts):
	beginning = Date(input_ts.year, input_ts.month, input_ts.date, 0, 0, 0)
	return system.date.addDays(beginning, 1)

```

Note that using SQL comparisons of the form t\_stamp \<= ‘… 23:59:59’ is suboptimal. If your DB stores fractional seconds, you will omit records that occur in that last second. The function above returns the beginning of the next day as the end of the given day, as that is always safe to use with a pure ‘less-than’ comparison. No matter what resolution your DB uses for timestamps.

---

<div class="post-metadata">

**Author:** ![huon.kendall](https://avatars.discourse-cdn.com/v4/letter/h/7ba0ec/32.png) [@huon.kendall](https://forum.inductiveautomation.com/u/huon.kendall)\
**Post date:** [October 11, 2018, 10:18pm UTC](https://forum.inductiveautomation.com/t/end-of-day-timestamp-with-daylight-saving-transitions/20080/3 "2018-10-11T22:18:01Z")

</div>

Thank you Phil, I really appreciate all your valued posts to the Forum
