Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: August 09, 2011, 05:32:37 pm » |
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
Edison Member
Karma: 20
Posts: 2308
|
 |
« Reply #1 on: August 10, 2011, 12:04:21 am » |
Don't think it is... However, if you do one, post them here so they can be added to the RTC library.
|
|
|
|
|
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
Newbie
Karma: 0
Posts: 31
Hamradio callsign: PG4I
|
 |
« Reply #2 on: August 10, 2011, 01:24:45 am » |
Have a look at the Arduino Time Library. It may have what you need.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #3 on: August 10, 2011, 06:53:21 am » |
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
|
|
|
|
|
Grand Blanc, MI, USA
Offline
Faraday Member
Karma: 43
Posts: 2518
"We're a proud service of the Lost Electricity Reclamation Agency"
|
 |
« Reply #4 on: August 10, 2011, 07:19:06 am » |
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
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #5 on: August 10, 2011, 10:16:17 am » |
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
God Member
Karma: 69
Posts: 802
If you believe something is right, you won't see what's wrong (David Straker).
|
 |
« Reply #6 on: August 10, 2011, 10:34:30 am » |
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_CasesAll procedures have been carefully optimized for 8 Bit controller with a limited amount of memory. Oliver
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #7 on: August 10, 2011, 10:55:29 am » |
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_CasesAll 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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #8 on: August 10, 2011, 12:04:47 pm » |
Looks good, saw a small bug in documentation ? from - http://code.google.com/p/ds1307new/wiki/Reference#Use_Cases - 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
|
|
|
|
|
Germany
Offline
God Member
Karma: 69
Posts: 802
If you believe something is right, you won't see what's wrong (David Straker).
|
 |
« Reply #9 on: August 10, 2011, 03:54:37 pm » |
@robtillaart Thanks for the hint. Your fully right. I have updated the wiki page.
Oliver
|
|
|
|
|
Logged
|
|
|
|
|
Victoria, BC, Canada
Offline
Full Member
Karma: 0
Posts: 222
|
 |
« Reply #10 on: August 11, 2011, 12:04:33 am » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9429
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #11 on: August 11, 2011, 04:44:40 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Victoria, BC, Canada
Offline
Full Member
Karma: 0
Posts: 222
|
 |
« Reply #12 on: August 11, 2011, 08:51:05 pm » |
...and to convert back to d/m/y?
|
|
|
|
|
Logged
|
|
|
|
|
Victoria, BC, Canada
Offline
Full Member
Karma: 0
Posts: 222
|
 |
« Reply #13 on: August 11, 2011, 09:02:53 pm » |
...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...
|
|
|
|
|
Logged
|
|
|
|
|
|