Time to milliseconds

Share your advanced PureBasic knowledge/code with the community.
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Time to milliseconds

Post by ultralazor »

It's scary how powerful this is. I didn't see it done elsewhere. I know it's simple, but when working with time this single function replaces a lot of code, like for offset calculations..

Code: Select all

;time to milliseconds
Procedure.l ttm(hr$,min$="00",sec$="00")
  Protected tmp,tmp1,tmp2
  tmp=1000*Val(sec$)
  tmp1=59000*Val(min$)
  tmp2=3481000*Val(hr$)
  ProcedureReturn tmp2+tmp1+tmp
EndProcedure
;milliseconds to time
Procedure.s mtt(num.l)
  Protected tmp$,tmp1$,tmp2$
  tmp$=Str(num/3481000)
  num = num - (3481000*(num/3481000))
  tmp1$=Str(num/59000)
  num = num - (59000*(num/59000))
  tmp2$=Str(num/1000)
  If Len(tmp$)<>2 : tmp$ = "0"+tmp$ : EndIf
  If Len(tmp1$)<>2 : tmp1$ = "0"+tmp1$ : EndIf
  If Len(tmp2$)<>2 : tmp2$ = "0"+tmp2$ : EndIf
  ProcedureReturn tmp$+":"+tmp1$+":"+tmp2$
EndProcedure
Debug Str(ttm("23","33","03"))
Debug mtt(ttm("23","33","03"))
so many ideas so little time..
infratec
Always Here
Always Here
Posts: 7623
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Time to milliseconds

Post by infratec »

Hi,

an 'optimized' version:

Code: Select all

;time to milliseconds
Procedure.l ttm(hr$,min$="00",sec$="00")
  ProcedureReturn 3481000*Val(hr$) + 59000*Val(min$) + 1000*Val(sec$)
EndProcedure

;milliseconds to time
Procedure.s mtt(num.l)
  Protected tmp$
  tmp$=RSet(Str(num/3481000),2,"0")+":"
  num%3481000
  tmp$+RSet(Str(num/59000),2,"0")+":"
  num%59000
  tmp$+RSet(Str(num/1000),2,"0")
  ProcedureReturn tmp$
EndProcedure

Debug Str(ttm("23","33","03"))
Debug mtt(ttm("23","33","03"))
Bernd
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Time to milliseconds

Post by bosker »

Since this is in Tricks 'n' Tips, I feel bound to point out that the conversion constants are wrong in the previous posts.
Every occurrence of 59000 should be 60000 and every 3481000 should be 3600000.
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Time to milliseconds

Post by ultralazor »

lol..I know the numbers were wrong, but I get a giggle every time people correct other people on internet forums..it's just the way they do it, like they were waiting to do it.

Code: Select all

;time to milliseconds
Procedure.l ttm(hr$,min$="00",sec$="00")
  ProcedureReturn 3600000*Val(hr$) + 60000*Val(min$) + 1000*Val(sec$)
EndProcedure

;milliseconds to time
Procedure.s mtt(num.l)
  Protected tmp$
  tmp$=RSet(Str(num/3600000),2,"0")+":"
  num%3481000
  tmp$+RSet(Str(num/60000),2,"0")+":"
  num%59000
  tmp$+RSet(Str(num/1000),2,"0")
  ProcedureReturn tmp$
EndProcedure

Debug Str(ttm("23","33","03"))
Debug mtt(ttm("23","33","03"))
so many ideas so little time..
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Time to milliseconds

Post by Fluid Byte »

ultralazor wrote:lol..I know the numbers were wrong, but I get a giggle every time people correct other people on internet forums..it's just the way they do it, like they were waiting to do it.
Your mixing up "being a smart ass" with sloppy programming.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Time to milliseconds

Post by ultralazor »

Fluid Byte wrote:
ultralazor wrote:lol..I know the numbers were wrong, but I get a giggle every time people correct other people on internet forums..it's just the way they do it, like they were waiting to do it.
Your mixing up "being a smart ass" with sloppy programming.
No I really find humor in it, sorry if that offends you. Thanks for allowing us to use the same forum as you and your friends.. :wink:

I made an error..shut the ISP gateways down before it emotionally wrecks the socially hyper-sensetive..it's like epilepsy and giving people chances to flare their mediocre opinions and desperate cries for recognition are neon strobe lights..
so many ideas so little time..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Time to milliseconds

Post by Trond »

I don't really see much use for the conversion to milliseconds, as it's almost always better to simply store the time as milliseconds in the first place, and only convert it to string for display.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Re: Time to milliseconds

Post by Fluid Byte »

ultralazor wrote:I made an error..shut the ISP gateways down before it emotionally wrecks the socially hyper-sensetive..it's like epilepsy and giving people chances to flare their mediocre opinions and desperate cries for recognition are neon strobe lights..
Any chance that a little bit of PCP slipped into your beloved cereals today?

You posted a half-assed snippet which you didn't even formatted properly and put it in "Tricks 'n' Tips" ...
May I ask what you were expecting? If you are so drastically thin-skinned and can't take this small criticism then please refrain us from future posts in this forum.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
peterb
User
User
Posts: 60
Joined: Sun Oct 02, 2005 8:55 am
Location: Czech Republic
Contact:

Re: Time to milliseconds

Post by peterb »

Code: Select all


Procedure.l ttm ( hr, min = 0, sec = 0)
  ProcedureReturn Date ( 1970, 1, 1, hr, min, sec ) * 1000
  
EndProcedure

Procedure.s mtt ( num.l )
  ProcedureReturn FormatDate ( "%hh:%ii:%ss", num / 1000 )
  
EndProcedure

Debug Str ( ttm ( 23, 33, 03 ) )
Debug mtt ( ttm ( 23, 33, 03 ) )

User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Time to milliseconds

Post by ultralazor »

Fluid Byte wrote:
ultralazor wrote:I made an error..shut the ISP gateways down before it emotionally wrecks the socially hyper-sensetive..it's like epilepsy and giving people chances to flare their mediocre opinions and desperate cries for recognition are neon strobe lights..
Any chance that a little bit of PCP slipped into your beloved cereals today?

You posted a half-assed snippet which you didn't even formatted properly and put it in "Tricks 'n' Tips" ...
May I ask what you were expecting? If you are so drastically thin-skinned and can't take this small criticism then please refrain us from future posts in this forum.
This was posted after 18 hours of being up, I threw it together to get non-UTC server time to local time offset for automaton, and found it more useful over the lacking integrated functions, so I posted to to help others, with the typical demographic who purchase this compiler in mind..non-conformist I know.

Being sensitive to being relevant and consistent here means very little to me, I'm a pseudonym in a community that yields only debates and technical support, and little else..for a proprietary BASIC compiler.

'Here I only burn bridges over wet grass, in a well-managed suburb with image-conscious people'
so many ideas so little time..
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Time to milliseconds

Post by MachineCode »

I want to know why it's so scary and powerful.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
bosker
Enthusiast
Enthusiast
Posts: 105
Joined: Fri Jan 08, 2010 11:04 pm
Location: Hampshire, UK

Re: Time to milliseconds

Post by bosker »

@Ultralazor
Posting to help others is a laudible aim but it's only achievable if what you post is correct.
I posted my comment for exactly that reason - to help others (and you) because your code was wrong.
Having noticed that it would have been irresponsible of me to NOT point out the errors. People visit this part of the forum to find out how to do things - giving incorrect examples doesn't help anybody.

Incidentally, your reposted code is still wrong.
User avatar
ultralazor
Enthusiast
Enthusiast
Posts: 186
Joined: Sun Jun 27, 2010 9:00 am

Re: Time to milliseconds

Post by ultralazor »

MachineCode wrote:I want to know why it's so scary and powerful.
It's an imaginative figure of speech that mysteriously attracts internet trolls. Like me saying you contribute more than that to all the communities you belong to..imagination :wink:

@bosker: I didn't see that, thanks for the corrected code, it should help people who are new that want to know how. Mine returns identical results to all code here with fixed numbers so it's confusing.
so many ideas so little time..
Post Reply