2012-01-17 08:27 AM
Hi
I need to work out the difference in days between 2 dates, what is the simplest way to do this? the only way I can think is work out if its a leap year or not, and put in the days per month etc. but this seems like re-inventing the wheel.Having looked at how PC compilers do this, most functions I've seen work in the seconds since a specific date/time manner, so its easy to perform calculations like this, but the STM32F103 RTC seems to use BCD instead, so I can't think of a way round this apart from hard coding. #rtcc-time-date2012-01-17 03:20 PM
In fact the the STM32 RTC is much more suited to the seconds method. The BCD method might have been used in an example you have seen but that is software related and not STM32 hardware related. IMHO the BCD method is lame. Let the RTC count seconds from 1-Jan-1970 00:00:00 (epoch). Convert dates to seconds using standard epoch C functions (time.h) and then counting the days (or any other comparisons) between dates is just simple maths.
2012-01-19 01:33 PM
The RTC on the F103 is a 32-bit counter, typically counting off seconds. You can slew it faster or slower with the prescaler. At 32-bit the second rollover would occur in ~136 years. The biggest mistake people usually make is to try and make it rollover at 24 hour periods.
Pick an epoch date you are comfortable with, the UNIX one works, but a 2000, or 2010 start time would be equally valid. The leap day computation is much more efficient if you limit yourself to 1901 to 2099. If you need to constantly convert seconds to D:M:Y H:M:S, then yes you'll need to count through the years, and then days, but you can cache the results, and step through multiple 4 year periods.2012-01-30 05:31 AM
thanks, that all makes sense!
Any idea of a sensible time.h and associated files I can download? the ones I have found are low on documentation (and I have to hack it to feed in my own counter value I guess) which is OK if you are sure what you are doing, I'm not :)2012-01-30 06:44 AM
What complier tools are you using. This sort of thing is usually already there in the tools.
2012-01-30 06:57 AM
I'm using Keil, and I think I have found it.... thanks
Do I simply re-write the time function? and everything else works...Where do I set the epoch?Sorry if this is all very obvious...2012-01-30 07:11 AM
It's only obvious when you've done it :) Yes, use your own get time function or implement a new ''time'' function that simply gets the tick count from the STM32 RTC counter (add a mutex if you are using an RTOS). The epoch is 1/1/1970 00:00:00 and I'm not sure how'd you'd go about using something else without implementing your own time fuctions (I've never used anything else to date).
2012-01-30 07:27 AM
2012-01-31 05:14 AM
You can easily find the difference between two dates if you convert the given dates into the 32-bit values that represent seconds and compute then (Value2 - Value2)/(60*60*24).
I attached my hardware independent calender support using some KEIL RTL time definition/convertion functions. Post here, should you have any questions. ________________ Attachments : HAL_Calender.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I112&d=%2Fa%2F0X0000000bgz%2F7rZohE.St6bAict8etnVZEkF9.1hDXHYpqa4iSaqRYY&asPdf=false2012-02-17 03:34 AM
thanks, your calender support routines are pretty comprehensive!
One thing that I am unsure of, surely when I set the time, I have to set the date at the same time? so why do you have individual SetDate and SetTime routines? as these just generate a value to write to the hardware counter, and one will overwrite the other surely?