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