Date and Time manipulation: addition of minutes

Hello,

I am making a project, where something happens periodically, .i.e. users sets a specific time frame like 15min, 30min etc. and then every 15 or 30 minutes specific event happens.

I am using external RTC timer (DS1307) and I use RTClib.h library to handle requests of time.

But I cannot find any libs or tools (or even reliable approaches), how to manipulate time. The problem I see is that, when I have to add like 30 min, when real time is 2015.12.31 23:50 or something like that, every member of the date increases and simple math will not work. I would not like to code everything from scratch, because my application is really bug sensitive and can cause serious problems if it fails in one or another way.

Any suggestions?

Thanks a lot.

The Time library. Absolutely.

rofl:
my application is really bug sensitive and can cause serious problems if it fails in one or another way.

Are human lives at stake here?

The problem I see is that, when I have to add like 30 min, when real time is 2015.12.31 23:50 or something like that, every member of the date increases and simple math will not work.

Why not? 50 + 30 = 80, which exceeds 60, so minutes becomes 20 AND you need to increment the hour value. That will overflow, so you need to reduce it by 24 AND increment day. Repeat until a value does not overflow.

Or, do it the easy way. The chip keeps track of the time in seconds since January 1st, 1970. When you ask for a value, it computes the appropriate value. You can get the time as a time_t (seconds since 1970), increment that by the appropriate number of seconds, and save it back. Then, the clock will handle the magic of 2015.12.31 23:50:17 + 30 minutes.

well... actually... yes people LIVES MIGHT be at stake (in one or another way)... Don't get me wrong, but it will be used by disabled people, who will not be able to say something (that something is wrong or whatever) and system must be simple enough to handle this task.

Well, if I understood it correctly... That idea of adjusting the timer itself looks nice. It would be possible, I think, to send back the adjustment and return the new time... Yeah, I will try that.

By the way, what do you think if I do not use that timer as a normal clock.. ? What if reset it every time event happens (to 0:0:0) and then I would just wait for like 0:30min:0 and do event again.

Thanks.

Work in Unix time. Unix time is a single number that contains year, month, day, hour, minute and seconds. The number itself is the count of the number of seconds that have elapsed since a certain epoch date. I wrote this post at approximately 1441615980 in Unix time. If I add 120 to that I get Unix time for two minutes from then. Check out the library you are using it should be able to both return Unix time much like reading millis() and be able to convert Unix time to what we humans are used to.

Here is a link to the Time library:
http://playground.arduino.cc/code/time

By the way, if you can tolerate a reset during a power loss, you are not concerned with absolute time but only relative time, and your accuracy need only be in the 100-500 ppm range, you may not need an RTC.

OK, thanks everybody for speedy replies. I will carry on working with these approaches.

If you don't care about the "clock time" (as you proposed to reset it every time.. ), another solution is to increase a counter ( uint32_t ) every seconds, and do appropriate action when this counter reach a certain value.

You are lucky, your RTC has a SQW pin that can be set to trigger an interrupt every second :slight_smile:

guix:
You are lucky, your RTC has a SQW pin that can be set to trigger an interrupt every second :slight_smile:

Actually, my RTC implementations always use this pin, instead of polling the registers.

Why not keep the RTC at the correct time and set a variable, SetTime = NOW (from the RTC) + the required delay. Then check if the RTC has reached that variable every time round loop().

if (NOW >= SetTime){
...  // Time's up!
}

You could even have a countdown:

 countdown = SetTime - NOW;

With the time library, don't forget to declare your time variables as type time_t or equivalent. e.g.:

time_t countdown;