Page 1 of 1
Time to milliseconds
Posted: Fri Mar 04, 2011 11:55 pm
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"))
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 12:48 am
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
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 4:42 pm
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.
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 7:06 pm
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"))
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 7:11 pm
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.
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 7:21 pm
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..
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..
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 7:41 pm
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.
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 8:11 pm
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.
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 10:20 pm
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 ) )
Re: Time to milliseconds
Posted: Sat Mar 05, 2011 11:48 pm
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'
Re: Time to milliseconds
Posted: Sun Mar 06, 2011 12:51 am
by MachineCode
I want to know why it's so scary and powerful.
Re: Time to milliseconds
Posted: Sun Mar 06, 2011 1:49 am
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.
Re: Time to milliseconds
Posted: Sun Mar 06, 2011 4:17 am
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
@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.