* cosmetics

This commit is contained in:
djcb
2012-01-21 11:10:19 +02:00
parent 336b09db42
commit 14834a01ee
2 changed files with 37 additions and 39 deletions

View File

@ -242,4 +242,3 @@ mu_cmd_cfind (MuConfig *opts, GError **err)
return run_cmd_cfind (opts->params[1], opts->format, opts->color, return run_cmd_cfind (opts->params[1], opts->format, opts->color,
err); err);
} }

View File

@ -1,5 +1,5 @@
/* /*
** Copyright (C) 2011 <djcb@djcbsoftware.nl> ** Copyright (C) 2012 <djcb@djcbsoftware.nl>
** **
** This program is free software; you can redistribute it and/or modify it ** 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 ** under the terms of the GNU General Public License as published by the
@ -24,18 +24,18 @@
#include "mu-date.h" #include "mu-date.h"
const char* const char*
mu_date_str_s (const char* frm, time_t t) mu_date_str_s (const char* frm, time_t t)
{ {
struct tm *tmbuf; struct tm *tmbuf;
static char buf[128]; static char buf[128];
static int is_utf8 = -1; static int is_utf8 = -1;
if (G_UNLIKELY(is_utf8 == -1)) if (G_UNLIKELY(is_utf8 == -1))
is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0; is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;
g_return_val_if_fail (frm, NULL); g_return_val_if_fail (frm, NULL);
tmbuf = localtime(&t); tmbuf = localtime(&t);
strftime (buf, sizeof(buf), frm, tmbuf); strftime (buf, sizeof(buf), frm, tmbuf);
@ -52,26 +52,26 @@ mu_date_str_s (const char* frm, time_t t)
strcpy (buf, "<error>"); strcpy (buf, "<error>");
} else } else
strncpy (buf, conv, sizeof(buf)); strncpy (buf, conv, sizeof(buf));
g_free (conv); g_free (conv);
} }
return buf; return buf;
} }
char* char*
mu_date_str (const char *frm, time_t t) mu_date_str (const char *frm, time_t t)
{ {
return g_strdup (mu_date_str_s(frm, t)); return g_strdup (mu_date_str_s(frm, t));
} }
const char* const char*
mu_date_display_s (time_t t) mu_date_display_s (time_t t)
{ {
time_t now; time_t now;
static const time_t SECS_IN_DAY = 24 * 60 * 60; static const time_t SECS_IN_DAY = 24 * 60 * 60;
now = time (NULL); now = time (NULL);
if (ABS(now - t) > SECS_IN_DAY) if (ABS(now - t) > SECS_IN_DAY)
@ -89,18 +89,18 @@ mu_date_parse_hdwmy (const char *nptr)
char *endptr; char *endptr;
time_t now, delta; time_t now, delta;
time_t never = (time_t)-1; time_t never = (time_t)-1;
g_return_val_if_fail (nptr, never); g_return_val_if_fail (nptr, never);
num = strtol (nptr, &endptr, 10); num = strtol (nptr, &endptr, 10);
if (num <= 0 || num > 9999) if (num <= 0 || num > 9999)
return never; return never;
if (endptr == NULL || *endptr == '\0') if (endptr == NULL || *endptr == '\0')
return never; return never;
switch (endptr[0]) { switch (endptr[0]) {
case 'h': /* hour */ case 'h': /* hour */
delta = num * 60 * 60; break; delta = num * 60 * 60; break;
case 'd': /* day */ case 'd': /* day */
delta = num * 24 * 60 * 60; break; delta = num * 24 * 60 * 60; break;
@ -109,13 +109,13 @@ mu_date_parse_hdwmy (const char *nptr)
case 'm': /* month */ case 'm': /* month */
delta = num * 30 * 24 * 60 * 60; break; delta = num * 30 * 24 * 60 * 60; break;
case 'y': /* year */ case 'y': /* year */
delta = num * 365 * 24 * 60 * 60; break; delta = num * 365 * 24 * 60 * 60; break;
default: default:
return never; return never;
} }
now = time(NULL); now = time(NULL);
return delta <= now ? now - delta : never; return delta <= now ? now - delta : never;
} }
@ -123,7 +123,7 @@ const char*
mu_date_complete_s (const char *date, gboolean is_begin) mu_date_complete_s (const char *date, gboolean is_begin)
{ {
static char fulldate[14 + 1]; static char fulldate[14 + 1];
static const char* full_begin = "00000101000000"; static const char* full_begin = "00000101000000";
static const char* full_end = "99991231235959"; static const char* full_end = "99991231235959";
@ -132,7 +132,7 @@ mu_date_complete_s (const char *date, gboolean is_begin)
strncpy (fulldate, is_begin ? full_begin : full_end, strncpy (fulldate, is_begin ? full_begin : full_end,
sizeof(fulldate)); sizeof(fulldate));
memcpy (fulldate, date, strlen(date)); memcpy (fulldate, date, strlen(date));
return fulldate; return fulldate;
} }
@ -158,7 +158,7 @@ mu_date_interpret_s (const char *datespec, gboolean is_begin)
g_return_val_if_fail (datespec, NULL); g_return_val_if_fail (datespec, NULL);
now = time(NULL); now = time(NULL);
if (strcmp (datespec, "today") == 0) { if (strcmp (datespec, "today") == 0) {
strftime(fulldate, sizeof(fulldate), strftime(fulldate, sizeof(fulldate),
is_begin ? "%Y%m%d000000" : "%Y%m%d235959", is_begin ? "%Y%m%d000000" : "%Y%m%d235959",
localtime(&now)); localtime(&now));
@ -170,7 +170,7 @@ mu_date_interpret_s (const char *datespec, gboolean is_begin)
localtime(&now)); localtime(&now));
return fulldate; return fulldate;
} }
{ {
time_t t; time_t t;
t = mu_date_parse_hdwmy (datespec); t = mu_date_parse_hdwmy (datespec);
@ -178,7 +178,7 @@ mu_date_interpret_s (const char *datespec, gboolean is_begin)
strftime(fulldate, sizeof(fulldate), "%Y%m%d%H%M%S", strftime(fulldate, sizeof(fulldate), "%Y%m%d%H%M%S",
localtime(&t)); localtime(&t));
return fulldate; return fulldate;
} }
} }
return datespec; /* nothing changed */ return datespec; /* nothing changed */
@ -189,9 +189,9 @@ char*
mu_date_interpret (const char *datespec, gboolean is_begin) mu_date_interpret (const char *datespec, gboolean is_begin)
{ {
char *s; char *s;
g_return_val_if_fail (datespec, NULL); g_return_val_if_fail (datespec, NULL);
s = mu_date_interpret (datespec, is_begin); s = mu_date_interpret (datespec, is_begin);
return s ? g_strdup(s) : NULL; return s ? g_strdup(s) : NULL;
} }
@ -204,22 +204,22 @@ mu_date_str_to_time_t (const char* date, gboolean local)
char mydate[14 + 1]; /* YYYYMMDDHHMMSS */ char mydate[14 + 1]; /* YYYYMMDDHHMMSS */
time_t t; time_t t;
const char *tz; const char *tz;
memset (&tm, 0, sizeof(struct tm)); memset (&tm, 0, sizeof(struct tm));
strncpy (mydate, date, 15); strncpy (mydate, date, 15);
mydate[sizeof(mydate)-1]='\0'; mydate[sizeof(mydate)-1]='\0';
g_return_val_if_fail (date, (time_t)-1); g_return_val_if_fail (date, (time_t)-1);
tm.tm_sec = atoi (mydate + 12); mydate[12] = '\0'; tm.tm_sec = atoi (mydate + 12); mydate[12] = '\0';
tm.tm_min = atoi (mydate + 10); mydate[10] = '\0'; tm.tm_min = atoi (mydate + 10); mydate[10] = '\0';
tm.tm_hour = atoi (mydate + 8); mydate[8] = '\0'; tm.tm_hour = atoi (mydate + 8); mydate[8] = '\0';
tm.tm_mday = atoi (mydate + 6); mydate[6] = '\0'; tm.tm_mday = atoi (mydate + 6); mydate[6] = '\0';
tm.tm_mon = atoi (mydate + 4) - 1; mydate[4] = '\0'; tm.tm_mon = atoi (mydate + 4) - 1; mydate[4] = '\0';
tm.tm_year = atoi (mydate) - 1900; tm.tm_year = atoi (mydate) - 1900;
tm.tm_isdst = -1; /* figure out the dst */ tm.tm_isdst = -1; /* figure out the dst */
if (!local) { /* temporalily switch to UTC */ if (!local) { /* temporarily switch to UTC */
tz = getenv ("TZ"); tz = getenv ("TZ");
setenv ("TZ", "", 1); setenv ("TZ", "", 1);
tzset (); tzset ();
@ -243,12 +243,12 @@ mu_date_time_t_to_str_s (time_t t, gboolean local)
{ {
/* static char datestr[14 + 1]; /\* YYYYMMDDHHMMSS *\/ */ /* static char datestr[14 + 1]; /\* YYYYMMDDHHMMSS *\/ */
static char datestr[14+1]; /* YYYYMMDDHHMMSS */ static char datestr[14+1]; /* YYYYMMDDHHMMSS */
static const char *frm = "%Y%m%d%H%M%S"; static const char *frm = "%Y%m%d%H%M%S";
strftime (datestr, sizeof(datestr), frm, strftime (datestr, sizeof(datestr), frm,
local ? localtime (&t) : gmtime(&t)); local ? localtime (&t) : gmtime(&t));
return datestr; return datestr;
} }
@ -260,6 +260,5 @@ mu_date_time_t_to_str (time_t t, gboolean local)
str = mu_date_time_t_to_str_s (t, local); str = mu_date_time_t_to_str_s (t, local);
return str ? g_strdup(str): NULL; return str ? g_strdup(str): NULL;
} }