* use timegm instead of the tzset hack

This commit is contained in:
djcb 2013-10-07 00:50:36 +03:00
parent def8094b16
commit c898265ee5
3 changed files with 48 additions and 35 deletions

View File

@ -94,6 +94,7 @@ AS_IF([test "x$ac_cv_member_struct_dirent_d_ino" != "xyes"],
# we need these
AC_CHECK_FUNCS([memset memcpy realpath setlocale strerror getpass])
AC_CHECK_FUNC(timegm,[],[AC_MSG_ERROR([missing required function timegm])])
# require pkg-config
AC_PATH_PROG([PKG_CONFIG], [pkg-config], [no])

View File

@ -75,6 +75,26 @@ get_refs_str (GMimeMessage *msg)
return rv;
}
static void
print_date (GMimeMessage *msg)
{
time_t t;
int tz;
char buf[64];
size_t len;
struct tm *t_m;
g_mime_message_get_date (msg, &t, &tz);
t_m = localtime (&t);
len = strftime (buf, sizeof(buf) - 1, "%c", t_m);
if (len > 0)
g_print ("Date : %s (%s%04d)\n",
buf,tz < 0 ? "-" : "+", tz);
}
static gboolean
test_message (GMimeMessage *msg)
{
@ -98,6 +118,9 @@ test_message (GMimeMessage *msg)
str = g_mime_message_get_subject (msg);
g_print ("Subject: %s\n", str ? str : "<none>");
print_date (msg);
str = g_mime_message_get_message_id (msg);
g_print ("Msg-id : %s\n", str ? str : "<none>");

View File

@ -28,9 +28,10 @@
const char*
mu_date_str_s (const char* frm, time_t t)
{
struct tm *tmbuf;
static char buf[128];
static int is_utf8 = -1;
struct tm *tmbuf;
static char buf[128];
static int is_utf8 = -1;
size_t len;
if (G_UNLIKELY(is_utf8 == -1))
is_utf8 = mu_util_locale_is_utf8 () ? 1 : 0;
@ -38,7 +39,9 @@ mu_date_str_s (const char* frm, time_t t)
g_return_val_if_fail (frm, NULL);
tmbuf = localtime(&t);
strftime (buf, sizeof(buf), frm, tmbuf);
len = strftime (buf, sizeof(buf) - 1, frm, tmbuf);
if (len == 0)
return ""; /* not necessarily an error... */
if (!is_utf8) {
/* charset is _not_ utf8, so we need to convert it, so
@ -223,14 +226,12 @@ time_t
mu_date_str_to_time_t (const char* date, gboolean local)
{
struct tm tm;
char tzbuf[16], mydate[14 + 1]; /* YYYYMMDDHHMMSS */
char *oldtz;
char mydate[14 + 1]; /* YYYYMMDDHHMMSS */
time_t t;
memset (&tm, 0, sizeof(struct tm));
strncpy (mydate, date, 15);
mydate[sizeof(mydate)-1]='\0';
oldtz = tzbuf;
mydate[sizeof(mydate)-1] = '\0';
g_return_val_if_fail (date, (time_t)-1);
@ -240,29 +241,13 @@ mu_date_str_to_time_t (const char* date, gboolean local)
tm.tm_mday = atoi (mydate + 6); mydate[6] = '\0';
tm.tm_mon = atoi (mydate + 4) - 1; mydate[4] = '\0';
tm.tm_year = atoi (mydate) - 1900;
tm.tm_isdst = -1; /* let mktime figure out the dst */
tm.tm_isdst = -1;
/* let timegm/mktime figure out the dst */
if (!local) { /* temporarily switch to UTC */
const char *tz;
tz = getenv ("TZ");
if (tz && strlen (tz) < sizeof(tz) -1) {
strcpy (oldtz, tz);
setenv ("TZ", "", 1);
tzset ();
} else
oldtz = NULL;
} else
oldtz = NULL;
t = mktime (&tm);
if (!local) { /* switch back */
if (oldtz)
setenv("TZ", oldtz, 1);
else
unsetenv("TZ");
tzset ();
}
if (local)
t = mktime (&tm);
else
t = timegm (&tm); /* GNU/BSD specific */
return t;
}
@ -271,12 +256,16 @@ const char*
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 const char *frm = "%Y%m%d%H%M%S";
size_t len;
static const char *frm = "%Y%m%d%H%M%S";
strftime (datestr, sizeof(datestr), frm,
local ? localtime (&t) : gmtime(&t));
len = strftime (datestr, sizeof(datestr), frm,
local ? localtime (&t) : gmtime(&t));
if (len == 0) {
g_warning ("bug: error converting time");
return "00000000000000";
}
return datestr;
}