- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-20-2021 09:22 AM
Update from ServiceNow at the bottom of this article
There appears to be an issue with GlideTime, at least when used in conjunction with GlideDateTime
When you run the following script, the GlideTime object is instantiated with the local time of the user, however to access the local hour you cannot use the getHourLocalTime / getHourOfDayLocalTime methods, you should use the getHourUTC / getHourOfDayUITC methods instead, or the getByFormat method.
var gdt = new GlideDateTime();
gs.info('The date & time in my User Timezone (' + gs.getSession().getTimeZoneName() + ') right now is: ' + gdt.getDisplayValue() + '\n');
gs.info('If we get a GlideTime object by calling the GlideDateTime.getLocalTime method, the methods return as follows:');
var gtFromGDT = gdt.getLocalTime();
gs.info('The GlideTime.getHourOfDayLocalTime method returns ' + gtFromGDT.getHourOfDayLocalTime() + ' which IS NOT the actual local hour');
gs.info('However, the GlideTime.getHourOfDayUTC method returns ' + gtFromGDT.getHourOfDayUTC() + ' which IS the actual local hour');
gs.info('And finally the GlideTime.getByFormat method returns ' + gtFromGDT.getByFormat('HH') + ' which IS the actual local hour\n');
var gt = new GlideTime();
gs.info('If we intantiate a GlideTime by itself without using GlideDateTime, the methods return as follows:');
gs.info('The GlideTime.getHourOfDayLocalTime method returns ' + gt.getHourOfDayLocalTime() + ' which IS the actual local hour');
gs.info('The GlideTime.getHourOfDayUTC method returns ' + gt.getHourOfDayUTC() + ' which IS NOT the actual local hour');
gs.info('And finally the GlideTime.getByFormat method returns ' + gt.getByFormat('HH') + ' which IS NOT the actual local hour');
The output of this script is as follows
*** Script: The date & time in my User Timezone (IST) right now is: 20/02/2021 21:51:25
*** Script: If we get a GlideTime object by calling the GlideDateTime.getLocalTime method, the methods return as follows:
*** Script: The GlideTime.getHourOfDayLocalTime method returns 3 which IS NOT the actual local hour
*** Script: However, the GlideTime.getHourOfDayUTC method returns 21 which IS the actual local hour
*** Script: And finally the GlideTime.getByFormat method returns 21 which IS the actual local hour
*** Script: If we intantiate a GlideTime by itself without using GlideDateTime, the methods return as follows:
*** Script: The GlideTime.getHourOfDayLocalTime method returns 21 which IS the actual local hour
*** Script: The GlideTime.getHourOfDayUTC method returns 16 which IS NOT the actual local hour
*** Script: And finally the GlideTime.getByFormat method returns 16 which IS NOT the actual local hour
It seems as though GlideDateTime.getLocalTime simply does something internally analogous to the following script
var gt = new GlideTime();
gt.setValue('**local_time_from_gdt**');
instead of the following which would instantiate the GlideTime correctly.
var gt = new GlideTime();
gt.setDisplayValue('**local_time_from_gdt**');
In summary, if you are getting your GlideTime object from a method on GlideDateTime, do not use the local time (display value) methods, just use the UTC (value) methods.
I have raised with with ServiceNow support and I will update this post with their response when I receive it.
UPDATE: ServiceNow are investigating this. For the time being I'd suggest using the getTime method of GlideDateTime which does produce a correctly formed GlideTime object.
UPDATE FROM SERVICENOW:
On discussing this with ServiceNow, they have advised against the use of the getLocalTime method as it would appear to serve no useful purpose. Instead developers should use the getTime method which will present you with a GlideTime object that works as one would expect.
The development team have been instructed to update the documentation on the developer site to make this as clear as possible to developers, however no timeframe has been given on when this will take place.
Callum
- 518 Views