SyntaxBoom

Languages & Coding => BlitzBasic (Plus/3D variants) => Code Archive => Topic started by: RemiD on Jun 15, 2025, 10:09 AM

Title: wrong millisecond value, correct millisecond value
Post by: RemiD on Jun 15, 2025, 10:09 AM
;wrong millisecond value, correct millisecond value 20180327
;on some computers with Windows 10, millisecs() returns a wrong millisecond value, this should get a correct millisecond value

Graphics3D(640,360,32,2)

Global NormalMil% = MilliSecs()
Global FixMil% = FixMilli()

Main()

End()

Function Main()

 Repeat

  If( KeyHit(28)=1 )
   NormalMil = MilliSecs()
   FixMil = FixMilli()
  EndIf

  SetBuffer(BackBuffer())
  ClsColor(000,000,000) : Cls()

  Color(250,250,250)
  TStr$ = NormalMil : Text(GraphicsWidth()/2-StringWidth(TStr)/2,0,TStr)
  TStr$ = FixMil : Text(GraphicsWidth()/2-StringWidth(TStr)/2,15,TStr)

  Flip(1)

 Until( KeyDown(1)=1 )

End Function

Function FixMilli%()

 Return MilliSecs() And $7FFFFFFF ; fix suggested by TomToad

End Function
Title: Re: wrong millisecond value, correct millisecond value
Post by: Baggey on Jun 15, 2025, 06:24 PM
@remid I ran the code on my windows 10 Pc and im getting "213688282"
Title: Re: wrong millisecond value, correct millisecond value
Post by: RemiD on Jun 18, 2025, 08:52 AM
the problem only happens on some computers... not sure why...
Title: Re: wrong millisecond value, correct millisecond value
Post by: Midimaster on Jun 18, 2025, 10:31 AM
You forgot to tell us the "wrong" value. Maybe it is the wellknown 32bit overrun problem which happens on long running computers?

Milliseconds reports a 32bit INTEGER value and when the timer increases over $7F FF FF FF after 24 days run of windows BlitzMax reports a negative number. You can expand this to 48 days, when you transfer the bytes to a LONG variable. And with a little calculation you can expand this to endless time. But it remain the problem of the moment when your app start shortly before $FFFFF000 and during the run the timer jumps back to 0 after $FFFFFFFF
Title: Re: wrong millisecond value, correct millisecond value
Post by: RemiD on Jun 18, 2025, 12:51 PM
we already discussed about this issue on syntaxbomb.com...

QuoteSame result than with the Blitz3d millisecs() function :
On 2 computers with Windows 7, it returns the correct value (the 2 milliseconds values of the 2 functions are the same)
But on the computer with Windows 10, it returns a negative value (around -508100000) (the computer has just been started... and it was not in sleep mode)

those who need this fix will find it.

(unfortunately waybackmachine does not give us access to all threads / posts of syntaxbomb.com...)
Title: Re: wrong millisecond value, correct millisecond value
Post by: Krischan on Jun 18, 2025, 01:46 PM
Maybe it's an unusual workaround but what about using the continous UNIX timestamp value from OS and a timer instead of Millisecs? The Unix time is consistent and the Milliseconds timer gets reset every second and is added to the Unix timestamp.

Here is a simple example, you can either use it with the Milliseconds or if seconds are sufficient use the Unix timestamp only:

SuperStrict

Framework brl.systemdefault
Import brl.timerdefault
Import brl.polledinput
Import brl.StandardIO
Import brl.glmax2d

Graphics 800,600

Global unixtimer:TTimer=CreateTimer(1000)
Global unixtemp:Long

While Not KeyHit(KEY_ESCAPE)

Cls
DrawText UnixMillisecs(True),0,0
DrawText UnixMillisecs(False),0,15
Flip

Wend

End

' --------------------------------------------------------------------------------------------
' get the UNIX Milliseconds since 01/01/1970
' --------------------------------------------------------------------------------------------
Function UnixMillisecs:Long(usems:Int=True)

Local add:String

If unixtemp<>GetUnixTimeStamp() Then

unixtemp=GetUnixTimeStamp()
StopTimer unixtimer
unixtimer=CreateTimer(1000)

EndIf

If usems Then add=String(unixtimer.ticks() Mod 1000)

Return Long(GetUnixTimeStamp()+add)

End Function

' --------------------------------------------------------------------------------------------
' get current UNIX timestamp from OS
' --------------------------------------------------------------------------------------------
Function GetUnixTimeStamp:Long()

Local time:Long[256]
time_(time)
Return time[0]

End Function

It should even ignore the Year 2038 problem, you can check this if you change the last line to Return time[0]+2^31