Shivanand Gowda / InternalRTC

Dependents:   Internal_RTC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RTC.cpp Source File

RTC.cpp

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