- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 08-05-2017 03:29 PM
This relates to glide-istanbul-09-23-2016__patch7-06-13-2017. Possibly other versions.
addDays() is a legacy function and should not be used and the documentation says "Use addDaysLocalTime() and addDaysUTC() instead of this method."
( https://developer.servicenow.com/app.do#!/api_doc?v=istanbul&id=r_GDT-addDays_N )
However there is a lot of existing code base and examples on the internet that use addDays() so this might be useful info to anyone that still has scripts with addDays() and has not changed them - yet.
I've been scratching my head why an old script using the legacy GlideDateTime addDays() function appeared to be correctly computing Daylight Savings even when the session timezone is was set to GMT (no Daylight Savings).
After some investigation it seems that the legacy addDays() appears to always compute using American daylight savings , ignoring the time zone encoded in the GlideDateTime object.
So dates between 12th March 2017 and 26th March 2017 are computed with US Daylight Savings even though this does not apply in the UK for example.
The following Script should illustrate the issue. It creates a GlideDateTime with London time zone, then starting from Midday 6th of March it repeatedly adds 7 days and displays the results.
On 20th March (which was not yet UK Daylight Savings) it will display 11:00 instead of the expected 12:00.
The output is below: The lines in red are an hour out.
*** Script: loop [0] gmt:[2017-03-06 12:00:00] Europe/London:[2017-03-06 12:00:00] unix:[1488801600]DST:[false]
*** Script: loop [1] gmt:[2017-03-13 11:00:00] Europe/London:[2017-03-13 11:00:00] unix:[1489402800]DST:[false]
*** Script: loop [2] gmt:[2017-03-20 11:00:00] Europe/London:[2017-03-20 11:00:00] unix:[1490007600]DST:[false]
*** Script: loop [3] gmt:[2017-03-27 11:00:00] Europe/London:[2017-03-27 12:00:00] unix:[1490612400]DST:[true]
*** Script: loop [4] gmt:[2017-04-03 11:00:00] Europe/London:[2017-04-03 12:00:00] unix:[1491217200]DST:[true]
*** Script: loop [5] gmt:[2017-04-10 11:00:00] Europe/London:[2017-04-10 12:00:00] unix:[1491822000]DST:[true]
The script:
(function(){
// Script to show legacy addDays is using US daylight savings and ignoring the time zone in the date object
// US summer time : 2AM Sunday 12th March 2017 - 5th November
// UK summer time : 1AM Sunday 26th March 2017 - 29th October
var testTZName = 'Europe/London';
var testTZ = Packages.java.util.TimeZone.getTimeZone(testTZName);
gs.log("UserTZ:"+gs.getUser().getTZ());
gs.log("SessionTZ:"+gs.getSession().getTimeZone());
var testDate = "2017-03-06 12:00";
var gdt = new GlideDateTime();
gdt.setTZ(testTZ);
gdt.setValue(testDate);
for( i = 0 ; i < 6 ; i++ ) {
gs.log("loop ["+i+"] "+
"gmt:["+gdt+"] "+
testTZName+":["+gdt.getDisplayValue()+"] "+
"unix:["+(gdt.getNumericValue()/1000)+"]"+
"DST:["+gdt.isDST()+"]");
gdt.addDays(7);
}
})();
- 1,139 Views