AUGH!! Trying to figure out how to switch between 12 and 24 hours

Former Member
Former Member
Hello everyone,

Ok, I am trying to figure out how to switch from 12 to 24 hours based on the watch's settings. My plan is that if the watch is set at 12 hour, it will just display something like 1:30 (for day or night) and if it's set for 24 hours, it will display 1330 for 1:30pm.

I have been trying to figure out how to do it for the past several days but can't get it to work. The 24 hour time works fine, but the 12 hour is screwing up. It displays 12 midnight as 0:00 instead of 12:00 and 1pm as 13:00.

My lines are obviously wrong somewhere and I am wondering if anyone can take a look at it and see where it bust?

Much thanks in advance.

View.onUpdate(dc);

dc.setColor(Gfx.COLOR_BLACK, Gfx.COLOR_BLACK);

var clockTime = Sys.getClockTime();
var hour = clockTime.hour;
var sys = Sys.getDeviceSettings().is24Hour;
if (sys==false) {
if (hour > 12) {
hour = hour - 12;
if (hour == 0) {hour = 12;}
}
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.drawText(0, 100, Gfx.FONT_LARGE, Lang.format("$1$:$2$", [clockTime.hour, clockTime.min.format("%02d")]), Gfx.TEXT_JUSTIFY_LEFT);
} else {
dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.drawText(0, 100, Gfx.FONT_LARGE, Lang.format("$1$$2$", [clockTime.hour.format("%02d"), clockTime.min.format("%02d")]), Gfx.TEXT_JUSTIFY_LEFT);
}
  • Here's an easy way to do it:
    var clockTime = Sys.getClockTime();
    var hour = clockTime.hour;
    var sys = Sys.getDeviceSettings().is24Hour;
    if (sys==false) {
    hour=hour%12;
    if(hour==0) {hour=12;}
    }
    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
    dc.drawText(0, 100, Gfx.FONT_LARGE, Lang.format("$1$:$2$", [hour, clockTime.min.format("%02d")]), Gfx.TEXT_JUSTIFY_LEFT);

    If you're in 12 hr mode, do a modulus 12 on the hour. This will make "hour" between 0 and 11. If it's 13:00, it would be 1:00, 15:00, 3:00 and so on.
    As there is no 0 hour in 12 Hr time, check for 0, and if it is, make it 12. so 0:00 (midnight) will be 12:00 in 12 hr mode, noon will also be 12:00

    If you want a leading 0 in 24 hr mode, could change it up a bit, but this will get you going.

    UPDATE: Actually, in looking at your code again, in the 12hr drawText, you want to just use "hour" and not "clockTime.hour", as "hour" has the 12hr value, and clockTime.hour is the 24hr value.
  • It is rather complicated and uses 3 arithmetic operations (including a mod), but this does it without an additional branch...

    hour = 1 + (clockTime.hour + 11) % 12
  • "If you're in 12 hr mode, do a modulus 12 on the hour" do you mean this line: "hour=hour%12;"?

    What is the "hour%12" trying to do here?
    When you use the modulo operator (‘[FONT=Book Antiqua]%[/FONT]’), you obtain the remainder of dividing the first operand (say, [FONT=Book Antiqua]m[/FONT]) by the second operand (say, [FONT=Book Antiqua]n[/FONT]). The result is an integer in [FONT=Book Antiqua][0, (n-1)][/FONT].

    Now, if [FONT=Book Antiqua]n[/FONT] is 12, then the modulo operation will return an integer between 0 and 11… but what you want is an integer between 1 and 12, so after performing the modulo you'll then have to apply some sort of mapping between the two sets of integers.
  • ..but what you want is an integer between 1 and 12, so after performing the modulo you'll then have to apply some sort of mapping between the two sets of integers.


    And that is what:

    if(hour==0) {hour=12;}

    does! :) Midnight (0 hrs in 24hr mode) it 12am in 12hr mode.
  • This is pure commentary on my part, but it's really great to see folks learning new programming concepts with Connect IQ. I imagine having the context of the watch with its limited scope and capabilities makes a great place for people to start writing small programs. And along with that, it's so nice to see experienced folks like jim.m.58, TRAVIS.VITEK, and ASmugDill willing to provide so much great instruction.
  • Thanks, Brandon. I used to write C code (as in K&R, and not any flavour of object-oriented C, let alone Java) and the odd BASH script for mid-range systems used in high-volume transaction processing applications last century, so “rusty” would not begin to describe my handicap in programming for modern consumer devices. :eek:
  • I still have my original K&R C book bought for £22.95 in 1986! How things have changed.
  • I still have my original K&R C book bought for £22.95 in 1986! How things have changed.


    Got mine in 1978 (IIRC, we got xerox copies to start as it was still being printed).. Unix on a PDP-11, and 8" floppies to backup code!
    The interesting thing is it was a skinny book, and it did tell you everything you needed to start using the language.
  • That would make sense as it is copyright 1978. I was still at school then although had access to my Dad's work computer via a 300 baud modem connected via acoustic couplers on the phone handset. BT said that was as fast as phone line technology could support.
  • Former Member
    Former Member over 8 years ago
    -

    use:

    hours = (hours+11)%12 + 1;

    can achieve the purpose. Less space and memory usage.