]> git.sur5r.net Git - i3/i3status/blob - src/print_time.c
Added support for Pango markup.
[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 *title, const char *format, const char *tz, const char *format_time, time_t t) {
37     const char *walk;
38     char *outwalk = buffer;
39     struct tm tm;
40     char timebuf[1024];
41
42     if (title != NULL)
43         INSTANCE(title);
44
45     set_timezone(tz);
46     localtime_r(&t, &tm);
47
48     if (format_time == NULL) {
49         strftime(timebuf, sizeof(timebuf), format, &tm);
50         maybe_escape_markup(timebuf, &outwalk);
51     } else {
52         for (walk = format; *walk != '\0'; walk++) {
53             if (*walk != '%') {
54                 *(outwalk++) = *walk;
55                 continue;
56             }
57
58             if (BEGINS_WITH(walk + 1, "time")) {
59                 strftime(timebuf, sizeof(timebuf), format_time, &tm);
60                 maybe_escape_markup(timebuf, &outwalk);
61                 walk += strlen("time");
62             }
63         }
64     }
65
66     *outwalk = '\0';
67     OUTPUT_FULL_TEXT(buffer);
68 }