1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-25 04:06:03 +00:00

outsource time, utime and tztime into a separate file

This commit is contained in:
Phil Sutter 2009-10-03 17:46:59 +02:00
parent e3e77ce75e
commit adf9d23927
5 changed files with 181 additions and 53 deletions

View File

@ -53,8 +53,8 @@ mandatory_sources = colours.c colours.h common.c common.h conky.c conky.h \
core.c core.h diskio.c diskio.h fs.c fs.h logging.h mail.c mail.h \
mixer.c mixer.h template.c template.h timed_thread.c timed_thread.h \
mboxscan.c mboxscan.h specials.c specials.h tailhead.c tailhead.h \
temphelper.c temphelper.h text_object.c text_object.h algebra.c \
algebra.h
temphelper.c temphelper.h text_object.c text_object.h timeinfo.c \
timeinfo.h algebra.c algebra.h
# source files only needed when the apropriate option is enabled
audacious = audacious.c audacious.h

View File

@ -88,6 +88,7 @@
#include "temphelper.h"
#include "template.h"
#include "tailhead.h"
#include "timeinfo.h"
#include "top.h"
/* check for OS and include appropriate headers */
@ -2342,40 +2343,13 @@ static void generate_text_internal(char *p, int p_max_size,
snprintf(p, p_max_size, "%s", cur->uname_s.sysname);
}
OBJ(time) {
time_t t = time(NULL);
struct tm *tm = localtime(&t);
setlocale(LC_TIME, "");
strftime(p, p_max_size, obj->data.s, tm);
print_time(obj, p, p_max_size);
}
OBJ(utime) {
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
strftime(p, p_max_size, obj->data.s, tm);
print_utime(obj, p, p_max_size);
}
OBJ(tztime) {
char *oldTZ = NULL;
time_t t;
struct tm *tm;
if (obj->data.tztime.tz) {
oldTZ = getenv("TZ");
setenv("TZ", obj->data.tztime.tz, 1);
tzset();
}
t = time(NULL);
tm = localtime(&t);
setlocale(LC_TIME, "");
strftime(p, p_max_size, obj->data.tztime.fmt, tm);
if (oldTZ) {
setenv("TZ", oldTZ, 1);
tzset();
} else {
unsetenv("TZ");
}
// Needless to free oldTZ since getenv gives ptr to static data
print_tztime(obj, p, p_max_size);
}
OBJ(totaldown) {
human_readable(obj->data.net->recv, p, 255);

View File

@ -47,6 +47,7 @@
#include "temphelper.h"
#include "template.h"
#include "tailhead.h"
#include "timeinfo.h"
#include "top.h"
/* check for OS and include appropriate headers */
@ -1356,26 +1357,11 @@ struct text_object *construct_text_object(const char *s, const char *arg, long
scan_bar(arg, &obj->data.pair.a, &obj->data.pair.b);
END OBJ(sysname, 0)
END OBJ(time, 0)
obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
scan_time(obj, arg);
END OBJ(utime, 0)
obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
scan_time(obj, arg);
END OBJ(tztime, 0)
char buf1[256], buf2[256], *fmt, *tz;
fmt = tz = NULL;
if (arg) {
int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
switch (nArgs) {
case 2:
tz = buf1;
case 1:
fmt = buf2;
}
}
obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
scan_tztime(obj, arg);
#ifdef HAVE_ICONV
END OBJ_ARG(iconv_start, 0, "Iconv requires arguments")
char iconv_from[ICONV_CODEPAGE_LENGTH];
@ -2224,11 +2210,10 @@ void free_text_objects(struct text_object *root, int internal)
break;
case OBJ_time:
case OBJ_utime:
free(data.s);
free_time(obj);
break;
case OBJ_tztime:
free(data.tztime.tz);
free(data.tztime.fmt);
free_tztime(obj);
break;
case OBJ_mboxscan:
free(data.mboxscan.args);

120
src/timeinfo.c Normal file
View File

@ -0,0 +1,120 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "text_object.h"
#include <locale.h>
void scan_time(struct text_object *obj, const char *arg)
{
obj->data.s = strndup(arg ? arg : "%F %T", text_buffer_size);
}
void scan_tztime(struct text_object *obj, const char *arg)
{
char buf1[256], buf2[256], *fmt, *tz;
fmt = tz = NULL;
if (arg) {
int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
switch (nArgs) {
case 2:
tz = buf1;
case 1:
fmt = buf2;
}
}
obj->data.tztime.fmt = strndup(fmt ? fmt : "%F %T", text_buffer_size);
obj->data.tztime.tz = tz ? strndup(tz, text_buffer_size) : NULL;
}
void print_time(struct text_object *obj, char *p, int p_max_size)
{
time_t t = time(NULL);
struct tm *tm = localtime(&t);
setlocale(LC_TIME, "");
strftime(p, p_max_size, obj->data.s, tm);
}
void print_utime(struct text_object *obj, char *p, int p_max_size)
{
time_t t = time(NULL);
struct tm *tm = gmtime(&t);
setlocale(LC_TIME, "");
strftime(p, p_max_size, obj->data.s, tm);
}
void print_tztime(struct text_object *obj, char *p, int p_max_size)
{
char *oldTZ = NULL;
time_t t;
struct tm *tm;
if (obj->data.tztime.tz) {
oldTZ = getenv("TZ");
setenv("TZ", obj->data.tztime.tz, 1);
tzset();
}
t = time(NULL);
tm = localtime(&t);
setlocale(LC_TIME, "");
strftime(p, p_max_size, obj->data.tztime.fmt, tm);
if (oldTZ) {
setenv("TZ", oldTZ, 1);
tzset();
} else {
unsetenv("TZ");
}
// Needless to free oldTZ since getenv gives ptr to static data
}
void free_time(struct text_object *obj)
{
if (!obj->data.s)
return;
free(obj->data.s);
obj->data.s = NULL;
}
void free_tztime(struct text_object *obj)
{
if (obj->data.tztime.tz) {
free(obj->data.tztime.tz);
obj->data.tztime.tz = NULL;
}
if (obj->data.tztime.fmt) {
free(obj->data.tztime.fmt);
obj->data.tztime.fmt = NULL;
}
}

49
src/timeinfo.h Normal file
View File

@ -0,0 +1,49 @@
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
* vim: ts=4 sw=4 noet ai cindent syntax=c
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef _TIMEINFO_H
#define _TIMEINFO_H
/* since time and utime are quite equal, certain functions
* are shared in between both text object types. */
/* parse args passed to *time objects */
void scan_time(struct text_object *, const char *);
void scan_tztime(struct text_object *, const char *);
/* print the time */
void print_time(struct text_object *, char *, int);
void print_utime(struct text_object *, char *, int);
void print_tztime(struct text_object *, char *, int);
/* free object data */
void free_time(struct text_object *);
void free_tztime(struct text_object *);
#endif /* _TIMEINFO_H */