Do you also get the actual "unix time". It's a number. With that it's easy to get the time in your current timezone.
info=Time.Gregorian.info(unixTime,Time.FORMAT_SHORT);
The time is returned from the remote server like 2021-05-03T14:47:30.810Z. How to convert it to the timestamp?
I think I can construct an options dictionary and format it with the method info. But it is not an elegant way.
// Options for Saturday February 24th, 2018 12:12am
var options = {
:year => 2018,
:month => 2,
:day => 24,
:hour => 0,
:minute => 12
};
var now = Gregorian.moment(options);
var info;
info = Gregorian.utcInfo(now, Time.FORMAT_SHORT);
hi, may be this could be usefull: developer.garmin.com/.../ClockTime.html
What does the data look like that you get back from the web request? Are you use you only get a string for the time? In one case I know of the data is actually labels "dt".
In another, I get this:
obsTimeUtc=>2021-05-03T16:30:00Z
but also this:
epoch=>1620059400
You do get any numbers around what I posted for epoch?
Yeah I would personally use ClockTime with timeZoneOffset
As Shent-Aurkhan (SHN) implied, you can get the current timezone offset via:
System.getClockTime().timeZoneOffset;
developer.garmin.com/.../ClockTime.html
However, that's only the offset for *today*. Offsets for other days could be different due to daylight savings time.
You should also *hypothetically* be able to use Gregorian info to convert between UTC and local, but there's a bug where the tz offset for conversion comes from the current date and not the date passed to Gregorian moment, which means the conversion could be 1 hour off due to daylight savings time.
Also note that Gregorian.moment takes local time as input, not UTC, despite what the docs say.
In other words, I don't think there's a perfect way to convert between local and UTC for dates other than today.
Actually, if you use "unix time", with Gregorian it works quite well,
"unix time" is the number of seconds since jan1 1970, 00:00 UTC - above it's 1620059400.
With
info=Time.Gregorian.info(unixTime,Time.FORMAT_SHORT);
info contains the time in localtime. Much easier than trying to parse strings, handle +/- UTC offset and dates, DST, etc.
The only question is if the data includes "unix time" in addition to the formatted string. The interfaces I've used all do.
Missed a line of code un my copy/paste:
unixTime=new Time.Moment(unixTime);
info=Calendar.info(unixTime, Time.FORMAT_SHORT);
Thanks I know what "unix time" is. That doesn't change the fact that the conversion between UTC and local is not handled correctly for certain dates other than today due to the handling of daylight savings time, as referenced in the bug report above. (Assuming the user's locale supports daylight savings time)
But that gives you the timeZoneOffset for today, not the date received by the web service (I'm assuming it's not always today).
As pointed out in the thread, if you have a Moment, you can use Gregorian.info to automatically convert it to local time :
var moment = Gregorian.moment(...); // input is UTC (time broken down into separate fields)
// or
var moment = new Time.Moment(...); // input is UTC (seconds since the unix epoch)
var info = Gregorian.info(moment , Time.FORMAT_SHORT); // info is in local time
But due to the following bug, the timezone conversion may not correctly account for DST depending on whether the input is today or some day other than today:
However, using Gregorian.info still saves you from having to do the math yourself.