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
4 * Conky, a system monitor, based on torsmo
6 * Any original torsmo code is licensed under the BSD license
8 * All code written since the fork of torsmo is licensed under the GPL
10 * Please see COPYING for details
12 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13 * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
15 * All rights reserved.
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.
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/>.
32 #include "text_object.h"
40 char *tz; /* timezone variable */
41 char *fmt; /* time display formatting */
44 void scan_time(struct text_object *obj, const char *arg)
46 obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size);
49 void scan_tztime(struct text_object *obj, const char *arg)
51 char buf1[256], buf2[256], *fmt, *tz;
56 int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
66 ts = malloc(sizeof(struct tztime_s));
67 memset(ts, 0, sizeof(struct tztime_s));
68 ts->fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
69 ts->tz = tz ? strndup(tz, text_buffer_size) : NULL;
70 obj->data.opaque = ts;
73 void print_time(struct text_object *obj, char *p, int p_max_size)
75 time_t t = time(NULL);
76 struct tm *tm = localtime(&t);
78 setlocale(LC_TIME, "");
79 strftime(p, p_max_size, (char *)obj->data.opaque, tm);
82 void print_utime(struct text_object *obj, char *p, int p_max_size)
84 time_t t = time(NULL);
85 struct tm *tm = gmtime(&t);
87 setlocale(LC_TIME, "");
88 strftime(p, p_max_size, (char *)obj->data.opaque, tm);
91 void print_tztime(struct text_object *obj, char *p, int p_max_size)
96 struct tztime_s *ts = obj->data.opaque;
102 oldTZ = getenv("TZ");
103 setenv("TZ", ts->tz, 1);
109 setlocale(LC_TIME, "");
110 strftime(p, p_max_size, ts->fmt, tm);
112 setenv("TZ", oldTZ, 1);
117 // Needless to free oldTZ since getenv gives ptr to static data
120 void free_time(struct text_object *obj)
122 if (!obj->data.opaque)
124 free(obj->data.opaque);
125 obj->data.opaque = NULL;
128 void free_tztime(struct text_object *obj)
130 struct tztime_s *ts = obj->data.opaque;
140 free(obj->data.opaque);
141 obj->data.opaque = NULL;
144 //all chars after the ending " and between the seconds and the starting " are silently
145 //ignored, this is wanted behavior, not a bug, so don't "fix" this.
146 void print_format_time(struct text_object *obj, char *p, unsigned int p_max_size) {
148 char *currentchar, *temp;
149 unsigned int output_length = 0;
150 int minutes, hours, days, weeks;
151 char show_minutes = 0, show_hours = 0, show_days = 0, show_weeks = 0, hidestring;
154 seconds = strtod(obj->data.s, ¤tchar);
155 if(errno == 0 && obj->data.s != currentchar) {
156 while(*currentchar != 0 && *currentchar != '"') {
159 if(*currentchar != 0) {
161 minutes = seconds / 60;
162 seconds -= minutes * 60;
163 hours = minutes / 60;
169 for(temp = currentchar; *temp != 0 && *temp != '"'; temp++) {
190 if(show_weeks == 0) days += weeks * 7;
191 if(show_days == 0) hours += days * 24;
192 if(show_hours == 0) minutes += hours * 60;
193 if(show_minutes == 0) seconds += minutes * 60;
195 while(output_length < p_max_size - 1) {
196 if(*currentchar != 0 && *currentchar != '"') {
198 if(*currentchar == '\\' && hidestring == 0) {
200 switch(*currentchar){
202 asprintf(&temp, "%d", weeks);
205 asprintf(&temp, "%d", days);
208 asprintf(&temp, "%d", hours);
211 asprintf(&temp, "%d", minutes);
214 asprintf(&temp, "%d", (int) seconds);
218 if(*currentchar >= '0' && *currentchar <= '9') {
219 asprintf(&temp, "%.*f", (*currentchar) - '0', seconds);
220 } else if(*currentchar == 'x') {
221 asprintf(&temp, "%.9f", seconds);
222 while(*(temp + strlen(temp) - 1) == '0' || *(temp + strlen(temp) - 1) == '.') {
223 *(temp + strlen(temp) - 1) = 0;
225 if(*temp == 0) *temp = '0';
228 NORM_ERR("$format_time needs a digit behind 'S' to specify precision")
234 p[output_length] = *currentchar;
238 NORM_ERR("$format_time doesn't have a special char '%c'", *currentchar)
240 } else if(*currentchar == '(') {
241 for(temp = currentchar + 1; *temp != 0 && *temp != ')'; temp++) {
242 if(*(temp-1) == '\\') {
245 if(weeks == 0) hidestring = 1;
248 if(days == 0) hidestring = 1;
251 if(hours == 0) hidestring = 1;
254 if(minutes == 0) hidestring = 1;
258 if(seconds == 0) hidestring = 1;
264 } else if(*currentchar == ')') {
266 } else if(hidestring == 0) {
267 p[output_length] = *currentchar;
271 if(output_length + strlen(temp) < p_max_size - 1) {
272 strcpy(p + output_length, temp);
273 output_length += strlen(temp);
274 } else NORM_ERR("The format string for $format_time is too long")
280 p[output_length] = 0;
282 NORM_ERR("$format_time needs a output-format starting with a \"-char as 2nd argument")
285 NORM_ERR("$format_time didn't receive a time in seconds as first argument")