12 hour display issues

Former Member
Former Member
Could someone please tell me what is wrong with the following logic, I've used this in a previous implementation but my updated watch face is having issues:

function onUpdate(dc)
{
var clock = Sys.getClockTime();
var timeDisplay = getClockTime(clock);
...
}

hidden function getClockTime(clockTime)
{
var hour, min, ampm, result;

hour = clockTime.hour;
min = clockTime.min.format("%02d");

if (devSettings.is24hour)
{
hour = hour.format("%02d");
result = Lang.format("$1$:$2$", [hour, min]);
}
else
{
ampm = (hour > 11) ? "PM" : "AM";
hour = hour % 12;
hour = (hour == 0) ? 12 : hour;
hour = hour.format("%02d");

result = Lang.format("$1$:$2$ $3$", [hour, min, ampm]);
}

return result;
}

For some reason when 9 AM hits, the time displayed indicates midnight and it looks like it might only be on the Forerunner 230 watches. I have my vivoactive set to 12 hour format right now and its fine. I just got a friend who reported the issue is still present, not sure what else could be wrong. Thanks in advance!
  • Just a wild guess here, but 9 am is right about 32767 seconds. Any chance that it's using a 16 bit signed integer where it needs something bigger?
  • function onUpdate(dc)
    {
    var clock = Sys.getClockTime();
    var timeDisplay = getClockTime(clock);
    ...
    }

    hidden function getClockTime(clockTime)
    {
    var hour, min, ampm, result;

    hour = clockTime.hour;
    min = clockTime.min.format("%02d");

    if (devSettings.is24hour)
    {
    hour = hour.format("%02d");
    result = Lang.format("$1$:$2$", [hour, min]);
    }
    else
    {
    ampm = (hour > 11) ? "PM" : "AM";
    hour = hour % 12;
    hour = (hour == 0) ? 12 : hour;
    hour = hour.format("%02d");

    result = Lang.format("$1$:$2$ $3$", [hour, min, ampm]);
    }

    return result;
    }



    I don't see any obvious problems. I've typically written it more like this...

    function onUpdate(dc)
    {
    var clock = Sys.getClockTime();
    var timeDisplay = getFormattedTime(clockTime.hour, clockTime.min, devSettings.is24hour);
    ...
    }

    hidden function getFormattedTime(hour, min, is24hour)
    {
    if (is24hour) {
    return Lang.format("$1$:$2$", [ hour.format("%02d"), min.format("%02d") ]);
    }
    else {
    var ampm = (hour < 12) ? "AM" : "PM"; // should use resource strings

    // map the range
    // [ 0, ..., 23 ]
    // to
    // [ 11, ..., 34 ]
    // then to
    // [ 11, 0, 1, ..., 10, 11, 0, 1, ..., 10 ]
    // then to
    // [ 12, 1, 2, ..., 11, 12, 1, 2, ..., 11 ]
    hour = (hour + 11) % 12 + 1;

    return Lang.format("$1$:$2$ $3$", [ hour, min.format("%02d"), ampm ]);
    }
    }


    You don't indicate what code path is being used when the problem is seen. Is the 24-hour clock being used, or does the problem occur with both the 12 and 24-hour clock? Have you tried just displaying the values in the structure provided by Sys.getClockTime()?

    Travis
  • Just a wild guess here, but 9 am is right about 32767 seconds. Any chance that it's using a 16 bit signed integer where it needs something bigger?


    If that is the issue it would have to be firmware related or he's leaving out some code that does bit packing. The problem would happen at about 9:06am, and it seems it would continue beyond that. Not to mention that ConnectIQ doesn't have a 16-bit integer type. :)

    Travis
  • This code seems to have some of the exact same code I use in my 12/24hr code.

    ampm = (hour > 11) ? "PM" : "AM";
    hour = hour % 12;
    hour = (hour == 0) ? 12 : hour;


    The first line checks for am/pm.
    The second line makes "hour" between 0 and 11....
    And the third line just says if "hour is 0, make it 12"

    I sure don't see what's wrong....
  • I ran the following code on my 235 with no problems. It displayed 9:34 PM last night correctly and it displayed 8:38 AM and 9:25 AM and a bunch in between correctly this morning. I used the 1.2.6 SDK and 4.10 firmware on the 235. I don't have a 230.

    function onUpdate(dc) {
    var clock = Sys.getClockTime();
    Sys.println("clock " + clock);
    var timeDisplay = getClockTime(clock);

    View.onUpdate(dc);
    dc.setColor(Gfx.COLOR_GREEN, Gfx.COLOR_BLACK);
    dc.drawText(50, 50, Gfx.FONT_MEDIUM, timeDisplay, Gfx.TEXT_JUSTIFY_LEFT);

    }

    hidden function getClockTime(clockTime) {
    var hour, min, ampm, result;

    hour = clockTime.hour;
    min = clockTime.min.format("%02d");

    ampm = (hour > 11) ? "PM" : "AM";
    hour = hour % 12;
    hour = (hour == 0) ? 12 : hour;
    hour = hour.format("%02d");

    result = Lang.format("$1$:$2$ $3$", [hour, min, ampm]);
    Sys.println("result " + result);
    return result;

    }
  • Former Member
    Former Member over 9 years ago
    If that is the issue it would have to be firmware related or he's leaving out some code that does bit packing. The problem would happen at about 9:06am, and it seems it would continue beyond that. Not to mention that ConnectIQ doesn't have a 16-bit integer type. :)

    Travis


    Thanks Travis for the idea. I have a pretty good feeling that the user's watch probably needs a firmware update because as soon as she switched it to 24 hour mode it was fine. Once changing back to 12 hour mode around 9 am it would display midnight even after I posted the updated code to garmin. I'll follow up with her tomorrow to see if its still occurring, if so, I might just recommend she uninstall the watch face and re-install (had another friend who did that and fixed his problem).
  • Former Member
    Former Member over 9 years ago
    I ran the following code on my 235 with no problems. It displayed 9:34 PM last night correctly and it displayed 8:38 AM and 9:25 AM and a bunch in between correctly this morning. I used the 1.2.6 SDK and 4.10 firmware on the 235. I don't have a 230.

    function onUpdate(dc) {
    var clock = Sys.getClockTime();
    Sys.println("clock " + clock);
    var timeDisplay = getClockTime(clock);

    View.onUpdate(dc);
    dc.setColor(Gfx.COLOR_GREEN, Gfx.COLOR_BLACK);
    dc.drawText(50, 50, Gfx.FONT_MEDIUM, timeDisplay, Gfx.TEXT_JUSTIFY_LEFT);

    }

    hidden function getClockTime(clockTime) {
    var hour, min, ampm, result;

    hour = clockTime.hour;
    min = clockTime.min.format("%02d");

    ampm = (hour > 11) ? "PM" : "AM";
    hour = hour % 12;
    hour = (hour == 0) ? 12 : hour;
    hour = hour.format("%02d");

    result = Lang.format("$1$:$2$ $3$", [hour, min, ampm]);
    Sys.println("result " + result);
    return result;

    }


    Thanks MoxyRoger, I use the heck out of the println function. It's the only way I have found to actively debug issues like this. I will get with the user tomorrow and make a few recommendations to see what else could be going on.