
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2023 05:25 AM - edited 08-31-2023 05:28 AM
Hello. Does someone have a function to parse the ZoneInfo array (?) or whatever it is that is returned from:
var userTimeZone = gs.getSession().getTimeZone();
The output from that looks like this:
sun.util.calendar.ZoneInfo[id="US/Eastern",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Eastern,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]
I care about the id, "US/Eastern" in this example...
and the "offset", -18000000 in this example.
*** OR ***
Is there a better way to get the name/id and the offset?
I know that I can get the name with a different function:
var userTimeZoneName = session.getTimeZoneName();
But I don't know the best way to get the offset. Thanks.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2023 06:10 AM
Hi,
Using string manipulation we can get the offset. But might not be a best way.
var userTimeZone = gs.getSession().getTimeZone()+'';
gs.print(userTimeZone);
userTimeZone = userTimeZone.replace('sun.util.calendar.ZoneInfo','');
gs.print(userTimeZone);
var tmArr = [];
tmArr = userTimeZone.split(",");
gs.print(tmArr[1]);
var offset = tmArr[1]+'';
offset = offset.replace('offset=','');
gs.print(offset);
Regards,
Karthik Nagaramu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-27-2025 03:11 PM
Here is a much more elegant solution to the problem I had to solve myself. The idea is to create a JSON associative array from the string so you can access all the values by key.
var timestamp = "2007-11-08 10:48:38"; // a give timestamp string
var gDT = new GlideDateTime(timestamp); // create a GlideDateTime object
var tz = gDT.getUserTimeZone(); // same as gs.getSession().getTimeZone()
tz = String(tz); // cast as a string
// Create an array with the list of regular expressions for substitution
var expReg = [
"sun.util.calendar.ZoneInfo", // Match the literal string.
"java.util.SimpleTimeZone", // Match the literal string.
"(\\w+) *=", // This will put the value of the key in the group 1.
": *([\\w@\\.\\/]+)", // This will put the value in the group 1.
"\\\"(-*\\d+)\\\"", // This will match an integer and return it into the group 1.
"\\[", // Match the opening square bracket.
"\\]", // Match the closing square bracket.
"\\\"true\\\"", // Match true with double quotes on each side.
"\\\"false\\\"" // Match false with double quotes on each side.
];
// Create an array with the replacing value
var resultat = [
"", // Remove the matched value.
"", // Remove the matched value.
"\"$1\":", // Insert the group 1 value surrounded by double quotes and add the suffix : (this actually replace the = by :).
":\"$1\"", // Insert the group 1 value matched and prefix with :.
"$1", // Insert the group 1 value matched.
"{", // Insert an opening curly brace.
"}", // Insert a closing curly brace.
"true", // Insert the value true without any quotes.
"false" // Insert the value false without any quotes.
];
// Loop for each value in the regular expressions array (expReg).
for ( i=0; i<expReg.length; i++ ) {
// Replace using the compiled regular expression globally ('g') with the equivalent result from the resultat array
tz = tz.replace(new RegExp(expReg[i], 'g'), resultat[i]);
}
// Parse and transform the string into a JSON structure
tz = JSON.parse(tz);
// Access the values by key
gs.info("\nTimezone name: " + tz.id + "\nOffset of the timezone in hours: " + tz.offset / 3600000 + "\nCheck if using daylight savings time: " + tz.useDaylight);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday - last edited yesterday
Isn't...
var tzOffsetMilliseconds = new GlideDateTime().getTZOffset();
var tzOffsetSeconds = (new GlideDateTime().getTZOffset()) / 1000;
var tzOffsetMinutes = (new GlideDateTime().getTZOffset()) / 1000 / 60;
var tzOffsetHours = (new GlideDateTime().getTZOffset()) / 1000 / 60 / 60;
... the easiest and most accurate way of doing it???
I replied here with that over 2 years ago and people keep coming up with ways to decipher/parse the results of "getUserTimeZone()" for some reason. 🤔