Hello,
I need your help. Im using the RTC modul to show the time on a display. Now I want to calculate the difference of two times. The first time is given by a String. And I want to know how much is the difference from the current time.
I have write the code in c, but in Arduino it is a bit different:
First I convert the String to an time object:
void strToTime(char* t[],char* d[]){
struct tm newyear;
int minute,hour,day,month,year;
char dtm[100];
strcpy(dtm,t);
strcat(dtm," ");
strcat(dtm,d);
sscanf(dtm,"%02d:%02d %02d.%02d.%d",&hour,&minute,&day,&month,&year);
newyear.tm_hour = hour ; newyear.tm_min = minute; newyear.tm_sec = 0;
newyear.tm_mon = month - 1; newyear.tm_mday = day; newyear.tm_year = year - 1900; newyear.tm_isdst = -1;
mktime(&newyear);
}
Know get the difference with "difftime":
dif = difftime(time1,now);
How could I do it with Arduino?
I have write the code in c, but in Arduino it is a bit different:
Different mostly in that it is programmed in C++ rather than C.
What have you tried ?
The whole C library - including difftime() is available to you.
This is my Code in c and I will use it for Arduino.
Convert String to time:
void strToTime(char* t[],char* d[]){
struct tm newyear;
int minute,hour,day,month,year;
char dtm[100];
strcpy(dtm,t);
strcat(dtm," ");
strcat(dtm,d);
sscanf(dtm,"%02d:%02d %02d.%02d.%d",&hour,&minute,&day,&month,&year);
newyear.tm_hour = hour ; newyear.tm_min = minute; newyear.tm_sec = 0;
newyear.tm_mon = month - 1; newyear.tm_mday = day; newyear.tm_year = year - 1900; newyear.tm_isdst = -1;
mktime(&newyear);
}
char *time1;
time1= strToTime(time1,date);
And calculate difference:
double dif = difftime(time1,t_now);
But in Arduino. Im not able to convert the String time to a time object.
Since I don't use Strings as a matter of principle, I can't help you.
Just use standard c strings, which is what the c library expects. You can probably copy your String into a char array.
With Strings I mean of course Char array , sorry..
What happens when you try?
Don't you need to assign mktime() to something? You are currently just throwing it away.
If you were just dealing with hours, minutes, and seconds, then you would have already worked out how to do it.
But you are using years, months, and days as well, and that makes it much harder.
This function will do most of the hard work for you.
int32_t daycount(int16_t y, int8_t m, int8_t d) {
// Input is year, month, and day, in that order.
// For contemporary years, use the four-digit year number.
// The return value is a day number using an "endless" count of days.
// sanity checking of input
if ((y<-30000)||(y>30000)) return -999999999;
if ((m<1)||(m>12)) return -999999999;
if ((d<1)||(d>31)) return -999999999;
// pretend Jan. and Feb. belong to previous year
if (m<=2) {
y--;
m+=12;
}
// day count is unimaginatively named "n"
int32_t n = 365 * (int32_t)y;
// next we calculate leap days
int16_t x = y >> 2; // to get floor(y/4) and not trunc(y/4)
n += x; // 4-year rule
x = ((x+8000)/25)-320; // workaround for braindead C/C++ division
// now x equals floor(y/100)
n -= x; // 100-year rule
x >>= 2; // make it floor(y/400)
n += x; // 400-year rule
// next we take care of the months
n += (31 * (int16_t)m);
// adjust for the 30-day months
if (m>11) n-=4;
else if (m>9) n-=3;
else if (m>6) n-=2;
else if (m>4) n-=1;
// next we take care of the day of the month
n += d;
// return n; // this line is for testing purposes only
// finally, we add or subtract a constant
return (n + 1721026); // day 0 == -4713-11-24(Mon) (Julian Day at noon)
return (n - 34); // day 0 == 0000-01-01(Sat)
return (n - 400); // day 0 == 0001-01-01(Mon) (think REXX)
return (n - 693993); // day 0 == 1899-12-30(Sat) (think MS Excel)
return (n - 719562); // day 0 == 1970-01-01(Thu) (think Unix time)
return (n - 730519); // day 0 == 2000-01-01(Sat)
}