Allora, allora,
ho preso l'esempio della libreria time.h denominato "TimeRTC" e l'ho modificato
per vedere se riesco a calcolare il timestamp, il codice è quello che segue:
/*
* TimeRTC.pde
* example code illustrating Time library with Real Time Clock.
*
*/
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h> // a basic DS1307 library that returns time as a time_t
void setup() {
Serial.begin(9600);
setSyncProvider(RTC.get); // the function to get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
}
unsigned long miotimestamp;
const unsigned long totmesiinsecondi [12] = {0,2678400,5097600,7776000,10368000,13046400,15638400,18316800,20995200,23587200,26265600,28857600};
const unsigned long annoinsec = 31536000; // Un anno espresso in secondi
const unsigned long giornoinsec = 86400; // Un giorno espresso in secondi
const int orainsec = 3600; // Un'ora spressa in secondi
const int announix = 1970;
int bisestile;
void loop()
{
int indicetotmesi = month()-1;
miotimestamp = ((year() - announix) * annoinsec) + ((int((year() - 1968) / 4)) * giornoinsec) + totmesiinsecondi[indicetotmesi] + (day() * giornoinsec) + (hour() * orainsec) + (minute() * 60) + second();
// (gli anni trascorsi da epoc ) + (giorni aggiuntivi per gli anni bisestili) + (leggo nel vettorequanti sec. + (giorni del mese in + (ore att. in secondi)
// sono passati dall' inizio anno corso in secondi)
// alla fine del mese precedente
// tenedo conto della variabilità
// dei mesi)
bisestile = (year() % 4);
if ((month() == 2) && (day() <= 29) && (bisestile = 0))
{ miotimestamp = miotimestamp - giornoinsec; }
// se l'anno rilevato è bisestile la formula sopra aggiunge un giorno anche se ancora non è passato il 29 febbraio quindi verifico le condizioni ed eventualmente sottraggo un giorno
digitalClockDisplay();
delay(1000);
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.print(" Timestamp = ");
Serial.print(now());
Serial.print(" MioTimestamp = ");
Serial.print(miotimestamp);
Serial.print(" Differenza = ");
Serial.println(miotimestamp - now());
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
Nel serial monitor, il mio timestamp è costantemente magggiore rispetto a quello di now() di 20864 secondi :(, ossia 5 ore 47 minuti e 44 secondi:
20:56:57 15 9 2012 Timestamp = 1347742617 MioTimestamp = 1347763481 Differenza = 20864
Da ore cerco di capire dove sta l'inghippo ma proprio non lo vedo =(,
Mi rendo conto che la domanda è impegnativa, ma qualcuno riesce a capire dove sbaglio... ![]()
Grazie Riccardo