text_object: completely remove the need for data.ifblock
[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 "conky.h"
32 #include "text_object.h"
33 #include <locale.h>
34 #include <string.h>
35 #include <time.h>
36
37 struct tztime_s {
38         char *tz;       /* timezone variable */
39         char *fmt;      /* time display formatting */
40 };
41
42 void scan_time(struct text_object *obj, const char *arg)
43 {
44         obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size);
45 }
46
47 void scan_tztime(struct text_object *obj, const char *arg)
48 {
49         char buf1[256], buf2[256], *fmt, *tz;
50         struct tztime_s *ts;
51
52         fmt = tz = NULL;
53         if (arg) {
54                 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
55
56                 switch (nArgs) {
57                         case 2:
58                                 fmt = buf2;
59                         case 1:
60                                 tz = buf1;
61                 }
62         }
63
64         ts = malloc(sizeof(struct tztime_s));
65         memset(ts, 0, sizeof(struct tztime_s));
66         ts->fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
67         ts->tz = tz ? strndup(tz, text_buffer_size) : NULL;
68         obj->data.opaque = ts;
69 }
70
71 void print_time(struct text_object *obj, char *p, int p_max_size)
72 {
73         time_t t = time(NULL);
74         struct tm *tm = localtime(&t);
75
76         setlocale(LC_TIME, "");
77         strftime(p, p_max_size, (char *)obj->data.opaque, tm);
78 }
79
80 void print_utime(struct text_object *obj, char *p, int p_max_size)
81 {
82         time_t t = time(NULL);
83         struct tm *tm = gmtime(&t);
84
85         setlocale(LC_TIME, "");
86         strftime(p, p_max_size, (char *)obj->data.opaque, tm);
87 }
88
89 void print_tztime(struct text_object *obj, char *p, int p_max_size)
90 {
91         char *oldTZ = NULL;
92         time_t t;
93         struct tm *tm;
94         struct tztime_s *ts = obj->data.opaque;
95
96         if (!ts)
97                 return;
98
99         if (ts->tz) {
100                 oldTZ = getenv("TZ");
101                 setenv("TZ", ts->tz, 1);
102                 tzset();
103         }
104         t = time(NULL);
105         tm = localtime(&t);
106
107         setlocale(LC_TIME, "");
108         strftime(p, p_max_size, ts->fmt, tm);
109         if (oldTZ) {
110                 setenv("TZ", oldTZ, 1);
111                 tzset();
112         } else {
113                 unsetenv("TZ");
114         }
115         // Needless to free oldTZ since getenv gives ptr to static data
116 }
117
118 void free_time(struct text_object *obj)
119 {
120         if (!obj->data.opaque)
121                 return;
122         free(obj->data.opaque);
123         obj->data.opaque = NULL;
124 }
125
126 void free_tztime(struct text_object *obj)
127 {
128         struct tztime_s *ts = obj->data.opaque;
129
130         if (!ts)
131                 return;
132
133         if (ts->tz)
134                 free(ts->tz);
135         if (ts->fmt)
136                 free(ts->fmt);
137
138         free(obj->data.opaque);
139         obj->data.opaque = NULL;
140 }