]> git.sur5r.net Git - i3/i3status/blob - src/print_time.c
Add tztime module to support multiple different timezones.
[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 *timezone) {
15         if (!local_timezone_init) {
16                 /* First call, initialize. */
17                 local_timezone = getenv("TZ");
18                 local_timezone_init = 1;
19         }
20         if (timezone == NULL || timezone[0] == '\0') {
21                 /* User wants localtime. */
22                 timezone = local_timezone;
23         }
24         if (timezone != current_timezone) {
25                 if (timezone) {
26                         setenv("TZ", timezone, 1);
27                 } else {
28                         unsetenv("TZ");
29                 }
30                 tzset();
31                 current_timezone = timezone;
32         }
33 }
34
35 void print_time(yajl_gen json_gen, char *buffer, const char *format, const char *timezone, time_t t) {
36         char *outwalk = buffer;
37         struct tm tm;
38
39         /* Convert time and format output. */
40         set_timezone(timezone);
41         localtime_r(&t, &tm);
42         outwalk += strftime(outwalk, 4095, format, &tm);
43         *outwalk = '\0';
44         OUTPUT_FULL_TEXT(buffer);
45 }