Function to parse gs.getSession().getTimeZone();

G24
Kilo Sage

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.

1 ACCEPTED SOLUTION

karthiknagaramu
Kilo Sage

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

View solution in original post

11 REPLIES 11

Bruce Doiron
Tera Contributor

Found this after I had to figure out a solution for my own use case (business requirement).
I'll share it in case it can help other users that live in a area that follows Daylight Savings (like I currently do). I made this solution into a function that I'll be reusing but here's the code.

var diffFromUTC = getTimeZoneAdj();
gs.info(diffFromUTC);

function getTimeZoneAdj(){
	var userTimeZone = gs.getSession().getTimeZone();
	var userTimeZoneString = "Current User's Time Zone: " + userTimeZone; // Converts the TimeZone OBJECT to a string.
	var match1 = userTimeZoneString.match(/offset=([^,]+)/);
	var match2 = userTimeZoneString.match(/dstSavings=([^,]+)/);

	if (match1) {
	var offsetValue1 = Number(match1[1]);
	}

	if (match2) {
	var dstSavingsValue2 = Number(match2[1]);
	}

	var correctTimeZoneAdj = Math.abs((offsetValue1 + dstSavingsValue2) / 60 / 60 / 1000) ; // Return the absolute value of hours difference between UTC and either daylight savings or standard time 
	return correctTimeZoneAdj ;
}

 

benn23
ServiceNow Employee
ServiceNow Employee

var tz = gs.getSession().getTimeZone();

var thgArr = tz.split(',');
var id = thgArr[0].substring(thgArr[0].indexOf('id=') + 3,thgArr[0].length);
var offset = thgArr[1].substring(thgArr[1].indexOf('offset=') + 7,thgArr[1].length);
gs.print('id = ' + id + ' offset = ' + offset);

 

@benn23 That looks pretty fancy / tight.  Thank you.

But something about it is not working for me.  I get no results.

benn23
ServiceNow Employee
ServiceNow Employee

Sorry, just need to convert that first line to a string.  This should work.

var tz = gs.getSession().getTimeZone() + '';

var thgArr = tz.split(',');
var id = thgArr[0].substring(thgArr[0].indexOf('id=') + 3,thgArr[0].length);
var offset = thgArr[1].substring(thgArr[1].indexOf('offset=') + 7,thgArr[1].length);
gs.print('id = ' + id + ' offset = ' + offset);

Jim Coyne
Kilo Patron

Here's a more accurate method:

gs.print((gs.getSession().getTimeZone()) + "");

var tzName = gs.getSession().getTimeZoneName();
var tzOffset = new GlideDateTime().getTZOffset();

gs.print(tzName);
gs.print(tzOffset);

 

This is the output:

JimCoyne_0-1693538034368.png

 

If you use "getTimeZone", you have to parse the results like you did, which is always dicy.  The other problem is you then need to figure out if DST comes into play or not and add the "dstSavings" to the "offset" time to get the true current offset, which means more parsing.  That's hoping to get 4 bits of info properly.  -18000000 + 3600000 = -14400000, the true offset, which "getTZOffset" gives you.