]> git.sur5r.net Git - i3/i3status/blob - src/print_time.c
clang-format-3.5 -i **/*.[ch], update modeline
[i3/i3status] / src / print_time.c
1 // vim:ts=4:sw=4:expandtab
2 #include <time.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <stdbool.h>
6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
8
9 #include "i3status.h"
10
11 static bool local_timezone_init = false;
12 static const char *local_timezone = NULL;
13 static const char *current_timezone = NULL;
14
15 void set_timezone(const char *tz) {
16     if (!local_timezone_init) {
17         /* First call, initialize. */
18         local_timezone = getenv("TZ");
19         local_timezone_init = true;
20     }
21     if (tz == NULL || tz[0] == '\0') {
22         /* User wants localtime. */
23         tz = local_timezone;
24     }
25     if (tz != current_timezone) {
26         if (tz) {
27             setenv("TZ", tz, 1);
28         } else {
29             unsetenv("TZ");
30         }
31         tzset();
32         current_timezone = tz;
33     }
34 }
35
36 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *tz, time_t t) {
37     char *outwalk = buffer;
38     struct tm tm;
39
40     /* Convert time and format output. */
41     set_timezone(tz);
42     localtime_r(&t, &tm);
43     outwalk += strftime(outwalk, 4095, format, &tm);
44     *outwalk = '\0';
45     OUTPUT_FULL_TEXT(buffer);
46 }