View previous topic :: View next topic |
Author |
Message |
kmp84
Joined: 02 Feb 2010 Posts: 345
|
DateTime compare |
Posted: Mon Nov 09, 2020 6:33 am |
|
|
Hello All,
Can you point me some easy way to compare two datetime value?
I have date/time from RTC(DS1307) and data/time from RS232 device.
I want to know if date/time from device are equal or greater 30 min from DS1307 RTC.
Best Regards! |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Nov 09, 2020 7:28 am |
|
|
One way....
Kinda rough but you get to work out the code... !
1st create 8 bit( byte) variables....
rtc_yr
rtc_mt
rtc_dy
rtc_hr
rtc_mn
ser_yr
ser_mt
ser_dy
ser_hr
ser_mn
now 2, 16 bit vaiables
rtc_time_sum
ser_time_sum
rtc_time_sum = 60*rtc_hr+ rtc_mn // total # of minutes
ser_time_sum = 60*rtc_hr+ rtc_mn // total # of minutes
now simply compare
yr to yr
mt to mt
dy to dy
then rtc_time_sum to ser_time_sum+30
The last compare decides if the 2 times are =>30 minutes apart.
Probably a better way but this should be fast and easy to do...
Jay |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Mon Nov 09, 2020 7:38 am |
|
|
Thanks mr. temtronic,
But if date/time ser= 2020/12/31 23:55 and RTC=2021/01/01 01:05 it will not trigger to true value? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Nov 09, 2020 7:39 am |
|
|
The other way is to use the time.h/time.c functions. These provide Unix
compatible time functions. So you can convert 'time' in ASCII format into a
int32 value, and convert the other way. You can then do comparisons etc.,
on the value. difftime, gives you the difference (in seconds) between two
time values. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Nov 09, 2020 7:50 am |
|
|
re:
Quote: |
but if date/time ser= 2020/12/31 23:55 and RTC=2021/01/01 01:05
it will not trigger to true value? |
As I said 'rough' code but easy enough to add in 'rollover' conditions.
Comparing 'DOW' might help. Convertng yr-mth-day into a 16-bit might help...
I'd have to lookup 24 year old code to see how I did the compares...sigh, time flies, no pun intended.
Otherwise Mr. T's UNIX solution does work, though I suspect a lot more codespace is used. Not a problem if the PIC is 'big'. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Mon Nov 09, 2020 8:05 am |
|
|
Thank you mr. temtronic, mr. Ttelmah!
I think "time.c" and "time.h" will do the my job.
Best Regards! |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Wed Nov 11, 2020 1:51 am |
|
|
Hi again,
I want to ask you why in <time.h>
Code: |
/* API Types*/
typedef signed int32 time_t;
|
time_t are signed type? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Nov 11, 2020 2:15 am |
|
|
That is standard in Unix. It's done to allow the code to handle going
back in time before the origin point. Otherwise it couldn't cope with times
before this.
Also, remember that when doing arithmetic, the type used is the type of
the variables. If the variables were 'unsigned', difftime couldn't have a
-ve result. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Wed Nov 11, 2020 2:35 am |
|
|
I ask you because range of sec. in years variables are reduce to half.
Thanks, |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Wed Nov 11, 2020 2:41 am |
|
|
I understand totally.
I had the same thought myself. Using an instrument that can only be
recording 'now' times, realised one could double the available range
of years, by using unsigned not signed. However in fact it worked much
better to leave it as signed, and just set the option to move the origin to
2010. (#define TIME_T_USES_2010, before you load time.c/time.h). |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Wed Nov 11, 2020 2:53 am |
|
|
Yes,
I'm using this option "#define TIME_T_USES_2010" and for current project it will be useful.
Thanks for help mr. Ttelmah!
Best Regards! |
|
|
jaka
Joined: 04 May 2014 Posts: 36 Location: Finland
|
|
Posted: Wed Nov 11, 2020 6:28 am |
|
|
I have gone through all these same considerations.
With "#define TIME_T_USES_2010" the time overflows 2078. One of my projects has a remote possibility that it could still be in use then. By that time, I am 99 years old and too demented to fix it (not to mention that any of the required tools would be available!).
My solution was to modify time.h and time.c to add own functions which use 'unix minutes' instead of 'unix seconds'. At least mktime() and localtime() were fairly easy to modify this way. In my application I am using these functions to convert time between time zones, so accuracy of one minute is OK. The seconds don't need conversion, and can be directly copied from the original time. |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Thu Nov 12, 2020 1:47 am |
|
|
It's a good idea, I've also think about it but not realized yet.
Best Regards! |
|
|
kmp84
Joined: 02 Feb 2010 Posts: 345
|
|
Posted: Thu Apr 01, 2021 8:11 am |
|
|
Hello,
I have modify CCS 's "time_t mktime(rtc_time_t *timeT)" function as as follows :
Code: |
time_t mktime(rtc_time_t *timeT)
{
time_t unixTime = 0;
int1 isLeapYear = FALSE;
unsigned int16 i = 0;
unsigned int16 tmp_year = 0;
tmp_year = 2000 + timeT->year;
if(timeT != '\0'){
unixTime += timeT->second;
unixTime += (unsigned int32)(timeT->minute) * 60;
unixTime += (unsigned int32)(timeT->hour) * 3600;
isLeapYear = LeapYear(tmp_year);
/* Clamp the month to [0,11) */
//timeT->month %= 12;
for(i=1; i<=timeT->month; i++)
{
unixTime += (DaysInMonth(i - 1, isLeapYear) * 86400);
}
/* Clamp the days in the month */
//timeT->tm_mday %= DaysInMonth(timeT->tm_mon,isLeapYear);
unixTime += (timeT->dayOfMonth - 1) * 86400;
#ifdef TIME_T_USES_2010
i = 110;
#else
i = 70;
#endif
while(i < tmp_year){
isLeapYear = LeapYear(i);
if(isLeapYear)
unixTime += (31622400); // seconds in 366 days
else
unixTime += (31536000); // seconds in 365 days
i++;
}
}
return unixTime;
} |
but calculation for different months are wrong. I give the function the following arguments YY[0-99], MM[1-12], DD[1-31], hh[0-23], mm[0-59], ss[0-59]. Can you help me? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Apr 01, 2021 8:36 am |
|
|
Glaring issue:
MM[1-12]
Month numbers are 0-11.
Having the month number wrong will mean that the number of days
in the month will be calculated incorrectly. Result "calculation for different
months are wrong"..... |
|
|
|