Date and time calculations

Hello, is it possible to have date and time calculations on the Arduino? Something like:

2011/08/08 23h:45m:00s + 0001/00/00 00h:16m:00s = 2012/08/09 00h:01m:00s

It must take in count leap years and the different lengths of months, too. If this kind of function is not available then I will code it myself.

Thanks,
Ivan

Don't think it is... However, if you do one, post them here so they can be added to the RTC library. :slight_smile:

Have a look at the Arduino Time Library. It may have what you need.

Convert both time to unix timestamps (seconds since 1 jan 1970 00:00) - Time_t in the time lib just does this.

subtract both and you have the difference in seconds, which can be turned into days/hours etc

joop++
robtillaart++

Also have a look at the Time.h file that is part of that library, there are lots of useful functions and constants. There is also a handy struct called tmElements_t and functions to convert between it and a time_t (the Unix time timestamp).

It seems the library doesn't suit our purposes. We'll be coding our own. It is for PIC's, though, because this and a few other libraries where the only reason for us to use the Arduino.

I just want to point out our library for the DS1307. It containts several algorithms for date and time calculation.
See some use cases here:
http://code.google.com/p/ds1307new/wiki/Reference#Use_Cases

All procedures have been carefully optimized for 8 Bit controller with a limited amount of memory.

Oliver

olikraus:
I just want to point out our library for the DS1307. It containts several algorithms for date and time calculation.
See some use cases here:
http://code.google.com/p/ds1307new/wiki/Reference#Use_Cases

All procedures have been carefully optimized for 8 Bit controller with a limited amount of memory.

Oliver

I will be using a DS1307 so it will help. I will check it out right now.

Looks good, saw a small bug in documentation ?

from - Google Code Archive - Long-term storage for Google Code Project Hosting. -

cdn

Prototype

uint8_t DS1307new::cdn

Description

Contains the number of the days, passed since 2000-01-01 (century day number). The cdn for 2000-01-01 is 0, for 2000-01-02 is 1. This is a read only variable. Do not write to this variable. Use the fill functions instead.

Range: 0..65535

if cdn is an uint8_t the range should be smaller, but as the range makes sense I assume cdn is a uint16_t

@robtillaart
Thanks for the hint. Your fully right.
I have updated the wiki page.

Oliver

robtillaart:
Convert both time to unix timestamps...
...which can be turned into days/hours etc

I was looking this up, and I couldn't find either of these conversion routines (I believe mktime and strftime) - are they part of Arduino?

Insert Quote
Quote from: robtillaart on August 10, 2011, 12:53:21 PM
Convert both time to unix timestamps...
...which can be turned into days/hours etc

I was looking this up, and I couldn't find either of these conversion routines (I believe mktime and strftime) - are they part of Arduino?

Use time_t makeTime(tmElements_t &tm);

unsigned long timeDiff( tmElements_t &tm1, tmElements_t &tm2)
{
  return makeTime(tm2) - makeTime(tm1);
}

you have to fill in the tmElements structs of course

...and to convert back to d/m/y?

...never mind - I found it:

void localTime(time_t *timep,byte *psec,byte *pmin,byte *phour,byte *pday,byte *pwday,byte *pmonth,byte *pyear);

so the answer to the original question is it can be done...