#include "sch_parser.h"

#if 0
void utc_time_updt(uint32_t *c_utc_epoch, uint32_t *new_utc_epoch, uint32_t adj_sec, utc_time_updt_e mode)
{
   

    //Update the New UTC date and time to current value
    memcpy(new_utc_epoch,c_utc_epoch,sizeof(uint32_t));

    //When UTC time has to be advanced by seconds
    if(UTC_SEC_ADD == mode)
    {    
        //Add seconds 
      *new_utc_epoch += adj_sec;    
        }
    
        else {
            //Subtract seconds
            *new_utc_epoch -=  adj_sec;
        }
    

#if 0
    printf("Previous UTC Date and Time - %hhu-%hhu  %hhu:%hhu:%hhu\n",\
            c_utc->utc_date.date, c_utc->utc_date.month, c_utc->utc_time.hour,c_utc->utc_time.min,c_utc->utc_time.sec);
    printf("Adjusted seconds is %hhu\n",adj_sec);
    printf("Adjusted UTC Date and Time - %hhu-%hhu  %hhu:%hhu:%hhu\n",\
            new_utc->utc_date.date, new_utc->utc_date.month, new_utc->utc_time.hour,new_utc->utc_time.min,new_utc->utc_time.sec);
#endif

}

#endif
void conv_time_to_sec(sys_time_s *utc_t, int32_t *sec)
{
    *sec = ((utc_t->hour*3600) + (utc_t->min*60) + (utc_t->sec));
}

void conv_sec_to_time(int32_t sec, sys_time_s *utc)
{
    utc->hour = sec / 3600;
    utc->min = (sec - (utc->hour*3600)) / 60;
    utc->sec = (sec - (utc->hour*3600) - (utc->min*60));
}

void conv_date_time_to_sec_since_epoch(evnt_time *sch_evnt_info, uint32_t *sec_var)
{
    struct tm time_in = {0};
    time_in.tm_year = 2023 - 1900;
    time_in.tm_mon = sch_evnt_info->month - 1;
    time_in.tm_mday = sch_evnt_info->day;
    time_in.tm_hour = sch_evnt_info->hour;
    time_in.tm_min = sch_evnt_info->min;
    time_in.tm_sec = sch_evnt_info->sec;

    *sec_var = mktime(&time_in);
}

void conv_epoch_to_localtime(evnt_time *cur_time, uint32_t epoch)
{

    struct tm * timeinfo;

    time_t epoch_time_as_time_t = (time_t)epoch;

    timeinfo = localtime(&epoch_time_as_time_t);

    cur_time->year=timeinfo->tm_year+1900;
    cur_time->month=timeinfo->tm_mon+1;
    cur_time->day=timeinfo->tm_mday;
    cur_time->hour=timeinfo->tm_hour; 
    cur_time->min=timeinfo->tm_min;
    cur_time->sec=timeinfo->tm_sec;

}
