1 // vim:ts=4:sw=4:expandtab
8 #include <yajl/yajl_gen.h>
9 #include <yajl/yajl_version.h>
13 static bool local_timezone_init = false;
14 static const char *local_timezone = NULL;
15 static const char *current_timezone = NULL;
17 void set_timezone(const char *tz) {
18 if (!local_timezone_init) {
19 /* First call, initialize. */
20 local_timezone = getenv("TZ");
21 local_timezone_init = true;
23 if (tz == NULL || tz[0] == '\0') {
24 /* User wants localtime. */
27 if (tz != current_timezone) {
33 current_timezone = tz;
38 void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) {
40 char *outwalk = buffer;
41 struct tm local_tm, tm;
48 localtime_r(&t, &local_tm);
53 // When hide_if_equals_localtime is true, compare local and target time to display only if different
54 time_t local_t = mktime(&local_tm);
55 double diff = difftime(local_t, t);
56 if (hide_if_equals_localtime && diff == 0.0) {
61 setlocale(LC_ALL, locale);
64 if (format_time == NULL) {
65 strftime(timebuf, sizeof(timebuf), format, &tm);
66 maybe_escape_markup(timebuf, &outwalk);
68 for (walk = format; *walk != '\0'; walk++) {
72 } else if (BEGINS_WITH(walk + 1, "time")) {
73 strftime(timebuf, sizeof(timebuf), format_time, &tm);
74 maybe_escape_markup(timebuf, &outwalk);
75 walk += strlen("time");
84 setlocale(LC_ALL, "");
89 OUTPUT_FULL_TEXT(buffer);