UnixTimeDate

A small module to convert a Unix timestamp to Readable Date/time.

Defines a structure to hold the date after the conversion and names it SntpTime. A conversion from a Proton version by Gabi

The variables in the structute are:-

    Sec As Byte
    Min As Byte 
    Hour As Byte 
    Year As Word 
    Month As Byte
    Day As Byte
    Dow As Byte
    DowStr As String * 3

Demo code

    Include "usart.bas"
    Include "convert.bas"
    Include "unixtimedate.bas"


    Dim unixtime As LongWord

    SetBaudrate(br19200)

    unixtime = 34857915 

    UnixToDate(unixtime)

    USART.Write(SntpTime.DowStr,DecToStr(SntpTime.Day),32,DecToStr(SntpTime.Month),32)
    USART.Write(DecToStr(SntpTime.Year),32,DecToStr(SntpTime.Hour),":",DecToStr(SntpTime.Min))
    USART.Write(":",DecToStr(SntpTime.Sec))

The module

{
****************************************************************
*  Name    : UnixTimeDate.BAS                                  *
*  Author  : Gabi Mihaila / Tim Box                            *
*  Notice  : No Copyright                                      *
*          :                                                   *
*  Date    : 17-Oct-06                                         *
*  Version : 1.0                                               *
*  Notes   :                                                   *
*          :                                                   *
****************************************************************
}

Module UnixTimeDate

    Structure  UnixSntpTime
    Sec As Byte
    Min As Byte
    Hour As Byte
    Year As Word 
    Month As Byte
    Day As Byte
    Dow As Byte
    DowStr As String * 3
    End Structure 

 Dim    
    TempDay As LongWord,
    tempa As LongWord

 Public Dim SntpTime As UnixSntpTime

Const    
    Months_Days(13) As Byte = ( 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
    DOWStrConst(7) As String = ("Thu","Fri","Sat","Sun","Mon","Tue","Wed")

Public Sub  UnixToDate(PUnixTime As LongWord) 
{ 
    check with    http://www.onlineconversion.com/PUnixTime.htm 
    Example --> PUnixTime = 1160906700 ; input Unix Time Stamp and call this sub
}
	 // seconds:
	 SntpTime.Sec = PUnixTime Mod 60 //;
	 PUnixTime = PUnixTime / 60  //;
	 // minutes:
	 SntpTime.Min = PUnixTime Mod 60
	 PUnixTime = PUnixTime / 60
	 // hours:
	 SntpTime.Hour = PUnixTime Mod 24
	 PUnixTime = PUnixTime / 24
     // here PUnixTime equals number of days since Thu 1st Jan 1970 
     // so we can find the day of the week like this:
     SntpTime.Dow = PUnixTime Mod 7     ' DOW       0,  1,  2,  3,  4,  5,  6
                               ' DOW_STR  Thu Fri Sat Sun Mon Tue Wed  

     SntpTime.DowStr = DOWStrConst(SntpTime.Dow)

     //
	 SntpTime.Year = 1970         //  we start with the NTP starting year offset
	 // year:
	 While 1 = 1
    	TempDay = 365              //  say its a normal year (not leap)
		If (SntpTime.Year And $03) = 0 Then
			TempDay = 365 + 1      // LEAP Year
        EndIf    
        If  TempDay > PUnixTime Then
            Break 
        EndIf
		PUnixTime = PUnixTime - TempDay 
		Inc (SntpTime.Year)
     Wend
	 // month:
     For SntpTime.Month = 1 To 12                            
		tempa = Months_Days(SntpTime.Month)
        If PUnixTime <= tempa Then
           Break
        EndIf
        PUnixTime = PUnixTime - tempa   
	 Next
	 // days: 
	 If TempDay = 366 And SntpTime.Month <= 2 Then
        SntpTime.Day = PUnixTime + 1
        Exit 
     ElseIf TempDay = 366 And SntpTime.Month > 2 Then
        SntpTime.Day = PUnixTime
        Exit 
     EndIf
     If TempDay = 365 And SntpTime.Month = 2 Then
        SntpTime.Day = PUnixTime + 1 
        If SntpTime.Day = 29 Then
           SntpTime.Day = 1
           SntpTime.Month = SntpTime.Month + 1
        EndIf
        Exit 
     EndIf
     SntpTime.Day = PUnixTime + 1
End Sub
End