Pages: [1]   Go Down
Author Topic: Date and time calculations  (Read 1070 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 42
Posts: 3223
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 32
Hamradio callsign: PG4I
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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




Logged

Rob Tillaart

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

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 93
Posts: 3968
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Germany
Offline Offline
Edison Member
*
Karma: 133
Posts: 1447
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Logged

Rob Tillaart

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

Germany
Offline Offline
Edison Member
*
Karma: 133
Posts: 1447
If you believe something is right, you won't see what's wrong (David Straker).
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Oliver
Logged

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
unsigned long timeDiff( tmElements_t &tm1, tmElements_t &tm2)
{
  return makeTime(tm2) - makeTime(tm1);
}

 you have to fill in the tmElements structs of course

Logged

Rob Tillaart

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

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

...never mind - I found it:

Code:
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...
Logged

Pages: [1]   Go Up
Jump to: