cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Time Functions

richardh
Associate II
Posted on January 17, 2012 at 17:27

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-date
14 REPLIES 14
richardh
Associate II
Posted on February 20, 2012 at 17:49

ColdWeather,

I think you have a bug in your routines (or I am getting it wrong.. so probably me!) on line 273 you have:

if (m == 1 /*Feb*/ && IsLeapYear(y +1900)) dr++; // leap year

Surely it should be >= 1 otherwise the only time it takes the leap year into account is in Feb?

other than that, job done so thanks for all the help!!  The last thing to catch me out was the enable for writing to the backup domain as I couldnt figure why I couldnt set the count.  Many thanks for all your help guys!

Posted on February 20, 2012 at 21:23

I think you have a bug in your routines (or I am getting it wrong.. so probably me!) on line 273 you have:

 

 

if (m == 1 /*Feb*/ && IsLeapYear(y +1900)) dr++; // leap year

 

 

Surely it should be >= 1 otherwise the only time it takes the leap year into account is in Feb?

 

February is the only month whose day count changes in a leap year. This is a fixup for the mday[] array value for February.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
richardh
Associate II
Posted on February 21, 2012 at 12:47

yep, I get that, Feb is the only month a day longer, but surely that would make the time/date incorrect by 86400 seconds after Feb every leap year?  surely once you've had the leap day, then you need to add this in?

Posted on February 21, 2012 at 17:48

Ok, I'll preface this by saying it's not my code, and I haven't validated it, but here are my observations walking the code.

a) The line/routine you cite, and a similar one within the code, perform input validation of Day, Month, Year parameters.

b) The validation does not reject inputs, merely pulls them into scope. So it checks if Feb 29 is valid for the given year, or if you had Jan 32, etc.

c) The actual conversion routines appear to be coming from the compiler's library. ie mktime(). Those routines would indeed need to account for the knock on effects of a leap year on subsequent months, as they typically work with the days-from-epoch to get to a year, and then have to take day-of-year and compute month, and day-of-month.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
richardh
Associate II
Posted on February 22, 2012 at 14:34

Hi Clive

You are correct (as you generally are having read lots of your replies through the forum), it was me not fully understanding the ternary operators as I generally use heavily typed and easy to read code, so avoid this type of operator (even though I know it is perfectly valid).

Many thanks for all your help (and the others) in getting me sorted out 🙂