Table of Contents
UnixTime or Epoch Time
Many computer systems store and process time as the elapsed seconds since 01/01/1970 00:00:00 and this method is commonly termed “unixtime” and it accepts the output of the Now() function directly. This function's counterpart is the HumanTime function - Humantime() and Unixtime() translate each-others outputs.
Unixtime removes much of the hassle in dealing with date formats as they are only converted back to “human readable” when needed and will use the date format of the locale. Any date numbers you generate can be checked against this web tool. Unixtime is not UTC implicitly. It can easily be made to be, but as it is calculated from local time, it will only be UTC if the time on the relevant system is UTC.
The following function for MMBasic returns the number of seconds since midnight on 1st January 1970 as an integer. It correctly takes account of leap years in its calculations but leap seconds are ignored - this conforms with the established method of calculating UnixTime as leap seconds are arbitrary and not generally predictable. Because of this, very small errors may exist when the output is compared to astronomical clocks.
There is zero sanity checking performed on the input. If you try to crash it you'll probably succeed. MMbasic uses +/-63bits to store integers, consequently, this function is immune to the Geek's Millennium until at least the year 292Billion.
The argument string is a concatenate of DATE, a space and the TIME e.g. “24-01-1999 17:01:58” (The associated Now() function produces correctly formatted strings).
Syntax:
UnixTime(“DD-MM-YYYY HH:MM:SS”)
Example Usage:
x%=UnixTime(“20-08-1995 06:00:00”)
Print UnixTime(Now())
Dependencies:
<code>
're-write SEP2024 uses pure mathematical algorithm here: 'https://howardhinnant.github.io/date_algorithms.html#days_from_civil
Function UnixTime(dt$) As Integer Local Integer era,yoe,doy,doe,y,m,d Local dd$,tt$ m=Split(dt$," "):dd$=SP$(1):tt$=SP$(2) m=Split(dd$,"-"):d=Val(SP$(1)):m=Val(SP$(2)):y=Val(SP$(3)) If m<=2 Then y=y-1 If y>=0 Then era=y\400 Else era=(y-399)\400 End If yoe=y-era*400 If m>2 Then doy=(153*(m-3)+2)\5+d-1 Else doy=(153*(m+9)+2)\5+d-1 End If doe=yoe*365+yoe\4-yoe\100+doy m=Split(tt$,":") UnixTime=86400*(era*146097+doe-719468)+Val(SP$(1))*3600+Val(SP$(2))*60+Val(SP$(3)) End Function
See Also:
HumanTime()
Now()