Page 1 of 1

Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 3:13 am
by Axeman
Here is a function to get the current UNIX UTC time in seconds as a quad integer value. This should be year 2038 safe.

Code: Select all

Procedure.q GetUnixTime()
; Get the number of seconds since January 1, 1970 12:00am UTC as a 64 bit quad integer.
; Use https://www.unixtimestamp.com/ to test.
; REFERENCE: https://stackoverflow.com/questions/20370920/convert-current-time-from-windows-to-unix-timestamp

#UNIX_TIME_START = $019DB1DED53E8000 ; January 1, 1970 (start of Unix epoch) in "ticks"
#TICKS_PER_SECOND = 10000000 ; A tick is 100ns

Protected ft.q
GetSystemTimeAsFileTime_( @ft ) ; Returns ticks in UTC.
ProcedureReturn ( ft - #UNIX_TIME_START ) / #TICKS_PER_SECOND
EndProcedure

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 4:00 am
by idle
As far as Im aware DateUTC is already quad, does this work too?

Code: Select all

CompilerIf #PB_Compiler_Version <= 604 
  CompilerSelect #PB_Compiler_OS ;for compatiblity with 6.04lts mk-soft
    CompilerCase #PB_OS_Windows
      CompilerIf #PB_Compiler_32Bit
        ImportC "" 
          DateUTC.q(t=0) As "_time"  
        EndImport 
      CompilerElse   
        ImportC "" 
          DateUTC.q(t=0) As "time"  
        EndImport 
      CompilerEndIf
    CompilerCase #PB_OS_Linux
      Procedure.q DateUTC()
        ProcedureReturn time_(#Null)
      EndProcedure
    CompilerCase #PB_OS_MacOS
      ImportC ""
        CFAbsoluteTimeGetCurrent.d()
      EndImport
      Procedure.q DateUTC()
        ProcedureReturn CFAbsoluteTimeGetCurrent() + Date(2001, 1, 1, 0, 0, 0)
      EndProcedure
  CompilerEndSelect
CompilerEndIf 

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 9:59 am
by Little John

Code: Select all

; cross-platform code
; tested with PB 5.73, 6.04, 6.21

ImportC ""
   time(*tm=#Null)
EndImport

Procedure.q UTC ()
   ; return value: current UTC in PureBasic format
   ProcedureReturn time()
EndProcedure


; -- Demo
Debug FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", UTC())

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 10:05 am
by idle
Little John wrote: Thu Sep 11, 2025 9:59 am

Code: Select all

; cross-platform code
; tested with PB 5.73, 6.04, 6.21

ImportC ""
   time(*tm=#Null)
EndImport

Procedure.q UTC ()
   ; return value: current UTC in PureBasic format
   ProcedureReturn time()
EndProcedure


; -- Demo
Debug FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", UTC())
We have DateUTC() native

Code: Select all

Debug FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", DateUTC())

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 10:11 am
by Little John
idle wrote: Thu Sep 11, 2025 10:05 am We have DateUTC() native
I know, but it's only implemented in PB 6.10+.

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 10:21 am
by idle
Little John wrote: Thu Sep 11, 2025 10:11 am
idle wrote: Thu Sep 11, 2025 10:05 am We have DateUTC() native
I know, but it's only implemented in PB 6.10+.
yes that's why posted the code which also has a the fix for #PB_OS_MacOS (mk-soft)

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 2:01 pm
by Axeman
I'm currently using PureBasic 5.73 LTS (Windows - x86), as newer versions of Purebasic are a disaster with some of the software I've developed. I haven't had the opportunity to test out any new functions, such as DateUTC().

Re: Get current UNIX UTC time (Windows)

Posted: Thu Sep 11, 2025 8:31 pm
by infratec
Axeman wrote: Thu Sep 11, 2025 2:01 pmI'm currently using PureBasic 5.73 LTS (Windows - x86), as newer versions of Purebasic are a disaster with some of the software I've developed.
You should sort out the problems of your old Software :wink: