Ensure timezone conversion works on timestamps (s,ms,us).
@@ -520,6 +520,21 @@ tm2time (const struct tm *src) {
return timegm (&tmp) - src->tm_gmtoff;
}
+static void
+set_tz (void) {
+ char tz[TZ_NAME_LEN] = { 0 };
+
+ if (!conf.tz_name)
+ return;
+
+ snprintf (tz, TZ_NAME_LEN, "TZ=%s", conf.tz_name);
+ if ((putenv (tz)) != 0) {
+ LOG_DEBUG (("Can't set TZ env variable %s: %s\n", tz, strerror (errno)));
+ return;
+ }
+ tzset ();
+}
+
/* Format the given date/time according the given format.
*
* On error, 1 is returned.
@@ -528,7 +543,6 @@ tm2time (const struct tm *src) {
int
str_to_time (const char *str, const char *fmt, struct tm *tm) {
time_t t;
- char tz[TZ_NAME_LEN] = { 0 };
char *end = NULL, *sEnd = NULL;
unsigned long long ts = 0;
int us, ms;
@@ -561,6 +575,8 @@ str_to_time (const char *str, const char *fmt, struct tm *tm) {
return 1;
seconds = (us) ? ts / SECS : ((ms) ? ts / MILS : ts);
+
+ set_tz ();
/* if GMT needed, gmtime_r instead of localtime_r. */
localtime_r (&seconds, tm);
@@ -579,12 +595,7 @@ str_to_time (const char *str, const char *fmt, struct tm *tm) {
return 0;
}
- snprintf (tz, TZ_NAME_LEN, "TZ=%s", conf.tz_name);
- if ((putenv (tz)) != 0) {
- LOG_DEBUG (("Can't set TZ env variable %s: %s\n", tz, strerror (errno)));
- return 0;
- }
- tzset ();
+ set_tz ();
localtime_r (&t, tm);
return 0;
