Get Unix timestamp (GMT)

Just starting out? Need help? Post your questions and find answers here.
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Get Unix timestamp (GMT)

Post by Xanos »

What is the easiest way to get the (GMT/UTC) unix timestamp (seconds from 1970) in Purebasic in a crossplattform application?
The PB Date functions do not support any sort of timezone offset as far as I can see and always give the seconds since 1970 in the local timezone.
However, I need the real timestamp in GMT, no matter the local timezone.

I found some functions using WinAPI, e.g. GetTimeZoneInformation_(), GetSystemTime_() and GetLocalTime_(), but I need a crossplatform solution :)
Last edited by Xanos on Thu Jun 01, 2017 8:10 am, edited 1 time in total.
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Get Unix timestamp (GMT)

Post by Shardik »

This is a cross-platform example to query the current GMT/UTC seconds since 1970-01-01 00:00:00. On Linux and MacOS it utilizes the date command, on Windows GetSystemTime_().

Greenwich Mean Time (GMT) is equivalent to the coordinated universal time (UTC):
Wikipedia wrote:Greenwich Mean Time (GMT) is the mean solar time at the Royal Observatory in Greenwich, London. GMT was formerly used as the international civil time standard, now superseded in that function by Coordinated Universal Time (UTC). Today GMT is considered equivalent to UTC for UK civil purposes (but this is not formalised) and for navigation is considered equivalent to UT1 (the modern form of mean solar time at 0° longitude); these two meanings can differ by up to 0.9 s. Consequently, the term GMT should not be used for precise purposes.[1]
I have tested the code example successfully on these operating systems:
- Linux Mint 18.1 x64 with Cinnamon and PB 5.60 x64
- MacOS 10.6.8 'Snow Leopard' with PB 5.44 x86
- Windows 7 SP1 x64 with PB 5.44 x86

Code: Select all

EnableExplicit

Define Seconds.I

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  Define SystemTime.SYSTEMTIME

  GetSystemTime_(@SystemTime)
  Seconds = Date(SystemTime\wYear, SystemTime\wMonth, SystemTime\wDay,
    SystemTime\wHour, SystemTime\wMinute, SystemTime\wSecond)
CompilerElse
  Define ProgramID.I

  ProgramID = RunProgram("date", "+%s", "", #PB_Program_Open | #PB_Program_Read)
 
  If ProgramID
    While ProgramRunning(ProgramID)
      Seconds = Val(ReadProgramString(ProgramID) + #CR$)
    Wend

    CloseProgram(ProgramID)
  EndIf
CompilerEndIf

MessageRequester("Seconds since 1970-01-01 00:00:00",
  Str(Seconds) + " (GMT = UTC+0)" + #CR$ +
  FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss", Seconds))
Last edited by Shardik on Thu Jun 01, 2017 12:01 pm, edited 1 time in total.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Get Unix timestamp (GMT)

Post by wilbert »

An alternative which seems to work cross platform.

Code: Select all

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

Debug time()
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: Get Unix timestamp (GMT)

Post by Xanos »

Thank you both very much! :)

The Import of the time() function is perfect and so easy :)
Last edited by Xanos on Fri Jun 02, 2017 1:11 pm, edited 1 time in total.
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Re: Get Unix timestamp (GMT)

Post by kenmo »

wilbert wrote:

Code: Select all

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

Debug time()
Nice :shock:
Guaranteed to work on Windows/Linux/Mac ??
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3943
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Get Unix timestamp (GMT)

Post by wilbert »

kenmo wrote:Nice :shock:
Guaranteed to work on Windows/Linux/Mac ??
It is a documented function on all three OS so it should work.
I tested it on Windows 10, MacOS Sierra and Linux Mint.
Windows (x64)
Raspberry Pi OS (Arm64)
Post Reply