DATETIME$()

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 CMM2

Syntax:
DATETIME$(n)
EPOCH(DATETIME$)

Description:

DATETIME$(n)
Returns the date and time corresponding to the epoch number n (number of seconds that have elapsed since midnight GMT on January 1, 1970). 
The format of the returned string is “dd-mm-yyyy hh:mm:ss”. 
Use the text NOW to get the current datetime string, i.e. ? DATETIME$(NOW)
This function has the same time limits as standard 32 bit Unix time.
Valid Epoch numbers and corresponding dates are
-2147483648    13-12-1901 20:45:52 
to 
2147483647     19-01-2038 03:14:07

EPOCH(DATETIME$)
Returns the epoch number (number of seconds that have elapsed since midnight GMT on January 1, 1970) for the supplied DATETIME$ string. 
The format for DATETIME$ is “dd-mm-yyyy hh:mm:ss”. 
The full time including seconds is required. MIlliseconds are OK but are ignored, not rounded.
Use NOW to get the epoch number for the current date and time, i.e. ? EPOCH(NOW)

This function has slightly different limits compared with DATETIME$()
Valid Epoch numbers and corresponding dates are

-2145916800    01-01-1902 00:00:00   to  32503679999  31-12-2999 23:59:59
Year must be between 1902 and 2999 inclusive.

PRINT datetime$(0)
PRINT datetime$(NOW)
PAUSE 1000
PRINT datetime$(NOW)
PRINT datetime$(2147483647)
PRINT datetime$(-2147483648)

PRINT EPOCH(NOW)
PAUSE 1000
PRINT EPOCH(NOW)
PRINT EPOCH("01-01-1939 00:00:00")

Output:
01-01-1970 00:00:00
15-09-2020 12:57:31
15-09-2020 12:57:32
19-01-2038 03:14:07
13-12-1901 20:45:52
1600174652
1600174653
-978307200

These functions give a longer time span:

FUNCTION epochX(dt$) AS INTEGER
 LOCAL FLOAT d,m,y,hh,mm,ss
 LOCAL INTEGER jd, unixd, unixf
 d=VAL(MID$(dt$,1,2))
 m=VAL(MID$(dt$,4,2))
 y=VAL(MID$(dt$,7,4))
 hh=VAL(MID$(dt$,12,2))
 mm=VAL(MID$(dt$,15,2))
 ss=VAL(MID$(dt$,18,2))
 IF m < 3 THEN m = m+12:y=y-1
 jd= 2 - y\100 + y\400 + d + FIX(365.25*(Y+4716)) + FIX(30.6001*(M+1)) - 1524
 unixd=jd-2440588
 unixf=hh*60*60+mm*60+ss
 epochX=unixd*86400+unixf
END FUNCTION

FUNCTION DateTimeX$(u AS INTEGER)
 LOCAL INTEGER d,m,y,hh,mm,ss,l,i,j,n
 LOCAL INTEGER jd, unixd, unixf
 unixd=INT(u/86400)
 unixf=u MOD 86400
 IF unixd<0 THEN
   unixf=86400+unixf
 ENDIF
 hh= INT(unixf/3600)
 mm= INT((unixf MOD 3600)/60)
 ss=(unixf MOD 3600) MOD 60
 jd=unixd+2440588
 l = jd+68569    ' valid for Gregorian dates after Oct 15, 1582
 n = FIX((4*l)/146097)
 l = l-FIX((146097*n+3)/4)
 i = FIX((4000*(l+1))/1461001)
 l = l-FIX((1461*i)/4)+31
 j = FIX((80*l)/2447)
 d = l-FIX((2447*j)/80)
 l = FIX(j/11)
 m = j+2-(12*l)
 y = 100*(n-49)+i+l
 DateTimeX$ = STR$(d,2,0,"0")+"-"+STR$(m,2,0,"0")+"-"+STR$(y,4,0,"0")+" "
 DateTimeX$ = DateTimeX$ +STR$(hh,2,0,"0")+":"+STR$(mm,2,0,"0")+":"+STR$(ss,2,0,"0")
END FUNCTION

Last edited: 29 September, 2020