Page 1 of 1

[CODE] Easy way to get millisecond-wise date

Posted: Sat Jan 09, 2016 8:23 am
by es_91
Thought I'd share it with you.

Code: Select all

Procedure. q InitExactDate (getVar = 0)
  
  Protected date
  
  Static ExactDate. q
  Static ExactDateElapsed. q
  
  If getVar = 1
    
    ProcedureReturn ExactDate
  ElseIf getVar = 2
    
    ProcedureReturn ExactDateElapsed
  EndIf
  
  date = Date ()
  Repeat
    
    Delay (1)
  Until Not Date () = date
  
  ExactDateElapsed = ElapsedMilliseconds ()
  ExactDate = Date () * 1000
EndProcedure
Procedure. q GetExactDate (ElapsedMilliseconds. q)
  
  ProcedureReturn InitExactDate (1) + (ElapsedMilliseconds - InitExactDate (2))
EndProcedure
Procedure. l GetRealDate (ExactDate. q) ; GetUnixDate (ExactDate. q)
  
  ProcedureReturn Int (0.001 * ExactDate)
EndProcedure

; EXAMPLE

Define sync$

InitExactDate ()

Debug "Starting " + FormatDate ("%mm-%dd-%yyyy %hh:%ii:%ss", GetRealDate (GetExactDate (ElapsedMilliseconds ())))

Repeat
  
  If GetRealDate (GetExactDate (ElapsedMilliseconds ())) = Date ()
    
    sync$ = "yes" : Else : sync$ = "no -- NO SYNCHRONIZATION!!!" : EndIf
  
  Debug Str (GetExactDate (ElapsedMilliseconds ())) + Space (1) + "synchronized=" + sync$
  
  Delay (10)
ForEver
Here with global variables:

Code: Select all

Global ExactDate. q
Global ExactDateElapsed. q

Procedure InitExactDate ()
  
  Protected date
  
  date = Date ()
  Repeat
    
    Delay (1)
  Until Not Date () = date
  
  ExactDateElapsed = ElapsedMilliseconds ()
  ExactDate = Date () * 1000
EndProcedure
Procedure. q GetExactDate (ElapsedMilliseconds. q)
  
  ProcedureReturn ExactDate + (ElapsedMilliseconds - ExactDateElapsed)
EndProcedure

Re: [CODE] Easy way to get millisecond-wise date

Posted: Sat Jan 09, 2016 8:51 am
by es_91
I would like to ask for help because of an understanding problem.

I thought the millisecond part could easily be calculated with something like this ...

Code: Select all

Millisecond = (GetExactDate (ElapsedMilliseconds ()) << 54) >> 54 + 512
The results, however, count up from 0 to 1023. In a test with a millisecond Delay () results came in each second millisecond, but after 1022 there was 0. How to calculate the exact millisecond value out of the exact date? ... not using string functions.

Re: [CODE] Easy way to get millisecond-wise date

Posted: Sat Jan 09, 2016 8:50 pm
by HeX0R
I'm not sure what you are going to achieve, but I usually do it like this, if I want to show timestamps including milliseconds:

Code: Select all

Global TimeStampOffset

Procedure InitTimeStamp()
	;Get ms offset of elapsedmilliseconds() to date()
	Protected k

	k  = Date()

	While k = Date()
		Delay(0)
	Wend

	TimeStampOffset = Val(Right(Str(ElapsedMilliseconds()), 3))

EndProcedure

;Example
InitTimeStamp()

For i = 1 To 100
	Debug FormatDate("%hh:%ii:%ss", Date()) + "." + Right(Str(ElapsedMilliseconds() - TimeStampOffset), 3)
	Delay(25)
Next i

Re: [CODE] Easy way to get millisecond-wise date

Posted: Fri Mar 04, 2016 4:31 pm
by Julian
HeX0R wrote:I'm not sure what you are going to achieve, but I usually do it like this, if I want to show timestamps including milliseconds:

Code: Select all

Global TimeStampOffset

Procedure InitTimeStamp()
	;Get ms offset of elapsedmilliseconds() to date()
	Protected k

	k  = Date()

	While k = Date()
		Delay(0)
	Wend

	TimeStampOffset = Val(Right(Str(ElapsedMilliseconds()), 3))

EndProcedure

;Example
InitTimeStamp()

For i = 1 To 100
	Debug FormatDate("%hh:%ii:%ss", Date()) + "." + Right(Str(ElapsedMilliseconds() - TimeStampOffset), 3)
	Delay(25)
Next i
HeX0R, I was wondering what your reason for the Delay is? Why do you wait for a second to tick by?

This seems to be ok?

Code: Select all

Global TimeStampOffset = Val(Right(Str(ElapsedMilliseconds()), 3))

For i = 1 To 100
   Debug FormatDate("%hh:%ii:%ss", Date()) + "." + Right(Str(ElapsedMilliseconds() - TimeStampOffset), 3)
   Delay(25)
Next i

Re: [CODE] Easy way to get millisecond-wise date

Posted: Fri Mar 04, 2016 6:12 pm
by HeX0R
You need to synchronize the milliseconds with the date() function otherwise you will end up with (copied directly from the output of your code):

Code: Select all

18:05:59.365
18:05:59.391 <--
18:06:00.417 <-- second increased, milliseconds should start at 0
18:06:00.443
We want to have (my code):

Code: Select all

18:09:25.952
18:09:25.977 <--
18:09:26.004 <--second increased, milliseconds started at 0
18:09:26.030

Re: [CODE] Easy way to get millisecond-wise date

Posted: Fri Mar 04, 2016 8:39 pm
by Julian
Ahh I see what you mean now, cheers