Bug with Connect IQ sdk?

Former Member
Former Member
Hi all, I am not sure if this is a bug with the sdks or firmware itself.

On the emulator, the time 23:05 is able to show properly.
But on the watch, it shows as 23:5.

The code as below. Is there something I did wrongly? Thks for your advice in advance. Thks!

var timeString = Lang.format("$1$:$2$", [clockTime.hour, clockTime.min.format("%.2d")]);

dc.drawText(15, (height/2) - 30, Gfx.FONT_NUMBER_THAI_HOT, timeString, Gfx.TEXT_JUSTIFY_LEFT);
  • Former Member
    Former Member over 10 years ago
    var timeString = Lang.format("$1$:$2$", [clockTime.hour, clockTime.min.format("%02d")]);
  • var timeString = Lang.format("$1$:$2$", [clockTime.hour.format("%.2d"), clockTime.min.format("%.2d")]);

    That should do it. The documentation for Number references this...

    http://www.cplusplus.com/reference/cstdio/printf/
  • Former Member
    Former Member over 10 years ago
    Using .precision with integers is part of the printf specification, but many implementations do not format this option properly. This online printf tester does not properly format "%.2d". The printf implementation in Microsoft Visual Studio (the version used by the Windows ConnectIQ simulator) also does not format this case properly. The printf implementation that is used by the Mac ConnectIQ simulator is the only one I have been able to find that conforms to the specification for this format string. The printf implementation on our ConnectIQ devices is written to match MSVS implementation, and as a result will not format this option properly. We will try to get this bug documented.

    TL;DR: "%.2d" doesn't work, use "%02d"
  • Former Member
    Former Member over 10 years ago
    It is reasonable to think that (especially since so many implementations don't handle it), but if you look in the C++ specification, you will find:

    http://www.cplusplus.com/reference/cstdio/printf/
    For integer specifiers (d, i, o, u, x, X): precision specifies the minimum number of digits to be written. If the value to be written is shorter than this number, the result is padded with leading zeros. The value is not truncated even if the result is longer. A precision of 0 means that no character is written for the value 0.
  • Former Member
    Former Member over 10 years ago
    var timeString = Lang.format("$1$:$2$", [clockTime.hour, clockTime.min.format("%02d")]);



    Thks for the advice everyone! That line of code was from the sample code. Maybe the sample should be updated too in that case.
  • Neither format is wrong.

    It is a problem because some you may see different behavior depending on what platform you are testing on. According to Brian's post above, it does not behave as expected on a device because the ConnectIQ implementation copies the behavior of a known-to-be broken implementation (MSVC). So to keep your code portable, you'd want to use %02d.

    I haven't verified which works, but I see no reason to argue. If %.2d works for you and doesn't work for anyone else, that's fine. Everyone else says that %02d works for them, so it seems to be the most portable and least risky way to write your code.

    Travis