Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 06:47 19 Apr 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : The midnight hour issue...

Author Message
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9041
Posted: 05:58am 17 Aug 2022
Copy link to clipboard 
Print this post

The problem with subtracting 12 from the 24 hour clock for PM, if the time is 12, is solved by this sub I wrote today to deal with the issue where 00 hours is midnight, but 12-12=0 for PM also reports Midday as being Midnight, and it all gets very confusing.

Fruit Of The Shed link to the fix...

Perhaps not the most elegant or efficient code, but this DOES work, and gets around the problem of zero being interpreted by code as BOTH the midnight and midday hours.
Smoke makes things work. When the smoke gets out, it stops!
 
jirsoft

Guru

Joined: 18/09/2020
Location: Czech Republic
Posts: 532
Posted: 10:52am 17 Aug 2022
Copy link to clipboard 
Print this post

Maybe I don't understand the problem correctly, but for me hour<12 is AM, everything else is PM...
00=midnight (so 00:30 is 0:30 AM)
then 01-11:59 is AM
12=midday (so 12:30 is 0:30 PM)
then 13-23:59 is PM, then follows 00:00

Usually times after 24 (24:01) should not be used, it should be written as 00:01, but when it's happening, it will be easier to put first check for it
Sub CLOCK
 HR=Val(Left$(Time$,2))
 INC HR, -24*(HR>23) 'IF HR>23 THEN HR=HR-24
 IF HR<12 THEN
   Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+Time$+" AM",C
 ELSE
    Text MM.HRes/2,29*16,"DATE: "+Date$+", TIME: "+Str$(HR-12)+Right$(Time$,6)+" PM",C
 ENDIF
END Sub

Jiri
Napoleon Commander and SimplEd for CMM2 (GitHub),  CMM2.fun
 
pwillard
Senior Member

Joined: 07/06/2022
Location: United States
Posts: 274
Posted: 11:35am 17 Aug 2022
Copy link to clipboard 
Print this post

I guess everyone has a different approach,  this worked for me.


SUB DO_TIME
 LastTime = Left$(Time$,5)
 LOCAL hour = VAL(LEFT$(lastTime$,2))
 LOCAL minute = VAL(RIGHT$(lastTime$,2))
 LOCAL MER = hour
 LOCAL AmPm$
 LOCAL HH$,MM$
' Check if AM or PM
 IF MER >= 12 THEN
   MER = MER - 12
   AMPM$ = "PM"
 ELSE
   AMPM$ = "AM"
 ENDIF
' Handle 12 AM, avoid 0 O'clock.
 IF MER = 0 THEN MER = 12
' Reassemble the TIME variables back into a string value
' with leading zero blanking or padding
 HH$ = STR$(MER)
 IF MER < 10 THEN HH$ = " " + HH$
 HH$ = HH$ + ":"
 MM$ = STR$(Minute)
 IF minute < 10 THEN MM$ = "0" + MM$
 HHMM$ = HH$ + MM$
' Update the time

   TEXT (MM.HRES/2 -50), MM.VRES/4, HHMM$, "CM", 6, 2 ' TIME
   TEXT MM.HRES -50, MM.VRES/4 +10, AMPM$, "R" ' AM/PM

END SUB

Edited 2022-08-17 21:39 by pwillard
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2285
Posted: 12:09pm 17 Aug 2022
Copy link to clipboard 
Print this post

or...

Dim DN$(2) length 2 = ("am", "pm", "")

For I=0 To 23                ' or "To 24 Step 3" to fit on a single screen
  X=((I+11) Mod 12)+1
  Print I "oo", X DN$(I>11)
Next I


been doing it this way since 1986!

cheers,
rob   :-)
Edited 2022-08-17 22:23 by robert.rozee
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 3490
Posted: 01:45pm 17 Aug 2022
Copy link to clipboard 
Print this post

Robert,

You are a wizard, that is by far the most compact solution I have seen.
X=((I+11) Mod 12)+1
Print X DN$(I>11)


Volhout
Edited 2022-08-17 23:48 by Volhout
PicomiteVGA PETSCII ROBOTS
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9041
Posted: 01:45am 18 Aug 2022
Copy link to clipboard 
Print this post

Wow!
Lots of different solutions!


I will HAVE to try Rob's solution out now! (substituting my HR for his I variable)
It is so small and elegant!    

I will link to this thread when I edit the FOTS page, saying there are more examples of doing it here.
Smoke makes things work. When the smoke gets out, it stops!
 
bigmik

Guru

Joined: 20/06/2011
Location: Australia
Posts: 2867
Posted: 03:44am 18 Aug 2022
Copy link to clipboard 
Print this post

GDay All,

  Volhout said  Robert,

You are a wizard, that is by far the most compact solution I have seen.
X=((I+11) Mod 12)+1
Print X DN$(I>11)


Volhout


What language is that?    Joking!!
I have no idea what it means, I will have to consult the Oracle..

It only shows me how little I actually do know about MMBasic.

Mick
Mick's uMite Stuff can be found >>> HERE (Kindly hosted by Dontronics) <<<
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9041
Posted: 03:53am 18 Aug 2022
Copy link to clipboard 
Print this post

I've never been able to follow Modulus calculations, but it does work!



Smoke makes things work. When the smoke gets out, it stops!
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024