From 1363655b59da6a8e87e64f54bb61522dfa70a7b3 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Fri, 3 May 2013 18:27:33 +0200 Subject: [PATCH] fixed issue with timezone and dst handling on openwrt platforms, thx !gm --- src/sun_main.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/sun_main.c b/src/sun_main.c index 2a67a54..f7707ab 100644 --- a/src/sun_main.c +++ b/src/sun_main.c @@ -25,6 +25,8 @@ * along with sun. If not, see . */ +#define _BSD_SOURCE 1 /* for tm_gmtoff field in struct tm */ + #include #include #include @@ -32,7 +34,8 @@ #include #include #include -#include +#include +#include #include "../config.h" #include "sun.h" @@ -120,7 +123,7 @@ int main(int argc, char *argv[]) { char *format = "%H:%M"; char *query = NULL; bool error = false; - int timezone = 0; + int timezone = INT_MAX; /* magic value for system default with dst */ enum mode mode = INVALID; struct tm date; @@ -131,12 +134,6 @@ int main(int argc, char *argv[]) { time(&t); localtime_r(&t, &date); - /* default timezone: system */ - struct timezone tz; - if (gettimeofday(NULL, &tz) == 0) { - timezone = -tz.tz_minuteswest / 60.0; - } - /* parse mode */ if (argc > 1 && argv[1][0] != '-') { if (strcmp(argv[1], "rise") == 0) mode = RISE; @@ -179,6 +176,9 @@ int main(int argc, char *argv[]) { fprintf(stderr, "invalid date: %s\n", optarg); error = true; } + else { + mktime(&date); /* update date.tm_gmtoff */ + } break; case 'f': @@ -217,6 +217,10 @@ int main(int argc, char *argv[]) { } } + if (timezone == INT_MAX) { + timezone = date.tm_gmtoff / 3600; + } + /* validate mode */ if (mode == INVALID) { fprintf(stderr, "invalid mode\n"); @@ -268,6 +272,7 @@ int main(int argc, char *argv[]) { printf("calculate for: %s\n", date_str); printf("for position: %f, %f\n", pos.lat, pos.lon); printf("with twilight: %f\n", twilight); + printf("with timezone: %d\n", timezone); #endif /* start the calculation */