STM32F103C8T6_Internal_RTC

Dependencies:   mbed-STM32F103C8T6 mbed

Fork of STM32F103C8T6_GPRS_TCP by Shivanand Gowda

Time.cpp

Committer:
shivanandgowdakr
Date:
2018-07-13
Revision:
13:bf9a5ca067bf

File content as of revision 13:bf9a5ca067bf:

#include "mbed.h"
#include "Time.h"
#include <string>

int Date_Time_Setting(struct tm curt,char *str)
{
    int len=0;
    len= strlen(str);
   // printf("len = %d",len);
    if(len < 14)
        return 0;

    curt.tm_mday = ( str[0]-0x30)*10+(str[1]-0x30);
    curt.tm_mon  = ( str[2]-0x30)*10+(str[3]-0x30) -1;
    curt.tm_year = (( str[4]-0x30)*1000 + (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30)) -1900 ;

    curt.tm_hour=(str[8]-0x30)*10+(str[9]-0x30);
    curt.tm_min=(str[10]-0x30)*10+(str[11]-0x30);
    curt.tm_sec=(str[12]-0x30)*10+(str[13]-0x30);

    time_t epoch = mktime(&curt);
    if (epoch == (time_t) -1) {
        error("Error in clock setting\n");
        // Stop here
    }
    set_time(epoch);
    return 1;
}


int iSetTerminalTime(char *tstring)
{
    int ret=0;
    struct tm curt;

    ret = Date_Time_Setting(curt,tstring);

    if(ret == 1) {
         wait(1);
     
             printf("Date Time Set succesfully\r\n");

    } else {
      
             printf("Date Time Set Failure \r\n");

    }
    wait(2);
  

    return ret;
}


int chk_time (char *str)
{
    int HH,MM,SS;
    HH=(str[0]-0x30)*10+(str[1]-0x30);
    MM=(str[2]-0x30)*10+(str[3]-0x30);
    SS=(str[4]-0x30)*10+(str[5]-0x30);
    if ( HH < 0 || HH > 23 || MM < 0 || MM > 59 || SS < 0 || SS > 59 )
        return -1;
    return 1;
}

int chk_date (char *str)
{
    int epos_date=0,epos_month=0,epos_year=0;
    epos_date  = ( str[0]-0x30)*10+(str[1]-0x30);
    epos_month = ( str[2]-0x30)*10+(str[3]-0x30);
    epos_year  = ( str[4]-0x30)*1000+ (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30);


    if ( epos_month < 1 || epos_date < 1 || epos_date > 31 || epos_month > 12  ||  epos_year < 2008 ) return ERROR ;

    else if(epos_month == 1 || epos_month == 3 || epos_month == 5 || epos_month == 7 ||                                                                     epos_month == 8 || epos_month ==10 ||epos_month == 12) {

        if (epos_date > 31)
            return -1;
    }

    else  if (epos_month == 4 || epos_month == 6 || epos_month == 9 || epos_month == 11) {

        if (epos_date > 30)
            return -1;
    }

    else if  (epos_month == 2 )

    {
        if ( !(epos_year%400) || (epos_year%100 != 0 && epos_year%4==0 ) ) {
            if (epos_date > 29 ) return -1;
        }

        else  if( epos_date > 28 ) return -1;
    }
    return 1;
}

void  Get_Date_Time(char *date_string,char *time_string, char *DTSTRING)
{
    time_t curr_time;
    tm * curr_tm;
    time(&curr_time);
    curr_tm = localtime(&curr_time);
    strftime(date_string,10,"%Y%m%d",curr_tm);
    strftime(time_string,10,"%H%M%S",curr_tm);
    strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm);
}

void  Get_Date_Time(char *date_string,char *time_string)
{
    time_t curr_time;
    tm * curr_tm;
    time(&curr_time);
    curr_tm = localtime(&curr_time);
    strftime(date_string,10,"%Y/%m/%d",curr_tm);
    strftime(time_string,10,"%H:%M:%S",curr_tm);

  
}

void  Get_Date_Time()
{
    time_t curr_time;
    tm * curr_tm;
    time(&curr_time);
    char date_string[12]={'\0'};
    char time_string[12]={'\0'};
    curr_tm = localtime(&curr_time);
    strftime(date_string,10,"%Y/%m/%d",curr_tm);
    strftime(time_string,10,"%H:%M:%S",curr_tm);
    printf("Date : %s\r\n",date_string);
    printf("Time : %s\r\n",time_string);
  
}

void  Get_Date_Time( char *DTSTRING)
{
    time_t curr_time;
    tm * curr_tm;
    time(&curr_time);
    curr_tm = localtime(&curr_time);  
    strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm);
    
}

void  Get_Date_Time_Trns( char *DTSTRING)
{
    time_t curr_time;
    tm * curr_tm;
    time(&curr_time);
    curr_tm = localtime(&curr_time);  
    strftime(DTSTRING,20,"%d%m%Y%H%M%S",curr_tm);
}