User Tools

Site Tools


mmbasic:humantime_function_to_return_a_human_readable_date_and_time_from_a_unixtime_number

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&#0091;,option&#0093;)

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…

  Function HumanTime(UUT As Integer,opt As Integer) As String
    ' take seconds since 1970-01-01 (unixtime) and converts to a human form date/time as
    ' if opt=0 (or not specified) the format is dd-mm-yyyy hh:mm:ss" to match the format of DATE$
    ' if opt<>0 the date part is returned as yyyy-mm-dd to aid sorting and avoid
    Local integer d,m,s,y,z,UT
    UT=UUT 'preserve original argument
    If UT<0 Then HumanTime$="":Exit Function
    y=1970:d=UT\86400:s=UT Mod 86400
    Do
      z=365+IsLeapYear(y)
      If d>=z Then
        d=d-z:y=y+1
      Else
        Exit Do
      EndIf
    Loop
    'here, Y is the current year and d the remaining days
    m=1' start at january
    ' days between the start of the year and the current month
    Do
      z=0
      Select Case m
        Case 1,3,5,7,8,10
          If d>=31 Then d=d-31:z=1
        Case 2
          If IsLeapYear(y)Then
            If d>=29 Then d=d-29:z=1
          Else
            If d>=28 Then d=d-28:z=1
          EndIf
        Case 4,6,9,11
          If d>=30 Then d=d-30:z=1
      End Select

      If z=1 Then
        m=m+1
      Else
        d=d+1:Exit Do 'first day is 1 not 0
      EndIf
    Loop

    If opt<>0 Then
      HumanTime$=Str$(y)+"-"+ZPad$(m,2)+"-"+ZPad$(d,2)
    Else
      HumanTime$=ZPad$(d,2)+"-"+ZPad$(m,2)+"-"+Str$(y)
    EndIf
    y=s\3600:s=s-y*3600
    m=s\60:s=s-m*60
    HumanTime$=HumanTime$+" "+ZPad$(y,2)+":"+ZPad$(m,2)+":"+ZPad$(s,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/01/19 09:30 by 127.0.0.1