Welcome Guest, you are in: Login

Fruit Of The Shed

Navigation (MMBasic)






Search the wiki

»


Page History: UnixTime or Epoch Time

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: 2020/04/13 11:51


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". This function's counterpart is the HumanTime function and it accepts the output of the Now() function directly. Humantime() and Unixtime() translate each-others outputs.

Unixtime removes much of the hassle with dealing with date formats as they are only converted back to "human readable" when needed and so will use the date format of the locale. Any date numbers you generate can be checked against this web tool. Unixtime is not UTC. It may be, but as it is calculated from local time, it will only be UTC if the time on the relevant system is UTC e.g. raw time from a GPS receiver.

The following is a function to add this facility to MMBasic. It returns the number of seconds since midnight on 1st January 1970 as an integer. UnixTime 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 little sanity checking performed on the strings you provide. If you try to crash it you'll probably succeed. If the argument string does not consist of 2 groups of 3 delimited values, -1 is returned to indicate an error. MMbasic uses +/-63bits to store integers. Consequently, this function is immune to the Geek's Millennium until at least the year 292,000,000,000.

The argument string is a concatenate of DATE, a space and the TIME e.g. "24-01-1999 17:01:58". A format option allows the date part to be interpretted as follows:
DD-MM-YYYY when opt=0 or omitted
YYYY-MM-DD when opt<>0

The option can be omitted if required

Syntax:
UnixTime(datestrDD-MM-YYYY HH:MM:SS[,opt])

Example Usage:
x%=UnixTime("20-08-1995 06:00:00")

Print UnixTime(Now())

x%=UnixTime("1995-08-20 06:00:00",1)


Dependencies:


	Function UnixTime(HHT$,opt) As integer
  ' seconds since 01-01-1970. no checks on the argument format
  ' HT$ is DD-MM-YYYY HH:MM:SS or if opt=1 HT$ is YYYY-MM-DD HH:MM:SS
  ' Now() returns a suitable string
		Local integer n,s,y,m
		Local String DD$,TT$,HT$
                HT$=HHT$
		m=Split(HT$," ")
		If m<>2 then UnixTime=-1:Exit Function
		DD$=SP$(1):TT$=SP$(2)
		If Not IsDate(DD$) Then UnixTime=-1:Exit Function' Not a proper date
		If Not IsTime(TT$) Then UnixTime=-1:Exit Function' not a proper time	
		m=Split(DD$,"-")
		If m<>3 then UnixTime=-1:Exit Function
		If opt=0 then
                    y=Val(SP$(3))
                else
                    y=Val(SP$(1))
                EndIf
                s=0
		For n=1970 To y-1
			s=s+365+IsLeapYear(n)
		Next
		For n=1 To Val(SP$(2))-1
			Select Case n
				Case 1,3,5,7,8,10
					s=s+31
				Case 2					
					s=s+28+IsLeapYear(y)					
				Case 4,6,9,11
					s=s+30
			End Select
		Next
                If opt=0 Then
                    s=86400*(s+Val(SP$(1))-1)
                Else
                    s=86400*(s+Val(SP$(3))-1)
                EndIf
		m=Split(TT$,":")
		If m<>3 then UnixTime=-1:Exit Function
		s=s+(3600*Val(SP$(1))) + (60*Val(SP$(2))) + Val(SP$(3))
		UnixTime=s
	End Function

See Also:
HumanTime()
Now()
IsDate and IsTime functions (VB Work-A-Like)