-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp.h
32 lines (24 loc) · 1.01 KB
/
ntp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#pragma once
#include "ntpGlobals.h"
#include "time.h"
void NTP_begin(char* server, char* tzCfg) {
// https://en.cppreference.com/w/c/locale/setlocale
// setlocale(LC_TIME, "de_DE"); // does'nt work :-(
setlocale(LC_TIME, "de_DE.utf8");
configTime(0, 0, server); // 0, 0 because we will use TZ in the next line
setenv("TZ", tzCfg, 1); // Set environment variable with your time zone
tzset();
log_i("NTP initialized!");
}
void NTP_updTimeinfo() {
time(&NTP.now); // read the current time
localtime_r(&NTP.now, &NTP.timeinfo); // update the structure tm with the current time
}
// format description: https://help.gnome.org/users/gthumb/unstable/gthumb-date-formats.html.de
// or https://cplusplus.com/reference/ctime/strftime/
// NOTE: NTP_updTimeinfo(); must be called before, to get the actual time!
size_t NTP_asString(char fmt[], char res[], size_t resLen) { // used in page.h
strncpy(res, "?\0\0\0", resLen-1);
size_t r = strftime(res, resLen-1, fmt, &NTP.timeinfo);
return strlen(res);
}