Proton BASIC Compiler - Calculate UNIXtime


  • PicŪ Basic


  • Calculate UNIXtime

    A simple program to calculate UNIX time from any date and time.

    UNIX time is the period since midnight on the 1st January 1970 in seconds. The following program calculates this from inputs of year/month/day/hour/minute/second.
    It allows for leap years. The program is well documented so further explanation is probably unnecessary.

    Code:
    '****************************************************************
    '*  Name    : UNIXtime.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2020 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/06/2020                                         *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    
    'Strip the string to get year, month, day1, hour, minute, second into Long Variables
    
    ;-------------------------------------------------------------------------------
    ;**** Added by Fuse Configurator ****
    ; Use the Fuse Configurator plug-in to change these settings
    
    Device = 18F452
    Declare Xtal 4
    Declare Reminders Off
    @ CONFIG_REQ = 0 ; Override Compiler's configuration settings
    Asm-
    __Config Config1H, 0x02 ;OSC_HS & FCMEN_OFF & IESO_OFF 
    __Config Config2L, 0x1F ;PWRT_OFF & BOREN_SBORDIS & BORV_3 
    __Config Config2H, 0x1E ;WDT_OFF & WDTPS_32768 
    __Config Config3H, 0x81 ;CCP2MX_PORTC & PBADEN_OFF & LPT1OSC_OFF & MCLRE_ON 
    __Config Config4L, 0x81 ;STVREN_ON & LVP_OFF & XINST_OFF & DEBUG_OFF 
    __Config Config5L, 0x07 ;CP0_OFF & CP1_OFF & CP2_OFF 
    __Config Config5H, 0xC0 ;CPB_OFF & CPD_OFF 
    __Config Config6L, 0x07 ;WRT0_OFF & WRT1_OFF & WRT2_OFF 
    __Config Config6H, 0xE0 ;WRTC_OFF & WRTB_OFF & WRTD_OFF 
    __Config Config7L, 0x07 ;EBTR0_OFF & EBTR1_OFF & EBTR2_OFF 
    __Config Config7H, 0x40 ;EBTRB_OFF 
    Endasm-
    Declare Reminders On
    
    ;**** End of Fuse Configurator Settings ****
    ;-------------------------------------------------------------------------------
    
    All_Digital =true
    
    Declare LCD_Type 0                'text type
    Declare LCD_DTPin PORTD.4         'assigns data lines to B4..7  
    Declare LCD_ENPin PORTE.1         'enable pin
    Declare LCD_RSPin PORTE.0         'RS line pin
    Declare LCD_Interface 4           '4 or 8 line interface
    Declare LCD_Lines 2               'lines in the display
    Declare Float_Rounding = On
    Declare Float_Display_Type = LARGE
    
    Dim Year As Dword
    Dim Month As Dword
    Dim Day As Dword
    Dim Hour As Dword
    Dim Minute As Dword
    Dim Second As Dword
    Dim AddDays1 As Dword
    Dim YearDiff As Byte             'good for a long while otherwise use word
    Dim AddLeap As Byte
    Dim Count1 As Byte
    dim UNIXtime as dword
    
    GoTo Main
    
                             'look up days from previous months
    Dim AddDays As Code = 0,31,59,90,120,151,181,212,243,273,304,334
    
    GetUNIX:                                         
           Dec Year                                  'current year incomplete
           Dec Month                                 'current month incomplete
           Dec Day                                   'current day incomplete
           YearDiff = 0                              'reset variables in loop                         
           AddLeap = 0  
            
           For Count1 = 1970 To Year                 'find the no. of years
               Inc YearDiff                          'and the leap days
               If Count1 // 4 = 0 Then Inc AddLeap
           Next
           Day = Day + (YearDiff) * 365 + AddLeap    'days based on 1970 to now
           If (Year + 1)//4 = 0 Then                 'fix if after Feb in this year
              If Month > 2 Then Inc Day              'if it's a leap year
           EndIf                                                                  
           AddDays1 = CRead16 AddDays [Month] + Day1 'update days due to months gone                                                
           Hour = Day * 24 + Hour                    'total hours
           Minute = Hour * 60 + Minute               'total minutes
           Unixtime = Minute * 60 + Second           'total seconds = UNIXtime
    Return
     
    Main:
    'strip the string to extract date and time
    'for the test I'll just have assigned values
        Year = 2020: Day = 6: Month = 6: Hour = 11: Minute = 28: Second = 5
        GoSub GetUNIX
        Print At 1,1,Dec unixtime, "   "
    End