mmbasic:humantime_function_to_return_a_human_readable_date_and_time_from_a_unixtime_number
Table of Contents
HumanTime() Function to return a human readable date and time from a unixtime number
This function returns a string which is the combined date and time from the unixtime number provided. It is the counterpart of the UnixTime function - Humantime() and Unixtime() translate each-others outputs.
A format option formats the date part as follows:
- DD-MM-YYYY when opt=0 or omitted (this matches DATE$)
- YYYY-MM-DD when opt<>0
The option can be omitted if required
Syntax:
HumanTime(ut,option)
Example Usage:
Print HumanTime(ut_from_log,1)
or
Print HumanTime(ut_my_birthday)
Dependencies: The following functions must be included in your program for HumanTime() to work correctly…
're-write SEP2024 uses pure mathematical algorithm here: 'https://howardhinnant.github.io/date_algorithms.html#days_from_civil Function HumanTime(ut As Integer,opt As Integer) As String Local Integer era,doe,yoe,y,doy,mp,d,m,z z=(ut\86400)+719468 If z >= 0 Then era=z\146097 Else era=(z-146096)\146097 End If doe=z-era*146097:yoe=(doe-doe\1460+doe\36524-doe\146096)\365:y=yoe+era*400:doy=doe-(365*yoe+yoe\4-yoe\100):mp=(5*doy+2)\153:d=doy-(153*mp+2)\5+1 If mp < 10 Then m=mp+3 Else m=mp-9 End If If m <= 2 Then y=y+1 If opt Then HumanTime=Str$(y)+"-"+ZPad$(m,2)+"-"+ZPad$(d,2) Else HumanTime=ZPad$(d,2)+"-"+ZPad$(m,2)+"-"+Str$(y) EndIf HumanTime=HumanTime+" "+ZPad$((ut Mod 86400)\3600,2)+":"+ZPad$((ut Mod 3600)\60,2)+":"+ZPad$(ut Mod 60,2) End Function
See Also:
UnixTime()
Now()
mmbasic/humantime_function_to_return_a_human_readable_date_and_time_from_a_unixtime_number.txt · Last modified: 2024/09/27 22:02 by captainboing