BBC BASIC for Windows
IDE and Compiler >> Tools and Utilities >> TIME$-- convert the current time to 12hr
http://bb4w.conforums.com/index.cgi?board=addins&action=display&num=1511584460

TIME$-- convert the current time to 12hr
Post by michael on Nov 25th, 2017, 03:34am

I am sure someone will come up with a better example, but for now if you want to extract the current time and convert the 24 hour clock to 12 hour clock then I think I did it correctly.. If not let me know.. Thanks
Code:
      MODE 8
      REPEAT
        t$= RIGHT$(TIME$,8)
        h$= LEFT$(t$,2)
        m$= MID$(t$,4,2)
        s$= RIGHT$(t$,2)
        LET std%=VAL(h$)
        CASE std% OF
          WHEN 13 :h$="01"
          WHEN 14 :h$="02"
          WHEN 15:h$="03"
          WHEN 16:h$="04"
          WHEN 17:h$="05"
          WHEN 18:h$="06"
          WHEN 19:h$="07"
          WHEN 20:h$="08"
          WHEN 21:h$="09"
          WHEN 22:h$="10"
          WHEN 23:h$="11"
          WHEN 00:h$="12"
        ENDCASE
  
        PRINT h$+":"+m$+":"+s$
        WAIT 10
        CLS
      UNTIL FALSE
      END
 

Re: TIME$-- convert the current time to 12hr
Post by DDRM on Nov 26th, 2017, 3:57pm

Hi Michael,

Your code looks fine, though it could be more concise. Since ":" is not a valid member of a number, the VAL of your t$ will just be the hour, so you could have:
Code:
t$=RIGHT$(TIME$,8)
IF VAL(t$)=0 THEN t$="12"+MID$(t$,3)
IF VAL(t$)>12 THEN t$=STR$(VAL(t$)-12)+MID$(t$,3)
PRINT t$ 


Could you add am or pm, as appropriate?

Code:
t$=RIGHT$(TIME$,8)+" am"
IF VAL(t$)=0 THEN t$="12"+MID$(t$,3)
IF VAL(t$)>12 THEN t$=STR$(VAL(t$)-12)+MID$(t$,3,6)+" pm"
PRINT t$ 


Best wishes,

D
Re: TIME$-- convert the current time to 12hr
Post by marsFS on Nov 27th, 2017, 12:22am

Very nice use of VAL, I've never used it that way so nice tip :)

I did spot one bug with the code as it will show incorrectly for 12 pm (noon).

Following code checks the time range and creates a suffix to append am or pm and I used h% to reduce repeated use of VAL and build the time string at the end but it makes no difference to the logic.

Code:
MODE 8

REPEAT
        t$=RIGHT$(TIME$,8)
  
        h%=VAL(t$)
        IF h%<12 THEN s$=" am" ELSE s$=" pm"
        IF h%>12 THEN h%-=12
        IF h%=0 THEN h%=12
  
        t$=RIGHT$("0"+STR$(h%),2)+MID$(t$,3)+s$
  
        PRINT TAB(0,0)t$
        WAIT 10
UNTIL FALSE