Functions to get the elapsed millisecond and second times since the start of 2020
Posted: Wed Jan 12, 2022 12:09 am
These two functions get the approximate elapsed millisecond and second times since the start of 2020.
The purpose of these functions is to obtain a non-repeating value (to seed a random number generator, etc). They are not meant to be precise.
The purpose of these functions is to obtain a non-repeating value (to seed a random number generator, etc). They are not meant to be precise.
Code: Select all
Procedure.q GetMillisecTime()
; Returns the approximate number of milliseconds elapsed since the start of 2020. The return value is a quad (64 bit integer).
; This function is designed to obtain a non-repeating value (to seed a random number generator, etc). It is not meant to be precise.
; -
; https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime
; https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
Protected time_info.SYSTEMTIME, elapsed.q
GetSystemTime_( @time_info )
elapsed = ( time_info\wYear - 2020 ) * ( 365 * 24 * 60 * 60 * 1000 )
elapsed + ( time_info\wMonth - 1 ) * ( 31 * 24 * 60 * 60 * 1000 ) ; We will use the maximum number of days in a month here so that we don't need to care about leap years and day-counts for individual months.
elapsed + ( time_info\wDay * ( 24 * 60 * 60 * 1000 ) )
elapsed + ( time_info\wHour * ( 60 * 60 * 1000 ) )
elapsed + ( time_info\wMinute * ( 60 * 1000 ) )
elapsed + ( time_info\wSecond * 1000 )
elapsed + time_info\wMilliseconds
ProcedureReturn elapsed
EndProcedure
Procedure.l GetSecondTime()
; Returns the approximate number of seconds elapsed since the start of 2020. The return value is a long (32 bit integer).
; This function is designed to obtain a non-repeating value (to seed a random number generator, etc). It is not meant to be precise.
; -
; https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsystemtime
; https://docs.microsoft.com/en-us/windows/win32/api/minwinbase/ns-minwinbase-systemtime
Protected time_info.SYSTEMTIME, elapsed.l
GetSystemTime_( @time_info )
elapsed = ( time_info\wYear - 2020 ) * ( 365 * 24 * 60 * 60 )
elapsed + ( time_info\wMonth - 1 ) * ( 31 * 24 * 60 * 60 ) ; We will use the maximum number of days in a month here so that we don't need to care about leap years and day-counts for individual months.
elapsed + ( time_info\wDay * ( 24 * 60 * 60 ) )
elapsed + ( time_info\wHour * ( 60 * 60 ) )
elapsed + ( time_info\wMinute * 60 )
elapsed + time_info\wSecond
ProcedureReturn elapsed
EndProcedure
Debug GetMillisecTime()
Debug GetSecondTime()