User Tools

Site Tools


mmbasic:clock_or_rtc_zero_hour_am_pm_fix

Clock or RTC zero-hour AM/PM fix...

This code was written when I needed to convert the 24-hour clock format into 12-hour with an AM or PM assignment.

With the 24-hour clock, there is an issue with how code deals with zero hours. As we all know, zero hours is midnight, but if you want a 12-hour clock instead of the 24 hour clock standard, the obvious thought would be to simply subtract 12 from any clock hour greater then 12 or 13.

That DOES work for all other hours, but for zero hour, it can be a problem for the code to decide on the AM/PM indication. For example, if the time is 12:15:00, if your code simply subtracts 12 from any hour >=12, the hour will end up as zero, and so the code will decide it is midnight(and assign an “AM”), when it is actually midday(and should be a “PM”). ;)

Any hour before 12 will be correctly assigned AM. Any hour after 13 will be correctly assigned PM. But 12 will be reported as midnight at midday AND midnight also. Confusing, isn't it! ;)

To fix this dilemma, you need to analyze what the hour number is from the 24 hour clock, and then use a Select Case routine to decide what actually needs to happen. Select Case is used, as this construct allows you to easily cater for both zero hours and the “Hour-12=0” issue.

The following sub, is a standard example of this being used inside an MMBASIC program running on a PicoMite VGA port:

Sub CLOCK
  HR=Val(Left$(Time$,2))
  Select Case HR
    Case 0
      CLK$="12"+Right$(Time$,6)
      Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+CLK$+" AM",C
    Case 1 To 11
      Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+Time$+" AM",C
    Case 12
      Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+Time$+" PM",C
    Case 13 To 23
      CLK$=Str$(HR-12)+Right$(Time$,6)
      Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+CLK$+" PM",C
  End Select
  If CVGA Then TILE 2,29,RGB(white),RGB(blue),36,1
End Sub

Variable HR and string CLK$ need to be defined, or you could make them local to the sub if you want. In either event, variable HR contains the numeric value of the hour from the 24-hour clock system string TIME$. This value is then used to work out what version of the AM/PM clock to print.

If the clock is indeed in the midnight hour, the code reformats the clock using CLK$ and essentially replacing the 00 with 12 to represent midnight, then copies the remains of TIME$ onto the end of that, and appends an “AM” to the end, then this is printed out as “12:15:55 AM” for example.

If the clock is the standard AM hours of 1AM to 11AM, then the time is printed as-is, along with “AM”. If the clock is in the midday hour, then the time is printed as-is, along with “PM” If the clock is the standard PM hours of 1PM to 11PM, then the time is printed as “Hour-12”, along with a “PM”.

In the example code above, this sub is actually called by a SETTICK interrupt every second, to update the clock as shown on the VGA screen.

mmbasic/clock_or_rtc_zero_hour_am_pm_fix.txt · Last modified: 2024/01/19 09:30 by 127.0.0.1