Shivanand Gowda / Mbed 2 deprecated STM32F103C8T6_Internal_RTC

Dependencies:   mbed-STM32F103C8T6 mbed

Fork of STM32F103C8T6_GPRS_TCP by Shivanand Gowda

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Time.cpp Source File

Time.cpp

00001 #include "mbed.h"
00002 #include "Time.h"
00003 #include <string>
00004 
00005 int Date_Time_Setting(struct tm curt,char *str)
00006 {
00007     int len=0;
00008     len= strlen(str);
00009    // printf("len = %d",len);
00010     if(len < 14)
00011         return 0;
00012 
00013     curt.tm_mday = ( str[0]-0x30)*10+(str[1]-0x30);
00014     curt.tm_mon  = ( str[2]-0x30)*10+(str[3]-0x30) -1;
00015     curt.tm_year = (( str[4]-0x30)*1000 + (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30)) -1900 ;
00016 
00017     curt.tm_hour=(str[8]-0x30)*10+(str[9]-0x30);
00018     curt.tm_min=(str[10]-0x30)*10+(str[11]-0x30);
00019     curt.tm_sec=(str[12]-0x30)*10+(str[13]-0x30);
00020 
00021     time_t epoch = mktime(&curt);
00022     if (epoch == (time_t) -1) {
00023         error("Error in clock setting\n");
00024         // Stop here
00025     }
00026     set_time(epoch);
00027     return 1;
00028 }
00029 
00030 
00031 int iSetTerminalTime(char *tstring)
00032 {
00033     int ret=0;
00034     struct tm curt;
00035 
00036     ret = Date_Time_Setting(curt,tstring);
00037 
00038     if(ret == 1) {
00039          wait(1);
00040      
00041              printf("Date Time Set succesfully\r\n");
00042 
00043     } else {
00044       
00045              printf("Date Time Set Failure \r\n");
00046 
00047     }
00048     wait(2);
00049   
00050 
00051     return ret;
00052 }
00053 
00054 
00055 int chk_time (char *str)
00056 {
00057     int HH,MM,SS;
00058     HH=(str[0]-0x30)*10+(str[1]-0x30);
00059     MM=(str[2]-0x30)*10+(str[3]-0x30);
00060     SS=(str[4]-0x30)*10+(str[5]-0x30);
00061     if ( HH < 0 || HH > 23 || MM < 0 || MM > 59 || SS < 0 || SS > 59 )
00062         return -1;
00063     return 1;
00064 }
00065 
00066 int chk_date (char *str)
00067 {
00068     int epos_date=0,epos_month=0,epos_year=0;
00069     epos_date  = ( str[0]-0x30)*10+(str[1]-0x30);
00070     epos_month = ( str[2]-0x30)*10+(str[3]-0x30);
00071     epos_year  = ( str[4]-0x30)*1000+ (str[5]-0x30)*100 + (str[6]-0x30)*10 + (str[7]-0x30);
00072 
00073 
00074     if ( epos_month < 1 || epos_date < 1 || epos_date > 31 || epos_month > 12  ||  epos_year < 2008 ) return ERROR ;
00075 
00076     else if(epos_month == 1 || epos_month == 3 || epos_month == 5 || epos_month == 7 ||                                                                     epos_month == 8 || epos_month ==10 ||epos_month == 12) {
00077 
00078         if (epos_date > 31)
00079             return -1;
00080     }
00081 
00082     else  if (epos_month == 4 || epos_month == 6 || epos_month == 9 || epos_month == 11) {
00083 
00084         if (epos_date > 30)
00085             return -1;
00086     }
00087 
00088     else if  (epos_month == 2 )
00089 
00090     {
00091         if ( !(epos_year%400) || (epos_year%100 != 0 && epos_year%4==0 ) ) {
00092             if (epos_date > 29 ) return -1;
00093         }
00094 
00095         else  if( epos_date > 28 ) return -1;
00096     }
00097     return 1;
00098 }
00099 
00100 void  Get_Date_Time(char *date_string,char *time_string, char *DTSTRING)
00101 {
00102     time_t curr_time;
00103     tm * curr_tm;
00104     time(&curr_time);
00105     curr_tm = localtime(&curr_time);
00106     strftime(date_string,10,"%Y%m%d",curr_tm);
00107     strftime(time_string,10,"%H%M%S",curr_tm);
00108     strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm);
00109 }
00110 
00111 void  Get_Date_Time(char *date_string,char *time_string)
00112 {
00113     time_t curr_time;
00114     tm * curr_tm;
00115     time(&curr_time);
00116     curr_tm = localtime(&curr_time);
00117     strftime(date_string,10,"%Y/%m/%d",curr_tm);
00118     strftime(time_string,10,"%H:%M:%S",curr_tm);
00119 
00120   
00121 }
00122 
00123 void  Get_Date_Time()
00124 {
00125     time_t curr_time;
00126     tm * curr_tm;
00127     time(&curr_time);
00128     char date_string[12]={'\0'};
00129     char time_string[12]={'\0'};
00130     curr_tm = localtime(&curr_time);
00131     strftime(date_string,10,"%Y/%m/%d",curr_tm);
00132     strftime(time_string,10,"%H:%M:%S",curr_tm);
00133     printf("Date : %s\r\n",date_string);
00134     printf("Time : %s\r\n",time_string);
00135   
00136 }
00137 
00138 void  Get_Date_Time( char *DTSTRING)
00139 {
00140     time_t curr_time;
00141     tm * curr_tm;
00142     time(&curr_time);
00143     curr_tm = localtime(&curr_time);  
00144     strftime(DTSTRING,20,"%Y%m%d%H%M%S",curr_tm);
00145     
00146 }
00147 
00148 void  Get_Date_Time_Trns( char *DTSTRING)
00149 {
00150     time_t curr_time;
00151     tm * curr_tm;
00152     time(&curr_time);
00153     curr_tm = localtime(&curr_time);  
00154     strftime(DTSTRING,20,"%d%m%Y%H%M%S",curr_tm);
00155 }
00156 
00157 
00158