I have the example working from the webpage working just fine. However, I am struggling to figure out how to apply the newly correct time to the system clock…
I am trying to get this newly formated time into my sketch, but no luck.
… Assume same code up to void loop…
I just don’t think I understand how to apply the tz.toLocal method on a time object. (See time_t current_time() )
Hi Cameron,
I understand what you want to do, but It’s discouraged because you lost some information (GTM for example), but here the code modified.
I advise you to use strftime that offer a lot of features here a list.
I update local time directly when the value is returned because if you call the functione more than one time you apply N time the DST and you lost control of the time.
unsigned long epoch = tz.toLocal(timeClient.getEpochTime());
setTime(epoch);
and use a strftime like so
Serial.println(getEpochStringByParams(now(), "%H:%M:%S %d.%m.%Y It's a beautiful %A of %B"));
and the complete sketch
pay attention I add my timezone:
/*
* Simple NTP client update system time with DST applyed (discouraged)
* http://mischianti.org/
*
* The MIT License (MIT)
* written by Renzo Mischianti
*/
#define DEBUG
const char *ssid = "";
const char *password = "";
#include
// change next line to use with another board/shield
#include
//#include // for WiFi shield
//#include // for WiFi 101 shield or MKR1000
#include
#include
#include
#include // https://github.com/JChristensen/Timezone
/**
* Input time in epoch format and return tm time format
* by Renzo Mischianti
*/
static tm getDateTimeByParams(long time) {
struct tm *newtime;
const time_t tim = time;
newtime = localtime(&tim);
return *newtime;
}
/**
* Input tm time format and return String with format pattern
* by Renzo Mischianti
*/
static String getDateTimeStringByParams(tm *newtime, char *pattern =
(char*) "%d/%m/%Y %H:%M:%S") {
char buffer[90];
strftime(buffer, 90, pattern, newtime);
return buffer;
}
/**
* Input time in epoch format format and return String with format pattern
* by Renzo Mischianti
*/
static String getEpochStringByParams(long time, char *pattern =
(char*) "%d/%m/%Y %H:%M:%S") {
// struct tm *newtime;
tm newtime;
newtime = getDateTimeByParams(time);
return getDateTimeStringByParams(&newtime, pattern);
}
WiFiUDP ntpUDP;
// By default 'pool.ntp.org' is used with 60 seconds update interval and
// no offset
// NTPClient timeClient(ntpUDP);
// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
int GTMOffset = 0; // SET TO UTC TIME
NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", GTMOffset * 60 * 60,
60 * 60 * 1000);
// Central European Time (Frankfurt, Paris)
TimeChangeRule CEST = { "CEST", Last, Sun, Mar, 2, 120 }; // Central European Summer Time
TimeChangeRule CET = { "CET ", Last, Sun, Oct, 3, 60 }; // Central European Standard Time
Timezone tz(CEST, CET);
//// US Pacific Time Zone (Las Vegas, Los Angeles)
//TimeChangeRule usPDT = {"PDT", Second, Sun, Mar, 2, -420};
//TimeChangeRule usPST = {"PST", First, Sun, Nov, 2, -480};
//Timezone tz(usPDT, usPST);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
timeClient.begin();
delay(1000);
if (timeClient.update()) {
Serial.print("Adjust local clock");
// unsigned long epoch = timeClient.getEpochTime();
unsigned long epoch = tz.toLocal(timeClient.getEpochTime());
setTime(epoch);
} else {
Serial.print("NTP Update not WORK!!");
}
}
void loop() {
// I print the time from local clock but first I check DST
// to add hours if needed
// Serial.println(getEpochStringByParams(tz.toLocal(now())));
Serial.println(getEpochStringByParams(now(), "%H:%M:%S %d.%m.%Y It's a beautiful %A of %B"));
Serial.print("MyCrummyCode:");
digitalClockDisplay();
delay(1000);
}
tmElements_t tm;
time_t current_time() {
return make_unixtime(tz.toLocal(second()), tz.toLocal(minute()),
tz.toLocal(hour()), tz.toLocal(day()), tz.toLocal(month()),
tz.toLocal(year()));
}
time_t make_unixtime(uint8_t sec, uint8_t mn, uint8_t hr, uint8_t dy,
uint8_t mon, uint16_t yr) {
tm.Second = sec;
tm.Hour = hr;
tm.Minute = mn;
tm.Day = dy;
tm.Month = mon;
tm.Year = yr - 1970;
return makeTime(tm);
}
void digitalClockDisplay() {
// digital clock display of the time
#ifdef DEBUG
yield();
Serial.print(hour());
yield();
printDigits(minute());
yield();
printDigits(second());
yield();
Serial.print(" ");
yield();
Serial.print(day());
yield();
Serial.print(".");
yield();
Serial.print(month());
yield();
Serial.print(".");
yield();
Serial.print(year());
yield();
Serial.print(" ");
yield();
Serial.println(dayStr(weekday()));
#endif
}
void printDigits(int digits) {
// utility for digital clock display: prints preceding colon and leading 0
yield();
Serial.print(":");
if (digits < 10)
Serial.print('0');
yield();
Serial.print(digits);
}
and here the result
MyCrummyCode:7:26:58 3.2.2021 Wednesday
07:26:59 03.02.2021 It's a beautiful Wednesday of February
MyCrummyCode:7:26:59 3.2.2021 Wednesday
07:27:00 03.02.2021 It's a beautiful Wednesday of February
MyCrummyCode:7:27:00 3.2.2021 Wednesday
07:27:01 03.02.2021 It's a beautiful Wednesday of February
MyCrummyCode:7:27:01 3.2.2021 Wednesday
07:27:02 03.02.2021 It's a beautiful Wednesday of February
MyCrummyCode:7:27:02 3.2.2021 Wednesday
07:27:03 03.02.2021 It's a beautiful Wednesday of February
MyCrummyCode:7:27:03 3.2.2021 Wednesday
Maintaining a repository (or site or forum) is a lot like tending to a garden - it requires constant care and attention to keep it thriving. If you're a skilled gardener (or coder!) and want to help keep our repository blooming, we'd love to have you on board! We're also looking for talented writers and forum moderators to help us grow our community. Interested in joining our team? Don't hesitate to reach out and let us know how you can contribute!
Are you a fan of electronics or programming? Share your knowledge with others, write a simple tutorial or how to make a great project Contact me: share_your_ideas@mischianti.org
The content displayed on this website is protected under a CC BY-NC-ND license. Visitors are prohibited from using, redistributing, or altering any content from this website for commercial purposes, including generating revenue through advertising. Any unauthorized use is a violation of the license terms and legal action may be taken against individuals or entities found to be in violation.
You must also provide the link to the source.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional
Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes.The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.