[CIQBUG] Gregorian.today() returns a moment specified in the UTC timezone

The documentation indicates that the today() is supposed to get a Moment for the beginning of today based on your current GPS location. The below code demonstrates that today() returns a time in the UTC time zone.


using Toybox.System as Sys;
using Toybox.WatchUi as Ui;
using Toybox.Time as Time;
using Toybox.Time.Gregorian as Gregorian;
using Toybox.Graphics as Gfx;

class TestWatchFace extends Ui.WatchFace
{
function initialize() {
}

function onUpdate(dc) {
dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_BLACK);
dc.clear();

var font = Gfx.FONT_LARGE;

var ty = dc.getFontHeight(font);
var cx = dc.getWidth() / 2;
var cy = dc.getHeight() / 2;
cy -= ty;

dc.setColor(Gfx.COLOR_GREEN, Gfx.COLOR_TRANSPARENT);

var times = [
Time.now(),
Time.today(),
Time.today().add(new Time.Duration(-Sys.getClockTime().timeZoneOffset))
];

for (var i = 0; i != times.size(); ++i) {
var time = Gregorian.info(times, Time.FORMAT_SHORT);

Sys.println(Lang.format("$1$: $2$", [ i, times.value() ]));

var text = Lang.format("$1$:$2$:$3$", [
time.hour.format("%02d"),
time.min.format("%02d"),
time.sec.format("%02d")
]);

dc.drawText(cx, cy, font, text, Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);
cy += ty;
}
}
}
[/code]

I ran this code and captured the following on the console...

Shell Version 0.1.0
0: 1432269608
1: 1432252800
2: 1432278000


According to http://www.epochconverter.com/, those timestamp values are equal to...

0: 5/22/2015 04:40:08 GMT, 21:40:08 PM GMT-7:00 DST
1: 5/22/2015 00:00:00 GMT, 5:00:00 PM GMT-7:00 DST
2: 5/22/2015 07:00:00 GMT, 12:00:00 AM GMT-7:00 DST
  • Former Member
    Former Member over 9 years ago
    I know I ran into this problem. I was going to post about it, can't remember if I did or not. However I also remember the events unfolding such that I found some logic to it in the end, that it worked when comparing against some other time value. I will try and work if I did settle on that conclusion, and if so, why.
  • That was my head banging today.
  • Former Member
    Former Member over 9 years ago
    Nah, think I'm remembering it wrong. I seem to have ended up with
    function timeNow() {
    return (Time.now().value() + System.getClockTime().timeZoneOffset);
    }


    function timeToday() {
    return (timeNow() - (timeNow() % 86400));
    }
  • Former Member
    Former Member over 9 years ago
    I remember now. What I had been thinking at the time, was that Time.today() had the correct relationship with Time.now(). However, like you say it is not based on GPS location.... in the simulator. Have you tried it on a device?
  • I tried it on my device and did some headbanging wondering why everything is not correct. (+8 hours in my case)
    refer to the thread -

    https://forums.garmin.com/showthread.php?250256-Countdown-timer-on-WatchFace-out-of-sync-with-clock
  • Former Member
    Former Member over 9 years ago
    Well that's actually sorta good. It means the relationship is consistent.

    FWIW I had mentioned this https://forums.garmin.com/showthread.php?227053-I-think-Time-toady()-should-return-a-different-result
  • Sorry for the late reply on this. I've already reported this issue based on the thread Sharkbait mentions above. I've added this thread to the tecket for more reference, but this should be taken care of as part of the other issue.
  • Sorry for the late reply on this. I've already reported this issue based on the thread Sharkbait mentions above. I've added this thread to the tecket for more reference, but this should be taken care of as part of the other issue.


    I ran into this as well. In case anyone else hits it, I ended up writing a `LocalTime` module and implemented a function to handle this:

    // Return a `Moment`representing now relative to the local time
    function now() {
    var offsetSeconds = System.getClockTime().timeZoneOffset;
    var offsetDuration = new Time.Duration(offsetSeconds);
    return Time.now().add(offsetDuration);
    }

    // Return a `Moment` representing the start of the current local day
    function today() {
    // Time.today() claims to do this based on your GPS, but appears to
    // actually be using UTC
    var utcToday = Time.today();

    if (utcToday.greaterThan(now())) {
    var oneDayBackwards = new Time.Duration(-Time.Gregorian.SECONDS_PER_DAY);
    // We're still in the previous day locally, so use the previous day
    return utcToday.add(oneDayBackwards);
    } else {
    // We're in the same day as UTC so we can use its today value
    return utcToday;
    }
    }