1 // vim:ts=4:sw=4:expandtab
7 #include <yajl/yajl_gen.h>
8 #include <yajl/yajl_version.h>
12 static bool local_timezone_init = false;
13 static const char *local_timezone = NULL;
14 static const char *current_timezone = NULL;
16 void set_timezone(const char *tz) {
17 if (!local_timezone_init) {
18 /* First call, initialize. */
19 local_timezone = getenv("TZ");
20 local_timezone_init = true;
22 if (tz == NULL || tz[0] == '\0') {
23 /* User wants localtime. */
26 if (tz != current_timezone) {
32 current_timezone = tz;
37 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) {
39 char *outwalk = buffer;
40 struct tm local_tm, tm;
47 localtime_r(&t, &local_tm);
52 // When hide_if_equals_localtime is true, compare local and target time to display only if different
53 time_t local_t = mktime(&local_tm);
54 double diff = difftime(local_t, t);
55 if (hide_if_equals_localtime && diff == 0.0) {
60 setlocale(LC_ALL, locale);
63 if (format_time == NULL) {
64 strftime(timebuf, sizeof(timebuf), format, &tm);
65 maybe_escape_markup(timebuf, &outwalk);
67 for (walk = format; *walk != '\0'; walk++) {
71 } else if (BEGINS_WITH(walk + 1, "time")) {
72 strftime(timebuf, sizeof(timebuf), format_time, &tm);
73 maybe_escape_markup(timebuf, &outwalk);
74 walk += strlen("time");
83 setlocale(LC_ALL, "");
88 OUTPUT_FULL_TEXT(buffer);