Page 1 of 1
Get Unix timestamp (GMT)
Posted: Wed May 31, 2017 1:59 pm
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

Re: Get Unix timestamp (GMT)
Posted: Wed May 31, 2017 9:25 pm
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))
Re: Get Unix timestamp (GMT)
Posted: Thu Jun 01, 2017 6:06 am
by wilbert
An alternative which seems to work cross platform.
Code: Select all
ImportC ""
time(*tloc = #Null)
EndImport
Debug time()
Re: Get Unix timestamp (GMT)
Posted: Thu Jun 01, 2017 8:11 am
by Xanos
Thank you both very much!
The Import of the time() function is perfect and so easy

Re: Get Unix timestamp (GMT)
Posted: Thu Jun 01, 2017 2:39 pm
by kenmo
wilbert wrote:Code: Select all
ImportC ""
time(*tloc = #Null)
EndImport
Debug time()
Nice
Guaranteed to work on Windows/Linux/Mac ??
Re: Get Unix timestamp (GMT)
Posted: Thu Jun 01, 2017 3:00 pm
by wilbert
kenmo wrote:Nice
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.