2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2009-10-03 15:46:59 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-02-27 15:14:19 +00:00
|
|
|
* Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
|
2009-10-03 15:46:59 +00:00
|
|
|
* (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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2010-03-05 13:20:04 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "timeinfo.h"
|
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <clocale>
|
|
|
|
#include <cstring>
|
|
|
|
#include <ctime>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "conky.h"
|
2009-12-05 20:00:10 +00:00
|
|
|
#include "logging.h"
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "text_object.h"
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2010-02-09 14:54:55 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2009-10-03 15:09:13 +00:00
|
|
|
struct tztime_s {
|
2018-05-12 16:03:00 +00:00
|
|
|
char *tz; /* timezone variable */
|
|
|
|
char *fmt; /* time display formatting */
|
2009-10-03 15:09:13 +00:00
|
|
|
};
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
conky::simple_config_setting<bool> times_in_seconds("times_in_seconds", false,
|
|
|
|
false);
|
2009-11-19 22:39:08 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void scan_time(struct text_object *obj, const char *arg) {
|
2018-05-12 23:26:31 +00:00
|
|
|
obj->data.opaque =
|
|
|
|
strndup(arg != nullptr ? arg : "%F %T", text_buffer_size.get(*state));
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void scan_tztime(struct text_object *obj, const char *arg) {
|
|
|
|
char buf1[256], buf2[256], *fmt, *tz;
|
|
|
|
struct tztime_s *ts;
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
fmt = tz = nullptr;
|
|
|
|
if (arg != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
int nArgs = sscanf(arg, "%255s %255[^\n]", buf1, buf2);
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-12-22 21:16:28 +00:00
|
|
|
if (nArgs == 2) {
|
|
|
|
fmt = buf2;
|
|
|
|
tz = buf1;
|
|
|
|
} else if (nArgs == 1) {
|
|
|
|
tz = buf1;
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
}
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
ts = static_cast<tztime_s *>(malloc(sizeof(struct tztime_s)));
|
2018-05-12 16:03:00 +00:00
|
|
|
memset(ts, 0, sizeof(struct tztime_s));
|
2018-05-12 23:26:31 +00:00
|
|
|
ts->fmt =
|
|
|
|
strndup(fmt != nullptr ? fmt : "%F %T", text_buffer_size.get(*state));
|
|
|
|
ts->tz = tz != nullptr ? strndup(tz, text_buffer_size.get(*state)) : nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = ts;
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_time(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
time_t t = time(nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
struct tm *tm = localtime(&t);
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
setlocale(LC_TIME, "");
|
2018-05-12 23:26:31 +00:00
|
|
|
strftime(p, p_max_size, static_cast<char *>(obj->data.opaque), tm);
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_utime(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
time_t t = time(nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
struct tm *tm = gmtime(&t);
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
setlocale(LC_TIME, "");
|
2018-05-12 23:26:31 +00:00
|
|
|
strftime(p, p_max_size, static_cast<char *>(obj->data.opaque), tm);
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 23:22:23 +00:00
|
|
|
void print_tztime(struct text_object *obj, char *p, unsigned int p_max_size) {
|
2018-05-12 23:26:31 +00:00
|
|
|
char *oldTZ = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
time_t t;
|
|
|
|
struct tm *tm;
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ts = static_cast<tztime_s *>(obj->data.opaque);
|
2009-10-03 15:09:13 +00:00
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
if (ts == nullptr) { return; }
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 23:26:31 +00:00
|
|
|
if (ts->tz != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
oldTZ = getenv("TZ");
|
|
|
|
setenv("TZ", ts->tz, 1);
|
|
|
|
tzset();
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
t = time(nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
tm = localtime(&t);
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
setlocale(LC_TIME, "");
|
|
|
|
strftime(p, p_max_size, ts->fmt, tm);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (oldTZ != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
setenv("TZ", oldTZ, 1);
|
|
|
|
tzset();
|
|
|
|
} else {
|
|
|
|
unsetenv("TZ");
|
|
|
|
}
|
|
|
|
// Needless to free oldTZ since getenv gives ptr to static data
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_time(struct text_object *obj) { free_and_zero(obj->data.opaque); }
|
2009-10-03 15:46:59 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void free_tztime(struct text_object *obj) {
|
2018-05-12 23:26:31 +00:00
|
|
|
auto *ts = static_cast<tztime_s *>(obj->data.opaque);
|
2009-10-03 15:09:13 +00:00
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
if (ts == nullptr) { return; }
|
2009-10-03 15:09:13 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(ts->tz);
|
|
|
|
free_and_zero(ts->fmt);
|
2009-10-03 15:09:13 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
free_and_zero(obj->data.opaque);
|
2009-10-03 15:46:59 +00:00
|
|
|
}
|
2009-11-18 14:35:35 +00:00
|
|
|
|
2009-12-20 11:28:27 +00:00
|
|
|
/* a safer asprintf()
|
|
|
|
* - no need to check for errors
|
|
|
|
* - exit conky on memory allocation failure
|
|
|
|
* - XXX: no return value at all, otherwise this
|
|
|
|
* could be used globally */
|
2018-05-12 16:03:00 +00:00
|
|
|
#define safe_asprintf(bufp, ...) \
|
|
|
|
{ \
|
|
|
|
int __v; \
|
|
|
|
if ((__v = asprintf(bufp, __VA_ARGS__)) == -1) { \
|
|
|
|
fprintf(stderr, "%s: memory allocation failed\n", __func__); \
|
|
|
|
exit(__v); \
|
|
|
|
} \
|
|
|
|
}
|
2009-12-20 11:28:27 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
// all chars after the ending " and between the seconds and the starting " are
|
|
|
|
// silently ignored, this is wanted behavior, not a bug, so don't "fix" this.
|
|
|
|
static void do_format_time(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
|
|
|
double seconds;
|
|
|
|
char *currentchar, *temp;
|
|
|
|
unsigned int output_length = 0;
|
|
|
|
int minutes, hours, days, weeks;
|
|
|
|
char show_minutes = 0, show_hours = 0, show_days = 0, show_weeks = 0,
|
|
|
|
hidestring;
|
2009-11-18 14:35:35 +00:00
|
|
|
|
2018-12-20 15:22:08 +00:00
|
|
|
if (!times_in_seconds.get(*state)) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("Enable \"times_in_seconds\" to use $format_time");
|
|
|
|
return;
|
|
|
|
}
|
2009-11-19 22:39:08 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
errno = 0;
|
|
|
|
seconds = strtod(obj->data.s, ¤tchar);
|
|
|
|
if (errno == 0 && obj->data.s != currentchar) {
|
2018-05-13 22:46:09 +00:00
|
|
|
while (*currentchar != 0 && *currentchar != '"') { currentchar++; }
|
2018-05-12 16:03:00 +00:00
|
|
|
if (*currentchar != 0) {
|
|
|
|
currentchar++;
|
|
|
|
minutes = seconds / 60;
|
|
|
|
seconds -= minutes * 60;
|
|
|
|
hours = minutes / 60;
|
|
|
|
minutes %= 60;
|
|
|
|
days = hours / 24;
|
|
|
|
hours %= 24;
|
|
|
|
weeks = days / 7;
|
|
|
|
days %= 7;
|
|
|
|
for (temp = currentchar; *temp != 0 && *temp != '"'; temp++) {
|
|
|
|
if (*temp == '\\') {
|
|
|
|
switch (*(temp + 1)) {
|
|
|
|
case '\\':
|
|
|
|
temp++;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
show_weeks = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
show_days = 1;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
show_hours = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
show_minutes = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-13 22:46:09 +00:00
|
|
|
if (show_weeks == 0) { days += weeks * 7; }
|
|
|
|
if (show_days == 0) { hours += days * 24; }
|
|
|
|
if (show_hours == 0) { minutes += hours * 60; }
|
|
|
|
if (show_minutes == 0) { seconds += minutes * 60; }
|
2018-05-12 16:03:00 +00:00
|
|
|
hidestring = 0;
|
|
|
|
while (output_length < p_max_size - 1) {
|
|
|
|
if (*currentchar != 0 && *currentchar != '"') {
|
2018-05-12 23:26:31 +00:00
|
|
|
temp = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
if (*currentchar == '\\' && hidestring == 0) {
|
|
|
|
currentchar++;
|
|
|
|
switch (*currentchar) {
|
|
|
|
case 'w':
|
|
|
|
safe_asprintf(&temp, "%d", weeks);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
safe_asprintf(&temp, "%d", days);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
safe_asprintf(&temp, "%d", hours);
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
safe_asprintf(&temp, "%d", minutes);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
safe_asprintf(&temp, "%d", (int)seconds);
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
currentchar++;
|
|
|
|
if (*currentchar >= '0' && *currentchar <= '9') {
|
|
|
|
safe_asprintf(&temp, "%.*f", (*currentchar) - '0', seconds);
|
|
|
|
} else if (*currentchar == 'x') {
|
2018-05-12 23:26:31 +00:00
|
|
|
if (seconds == static_cast<int>(seconds)) {
|
2018-05-12 16:03:00 +00:00
|
|
|
safe_asprintf(&temp, "%d", (int)seconds);
|
|
|
|
} else {
|
|
|
|
safe_asprintf(&temp, "%.9f", seconds);
|
|
|
|
while (*(temp + strlen(temp) - 1) == '0' ||
|
|
|
|
*(temp + strlen(temp) - 1) == '.') {
|
|
|
|
*(temp + strlen(temp) - 1) = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
currentchar--;
|
|
|
|
NORM_ERR(
|
|
|
|
"$format_time needs a digit behind 'S' to specify "
|
|
|
|
"precision");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
p[output_length] = *currentchar;
|
|
|
|
output_length++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NORM_ERR("$format_time doesn't have a special char '%c'",
|
|
|
|
*currentchar);
|
|
|
|
}
|
|
|
|
} else if (*currentchar == '(') {
|
|
|
|
for (temp = currentchar + 1; *temp != 0 && *temp != ')'; temp++) {
|
|
|
|
if (*(temp - 1) == '\\') {
|
|
|
|
switch (*temp) {
|
|
|
|
case 'w':
|
2018-05-13 22:46:09 +00:00
|
|
|
if (weeks == 0) { hidestring = 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2018-05-13 22:46:09 +00:00
|
|
|
if (days == 0) { hidestring = 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
2018-05-13 22:46:09 +00:00
|
|
|
if (hours == 0) { hidestring = 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2018-05-13 22:46:09 +00:00
|
|
|
if (minutes == 0) { hidestring = 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
2018-05-13 22:46:09 +00:00
|
|
|
if (seconds == 0) { hidestring = 1; }
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
temp = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
} else if (*currentchar == ')') {
|
|
|
|
hidestring = 0;
|
|
|
|
} else if (hidestring == 0) {
|
|
|
|
p[output_length] = *currentchar;
|
|
|
|
output_length++;
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
if (temp != nullptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
if (output_length + strlen(temp) < p_max_size - 1) {
|
2018-05-13 22:46:09 +00:00
|
|
|
strncpy(p + output_length, temp,
|
|
|
|
p_max_size - output_length + strlen(temp));
|
2018-05-12 16:03:00 +00:00
|
|
|
output_length += strlen(temp);
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("The format string for $format_time is too long");
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
currentchar++;
|
2018-05-12 23:26:31 +00:00
|
|
|
} else {
|
2018-05-12 16:03:00 +00:00
|
|
|
break;
|
2018-05-12 23:26:31 +00:00
|
|
|
}
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
|
|
|
p[output_length] = 0;
|
|
|
|
} else {
|
|
|
|
NORM_ERR(
|
|
|
|
"$format_time needs a output-format starting with a \"-char as 2nd "
|
|
|
|
"argument");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NORM_ERR("$format_time didn't receive a time in seconds as first argument");
|
|
|
|
}
|
2009-11-18 14:35:35 +00:00
|
|
|
}
|
2009-11-25 23:20:42 +00:00
|
|
|
|
2018-12-22 21:16:28 +00:00
|
|
|
void print_format_time(struct text_object *obj, char *p,
|
|
|
|
unsigned int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
std::unique_ptr<char[]> buf(new char[max_user_text.get(*state)]);
|
2009-11-25 23:20:42 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
generate_text_internal(buf.get(), max_user_text.get(*state), *obj->sub);
|
|
|
|
obj->data.s = buf.get();
|
|
|
|
do_format_time(obj, p, p_max_size);
|
2009-11-25 23:20:42 +00:00
|
|
|
}
|