Simple time syntaxis right

Share your advanced PureBasic knowledge/code with the community.
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Simple time syntaxis right

Post by minimy »

Just it, a simple procedure return true or false is syntaxis is good or bad.
8 digit verification format like this 12:30:27 %hh:%ii:%ss
12:30:27 true
a9:10:22 false
1:44:53 false..

Code: Select all

  Procedure isInt(n.s)
    Protected.i p,l,ok
    For l= 1 To Len(n)
      For p='0' To '9'
        If Chr(p)=Mid(n,l,1)
          ok+1
        EndIf
      Next p
    Next l
    If ok=Len(n)
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
  EndProcedure

  Procedure isTime(time.s)
    If isInt(Mid(time,1,2)) And isInt(Mid(time,4,2)) And isInt(Mid(time,7,2)) And Mid(time,3,1)=":" And Mid(time,6,1)=":" And Len(time)=8
      ProcedureReturn #True
    Else
      ProcedureReturn #False
    EndIf
  EndProcedure
  
  
CompilerIf #PB_Compiler_IsMainFile

  Debug isTime("22:10:00")
  Debug isTime("02:03:aa")
  
CompilerEndIf
If translation=Error: reply="Sorry, Im Spanish": Endif
BarryG
Addict
Addict
Posts: 3320
Joined: Thu Apr 18, 2019 8:17 am

Re: Simple time syntaxis right

Post by BarryG »

Let ParseDate() do the work:

Code: Select all

Procedure IsTime(time$)
  If Len(time$)=8 And ParseDate("%hh:%ii:%ss",time$)>-1
    ok=1
  EndIf
  ProcedureReturn ok
EndProcedure

Debug IsTime("00:00:00") ; 1
Debug IsTime("12:30:27") ; 1
Debug IsTime("22:10:00") ; 1
Debug IsTime("a1:b2:c3") ; 0
Debug IsTime("01-02-03") ; 0
Debug IsTime("1:44:53")  ; 0
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Simple time syntaxis right

Post by Saki »

No offense intended, but such nonsense has no place in T&T.
地球上の平和
User avatar
idle
Always Here
Always Here
Posts: 5093
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Simple time syntaxis right

Post by idle »

Saki wrote: Sat May 01, 2021 8:40 pm No offense intended, but such nonsense has no place in T&T.
Dial it down a bit Saki, why is it nonsense? It's a valid Tip the code does what it says. If you don't have something positive or constructive to say It's generally better not to say anything at all.
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Simple time syntaxis right

Post by Saki »

Yes, you are right, of course.
I was probably a bit hasty, since I am accustomed to the visual control of the actual time. :oops:
地球上の平和
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Re: Simple time syntaxis right

Post by minimy »

Thanks Idle, I appreciate the help.
Yes BarryG with PB you can, thanks for share. But i think is good to know how does at low level. May be im old to post things.. :lol:

Hi saki, greetings and welllcome to the noobs forever world.
I put this like old school example. I know 'millenians' comes with google chip included, but is not bad know how can do by yourself the things.
About is not the section. may be. :shock: . Now in the section, thanks to BarryG and his positive comment, we have other code to use.
Any way.. No problem, no offense me. Thanks for your comments and next time you can include any code to share. Be happy! Be safe!

PB forum is the best! ..or not? may be? is this the right place to say this? sure!!! :lol:
If translation=Error: reply="Sorry, Im Spanish": Endif
User avatar
Saki
Addict
Addict
Posts: 830
Joined: Sun Apr 05, 2020 11:28 am
Location: Pandora

Re: Simple time syntaxis right

Post by Saki »

Hi, no, I didn't mean to offend you either.
Technically everything is OK.
A tip :
You don't need math for the clock, you can do that in VectorLib quite easily with rotated circle cutouts.
地球上の平和
User avatar
minimy
Enthusiast
Enthusiast
Posts: 349
Joined: Mon Jul 08, 2013 8:43 pm

Re: Simple time syntaxis right

Post by minimy »

Hey Saki! no problem! Be happy friend!
thanks for the trick!
If translation=Error: reply="Sorry, Im Spanish": Endif
Post Reply