Get current UNIX UTC time (Windows)

Share your advanced PureBasic knowledge/code with the community.
Axeman
User
User
Posts: 93
Joined: Mon Nov 03, 2003 5:34 am

Get current UNIX UTC time (Windows)

Post 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
User avatar
idle
Always Here
Always Here
Posts: 5921
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Get current UNIX UTC time (Windows)

Post 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 
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Get current UNIX UTC time (Windows)

Post 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())
User avatar
idle
Always Here
Always Here
Posts: 5921
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Get current UNIX UTC time (Windows)

Post 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())
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Get current UNIX UTC time (Windows)

Post 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+.
User avatar
idle
Always Here
Always Here
Posts: 5921
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Get current UNIX UTC time (Windows)

Post 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)
Axeman
User
User
Posts: 93
Joined: Mon Nov 03, 2003 5:34 am

Re: Get current UNIX UTC time (Windows)

Post 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().
infratec
Always Here
Always Here
Posts: 7625
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get current UNIX UTC time (Windows)

Post 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:
Post Reply