DateUtc

Share your advanced PureBasic knowledge/code with the community.
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

DateUtc

Post by idle »

Get UTC time
linux OSX support added Infratec

Code: Select all

CompilerSelect #PB_Compiler_OS
  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

CompilerIf #PB_Compiler_IsMainFile
  Debug "UTC" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", DateUTC())
  Debug "LOCAL" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", Date())
CompilerEndIf

we just came out of daylight savings
UTC 03/04/2023 02:52:43
LOCAL 03/04/2023 14:52:43
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: DateUtc

Post by Lunasole »

Nice. But seems didn't work with x86 compilers on Windows (throws unresolved externals for some reason), only x64.
At least in my case.
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: DateUtc

Post by Thunder93 »

Same.
Lunasole wrote: Mon Apr 03, 2023 3:09 am Nice. But seems didn't work with x86 compilers on Windows (throws unresolved externals for some reason), only x64.
At least in my case.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: DateUtc

Post by idle »

Lunasole wrote: Mon Apr 03, 2023 3:09 am Nice. But seems didn't work with x86 compilers on Windows (throws unresolved externals for some reason), only x64.
At least in my case.
fixed
infratec
Always Here
Always Here
Posts: 7576
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: DateUtc

Post by infratec »

Extended to be crossplatform:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    CompilerIf #PB_Compiler_32Bit
      ImportC "" 
        Date_UTC(t=0) As "_time"  
      EndImport 
    CompilerElse   
      ImportC "" 
        Date_UTC(t=0) As "time"  
      EndImport 
    CompilerEndIf
    
    Procedure.q DateUTC()
      ProcedureReturn Date_UTC()
    EndProcedure
    
  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


CompilerIf #PB_Compiler_IsMainFile
  Debug "UTC" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", DateUTC())
  Debug "LOCAL" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", Date())
CompilerEndIf
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: DateUtc

Post by idle »

infratec wrote: Mon Apr 03, 2023 7:25 am Extended to be crossplatform:

Code: Select all

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit
CompilerEndIf


CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Windows
    CompilerIf #PB_Compiler_32Bit
      ImportC "" 
        Date_UTC(t=0) As "_time"  
      EndImport 
    CompilerElse   
      ImportC "" 
        Date_UTC(t=0) As "time"  
      EndImport 
    CompilerEndIf
    
    Procedure.q DateUTC()
      ProcedureReturn Date_UTC()
    EndProcedure
    
  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


CompilerIf #PB_Compiler_IsMainFile
  Debug "UTC" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", DateUTC())
  Debug "LOCAL" + #TAB$ + FormatDate("%dd/%mm/%yyyy %hh:%ii:%ss", Date())
CompilerEndIf
Thanks for that
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: DateUtc

Post by Kwai chang caine »

Thanks for sharing this usefull code worked here :wink:
Mainly for europeans people, who never do like the others :cry:
Government promise stop this history of summer time, but again it's not the case now :evil:
ImageThe happiness is a road...
Not a destination
benubi
Enthusiast
Enthusiast
Posts: 215
Joined: Tue Mar 29, 2005 4:01 pm

Re: DateUtc

Post by benubi »

Very useful for web applications & interfaces!
Kwai chang caine wrote: Mon Apr 24, 2023 10:21 am Thanks for sharing this usefull code worked here :wink:
Mainly for europeans people, who never do like the others :cry:
Government promise stop this history of summer time, but again it's not the case now :evil:
And that's only the summer time problem. There are also leap seconds now!
And on an other front in my head megabytes are still megabytes and not mibibytes (or how they are called now); my head and my tongue won't articulate mibibyte except when I'm being sarcastic or joking. :lol:
And I'm rather sure there are worse things to come in the near future like 'misgendering' technical words. I refuse to imagine the manuals and the lawsuits... :?
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: DateUtc

Post by idle »

I was binge watching red dwarf last week so l'm already ahead on the pronouns, I'm just the Cat. Cool Attractive and Tanned,
Image
so if I say, Yo Sheit, I'm meaning youse human whats it's faces, is that OK. Right gotta go meowww Pussies. that should be offensive enough for the pronoun demanding lot. :lol:
Post Reply