Hallo,
ich bin gerade dabei mit einem 18F2620 und GPRS Modem die Uhrzeit per NTP Protokoll mit einem Zeitserver zu synchronisieren.
Im PIC wird die Zeit als UNIX-timestamp gespeichert (Sekunden seit dem 01.01.1970), um die Zeit in Stunden, Minuten und Sekunden umzurechnen verwende ich die im HI-TECH Compiler enthaltene Funktion
struct tm *localtime(const time_t * tp).
Leider ist die time.h Bibliothek recht abgespeckt und eine Berücksichtigung der Sommerzeit ist nicht integriert.
Deshalb habe ich folgende Funktion geschrieben:
void get_time(void) {
struct tm *t;
signed long dst_start;
signed long dst_end;
signed long u;
//tm_sec seconds after the minute 0-61
//tm_min minutes after the hour 0-59
//tm_hour hours since midnight 0-23
//tm_mday day of the month 1-31
//tm_mon months since January 0-11
//tm_year years since 1900
//tm_wday days since Sunday 0-6
//tm_yday days since January 1 0-365
//tm_isdst Daylight Saving Time flag
time_zone=-60; //set time zone for MEZ
t = gmtime(&rtc_time); //get current time (convert UNIX timestamp to local time struct
t->tm_mon = 2; //March
t->tm_mday = 31; //31.
t->tm_hour = 1; //UTC time
t->tm_min = 0;
t->tm_sec = 0;
u=mktime(t);
t=gmtime(&u);
t->tm_mday -= t->tm_wday; //calculate last sunday in month
dst_start = mktime(t);
t = gmtime (&rtc_time); //get current time
t->tm_mon = 9; //October
t->tm_mday = 31; //31.
t->tm_hour = 1; //UTC time
t->tm_min = 0;
t->tm_sec = 0;
u=mktime(t);
t=gmtime(&u);
t->tm_mday -= t->tm_wday; //calculate last sunday in month
dst_end = mktime(t);
if(dst_start <= rtc_time && rtc_time < dst_end) {
//sommerzeit
} else {
//normalzeit
}
}
Das funktioniert zwar aber ist sehr hässlich und sicher auch langsam. Hat jemand eine bessere Idee?
LG