Page 1 of 1

Get the Current Timezone?

Posted: 04 Jan 2019 19:33
by piz
Is it possible to get the timezone I'm currently in? I need to convert local times to UTC and I might be in any timezone when that happens. The system knows the current timezone - does Automagic have a way to get it?

Thanks!

Re: Get the Current Timezone?

Posted: 04 Jan 2019 22:30
by anuraag
My timezone is IST.

Code: Select all

 tz="{0, dateformat, hh:mm}" //05:30

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName()"); //India Standard Time

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST

Code: Select all

 tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getID()"); //Asia/Kolkata
To get current time in UTC

Code: Select all

 {getDate(),dateformat,timezone,UTC,HH:mm}"

Re: Get the Current Timezone?

Posted: 05 Jan 2019 06:21
by Desmanto
anuraag wrote:

Code: Select all

 tz="{0, dateformat, hh:mm}" //05:30
Ah, thanks. This is much shorter. If I only need to get the hour, I simply change the hh:mm to h only. But this is risky for timezone that has 30 minutes.

I used to use this

Code: Select all

tz = -getDate("{0,dateformat,z}", "z");
Which will get the miliseconds instead, can be used for add/subtract operation directly. But still have to be divided by 3600000 to get the hour.

Re: Get the Current Timezone?

Posted: 05 Jan 2019 14:51
by piz
Thanks, anuraag, those are perfect, especially this one:
anuraag wrote:

Code: Select all

tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST

Re: Get the Current Timezone?

Posted: 05 Jan 2019 15:01
by anuraag
piz wrote:Thanks, anuraag, those are perfect, especially this one:
anuraag wrote:

Code: Select all

tz=callJavaMethod(callJavaStaticMethod("java.util.TimeZone", "getDefault()"), "java.util.TimeZone", "getDisplayName(boolean, int)", false, getJavaStaticField("java.util.TimeZone", "SHORT")); //IST
Alternative posted by Desmanto If you need that only

Code: Select all

tz = "{0,dateformat,z}" //IST