summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2018-07-04 14:09:07 +0200
committerantirez <antirez@gmail.com>2018-07-04 14:09:07 +0200
commit6614d30599ddbfdb643fe7080173729636af27ad (patch)
tree6e3029c8ebacb68f1dfbd82a131f689aa24eb648
parent243c5a7a305eaf7a7cf936eb6805f44f9802b3a5 (diff)
downloadredis-6614d30599ddbfdb643fe7080173729636af27ad.tar.gz
Localtime: fix daylight time documentation and computation.
-rw-r--r--src/localtime.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/localtime.c b/src/localtime.c
index c3564d1b0..ed6ff2cd3 100644
--- a/src/localtime.c
+++ b/src/localtime.c
@@ -39,7 +39,12 @@
* This function takes the timezone 'tz' as argument, and the 'dst' flag is
* used to check if daylight saving time is currently in effect. The caller
* of this function should obtain such information calling tzset() ASAP in the
- * main() function, and later accessing the globals 'timezone' and 'daylight'.
+ * main() function to obtain the timezone offset from the 'timezone' global
+ * variable. To obtain the daylight information, if it is currently active or not,
+ * one trick is to call localtime() in main() ASAP as well, and get the
+ * information from the tm_isdst field of the tm structure. However the daylight
+ * time may switch in the future for long running processes, so this information
+ * should be refreshed at safe times.
*
* Note that this function does not work for dates < 1/1/1970, it is solely
* designed to work with what time(NULL) may return, and to support Redis
@@ -57,6 +62,7 @@ void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst) {
const time_t secs_day = 3600*24;
t -= tz; /* Adjust for timezone. */
+ t += 3600+dst; /* Adjust for daylight time. */
time_t days = t / secs_day; /* Days passed since epoch. */
time_t seconds = t % secs_day; /* Remaining seconds. */
@@ -101,13 +107,17 @@ void nolocks_localtime(struct tm *tmp, time_t t, time_t tz, int dst) {
#include <stdio.h>
int main(void) {
- tzset();
+ /* Obtain timezone and daylight info. */
+ tzset(); /* Now 'timezome' global is populated. */
time_t t = time(NULL);
+ struct tm *aux = localtime(&t);
+ int daylight_active = aux->tm_isdst;
+
struct tm tm;
char buf[1024];
- nolocks_localtime(&tm,t,timezone,daylight);
+ nolocks_localtime(&tm,t,timezone,daylight_active);
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",&tm);
- printf("[timezone: %d, dl: %d] %s\n", (int)timezone, (int)daylight, buf);
+ printf("[timezone: %d, dl: %d] %s\n", (int)timezone, (int)daylight_active, buf);
}
#endif