convert tztime to generic object payload
[monky] / src / timeinfo.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "text_object.h"
32 #include <locale.h>
33
34 struct tztime_s {
35         char *tz;       /* timezone variable */
36         char *fmt;      /* time display formatting */
37 };
38
39 void scan_time(struct text_object *obj, const char *arg)
40 {
41         obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size);
42 }
43
44 void scan_tztime(struct text_object *obj, const char *arg)
45 {
46         char buf1[256], buf2[256], *fmt, *tz;
47         struct tztime_s *ts;
48
49         fmt = tz = NULL;
50         if (arg) {
51                 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
52
53                 switch (nArgs) {
54                         case 2:
55                                 fmt = buf2;
56                         case 1:
57                                 tz = buf1;
58                 }
59         }
60
61         ts = malloc(sizeof(struct tztime_s));
62         memset(ts, 0, sizeof(struct tztime_s));
63         ts->fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
64         ts->tz = tz ? strndup(tz, text_buffer_size) : NULL;
65         obj->data.opaque = ts;
66 }
67
68 void print_time(struct text_object *obj, char *p, int p_max_size)
69 {
70         time_t t = time(NULL);
71         struct tm *tm = localtime(&t);
72
73         setlocale(LC_TIME, "");
74         strftime(p, p_max_size, (char *)obj->data.opaque, tm);
75 }
76
77 void print_utime(struct text_object *obj, char *p, int p_max_size)
78 {
79         time_t t = time(NULL);
80         struct tm *tm = gmtime(&t);
81
82         setlocale(LC_TIME, "");
83         strftime(p, p_max_size, (char *)obj->data.opaque, tm);
84 }
85
86 void print_tztime(struct text_object *obj, char *p, int p_max_size)
87 {
88         char *oldTZ = NULL;
89         time_t t;
90         struct tm *tm;
91         struct tztime_s *ts = obj->data.opaque;
92
93         if (!ts)
94                 return;
95
96         if (ts->tz) {
97                 oldTZ = getenv("TZ");
98                 setenv("TZ", ts->tz, 1);
99                 tzset();
100         }
101         t = time(NULL);
102         tm = localtime(&t);
103
104         setlocale(LC_TIME, "");
105         strftime(p, p_max_size, ts->fmt, tm);
106         if (oldTZ) {
107                 setenv("TZ", oldTZ, 1);
108                 tzset();
109         } else {
110                 unsetenv("TZ");
111         }
112         // Needless to free oldTZ since getenv gives ptr to static data
113 }
114
115 void free_time(struct text_object *obj)
116 {
117         if (!obj->data.opaque)
118                 return;
119         free(obj->data.opaque);
120         obj->data.opaque = NULL;
121 }
122
123 void free_tztime(struct text_object *obj)
124 {
125         struct tztime_s *ts = obj->data.opaque;
126
127         if (!ts)
128                 return;
129
130         if (ts->tz)
131                 free(ts->tz);
132         if (ts->fmt)
133                 free(ts->fmt);
134
135         free(obj->data.opaque);
136         obj->data.opaque = NULL;
137 }