HO provato
SukkoPera:
Sbagli ;D.
Sto provando il codice che mi hai consigliato :
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h" // https://github.com/adafruit/RTClib
RTC_DS3231 rtc;
char daysOfTheWeek[7][12] = {"Domenica", "Lunedì", "Martedì", "Mercoledì", "Giovedì", "Venerdì", "Sabato"};
void setup () {
#ifndef ESP8266
while (!Serial); // for Leonardo/Micro/Zero
#endif
Serial.begin(9600);
delay(3000); // wait for console opening
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.lostPower()) {
Serial.println("RTC lost power, lets set the time!");
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
// This line sets the RTC with an explicit date & time, for example to set
// January 21, 2014 at 3am you would call:
// rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0));
}
}
byte dstOffset (byte d, byte m, unsigned int y, byte h) {
/* This function returns the DST offset for the current UTC time.
This is valid for the EU, for other places see
http://www.webexhibits.org/daylightsaving/i.html
Results have been checked for 2012-2030 (but should work since
1996 to 2099) against the following references:
- http://www.uniquevisitor.it/magazine/ora-legale-italia.php
- http://www.calendario-365.it/ora-legale-orario-invernale.html
*/
// Day in March that DST starts on, at 1 am
byte dstOn = (31 - (5 * y / 4 + 4) % 7);
// Day in October that DST ends on, at 2 am
byte dstOff = (31 - (5 * y / 4 + 1) % 7);
if ((m > 3 && m < 10) ||
(m == 3 && (d > dstOn || (d == dstOn && h >= 1))) ||
(m == 10 && (d < dstOff || (d == dstOff && h <= 1))))
return 1;
else
return 0;
}
const int utcOffset = 1;
DateTime getLocalTime() {
DateTime now = rtc.now();
int32_t deltaSecs = (utcOffset + dstOffset (now.day(), now.month(), now.year(), now.hour()) * 3600UL);
// int32_t deltaSecs = (utcOffset + dstOffset (now.day(), now.month(), now.year(), now.hour())) * SECS_PER_HOUR
const unsigned long SECS_PER_HOUR = 3600;
DateTime correctedNow (now + TimeSpan(deltaSecs));
return correctedNow;
}
void loop () {
DateTime now = getLocalTime();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print(" since midnight 1/1/1970 = ");
Serial.print(now.unixtime());
Serial.print("s = ");
Serial.print(now.unixtime() / 86400L);
Serial.println("d");
// calculate a date which is 7 days and 30 seconds into the future
DateTime future (now + TimeSpan(7, 12, 30, 6));
Serial.print(" now + 7d + 30s: ");
Serial.print(future.year(), DEC);
Serial.print('/');
Serial.print(future.month(), DEC);
Serial.print('/');
Serial.print(future.day(), DEC);
Serial.print(' ');
Serial.print(future.hour(), DEC);
Serial.print(':');
Serial.print(future.minute(), DEC);
Serial.print(':');
Serial.print(future.second(), DEC);
Serial.println();
Serial.println();
delay(3000);
}
però sulla seriale mi riporta:
2018/4/7 (Sabato) 18:11:40
since midnight 1/1/1970 = 1523124700s = 17628d
now + 7d + 30s: 2018/4/15 6:41:46
ovvero un'ora esatta in più; Come mai secondo voi?