Go Down

Topic: Date and time calculations (Read 1 time) previous topic - next topic

Ivan747

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

bubulindo

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

joop

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

robtillaart


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




Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Jack Christensen

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).

Ivan747

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.

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

Ivan747



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.

robtillaart

Looks good, saw a small bug in documentation ?

from  - http://code.google.com/p/ds1307new/wiki/Reference#Use_Cases - 
Quote
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


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

olikraus

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

Oliver

David Pankhurst


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?

robtillaart

Quote
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);

Code: [Select]

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


you have to fill in the tmElements structs of course

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

David Pankhurst

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

David Pankhurst

...never mind - I found it:

Code: [Select]
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...

Go Up