mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-04-02 05:43:07 +02:00
Merge branch 'th/use-localtime-r' into 'main'
[th/use-localtime-r] use localtime_r() in g_log_writer_format_fields() See merge request GNOME/glib!3543
This commit is contained in:
commit
0c1e719bdb
69
glib/gdate.c
69
glib/gdate.c
@ -53,6 +53,7 @@
|
|||||||
#include "gtestutils.h"
|
#include "gtestutils.h"
|
||||||
#include "gthread.h"
|
#include "gthread.h"
|
||||||
#include "gunicode.h"
|
#include "gunicode.h"
|
||||||
|
#include "gutilsprivate.h"
|
||||||
|
|
||||||
#ifdef G_OS_WIN32
|
#ifdef G_OS_WIN32
|
||||||
#include "garray.h"
|
#include "garray.h"
|
||||||
@ -1403,6 +1404,33 @@ g_date_set_parse (GDate *d,
|
|||||||
G_UNLOCK (g_date_global);
|
G_UNLOCK (g_date_global);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
gboolean
|
||||||
|
_g_localtime (time_t timet, struct tm *out_tm)
|
||||||
|
{
|
||||||
|
gboolean success = TRUE;
|
||||||
|
|
||||||
|
#ifdef HAVE_LOCALTIME_R
|
||||||
|
if (!localtime_r (&timet, out_tm))
|
||||||
|
success = FALSE;
|
||||||
|
#else
|
||||||
|
{
|
||||||
|
struct tm *ptm = localtime (&timet);
|
||||||
|
|
||||||
|
if (ptm == NULL)
|
||||||
|
{
|
||||||
|
/* Happens at least in Microsoft's C library if you pass a
|
||||||
|
* negative time_t.
|
||||||
|
*/
|
||||||
|
success = FALSE;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
memcpy (out_tm, ptm, sizeof (struct tm));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_date_set_time_t:
|
* g_date_set_time_t:
|
||||||
* @date: a #GDate
|
* @date: a #GDate
|
||||||
@ -1427,33 +1455,21 @@ g_date_set_time_t (GDate *date,
|
|||||||
time_t timet)
|
time_t timet)
|
||||||
{
|
{
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
|
gboolean success;
|
||||||
|
|
||||||
g_return_if_fail (date != NULL);
|
g_return_if_fail (date != NULL);
|
||||||
|
|
||||||
#ifdef HAVE_LOCALTIME_R
|
|
||||||
localtime_r (&timet, &tm);
|
|
||||||
#else
|
|
||||||
{
|
|
||||||
struct tm *ptm = localtime (&timet);
|
|
||||||
|
|
||||||
if (ptm == NULL)
|
success = _g_localtime (timet, &tm);
|
||||||
{
|
if (!success)
|
||||||
/* Happens at least in Microsoft's C library if you pass a
|
{
|
||||||
* negative time_t. Use 2000-01-01 as default date.
|
/* Still set a default date, 2000-01-01.
|
||||||
*/
|
*
|
||||||
#ifndef G_DISABLE_CHECKS
|
* We may assert out below. */
|
||||||
g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "ptm != NULL");
|
tm.tm_mon = 0;
|
||||||
#endif
|
tm.tm_mday = 1;
|
||||||
|
tm.tm_year = 100;
|
||||||
|
}
|
||||||
|
|
||||||
tm.tm_mon = 0;
|
|
||||||
tm.tm_mday = 1;
|
|
||||||
tm.tm_year = 100;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
memcpy ((void *) &tm, (void *) ptm, sizeof(struct tm));
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
date->julian = FALSE;
|
date->julian = FALSE;
|
||||||
|
|
||||||
date->month = tm.tm_mon + 1;
|
date->month = tm.tm_mon + 1;
|
||||||
@ -1463,6 +1479,11 @@ g_date_set_time_t (GDate *date,
|
|||||||
g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
|
g_return_if_fail (g_date_valid_dmy (date->day, date->month, date->year));
|
||||||
|
|
||||||
date->dmy = TRUE;
|
date->dmy = TRUE;
|
||||||
|
|
||||||
|
#ifndef G_DISABLE_CHECKS
|
||||||
|
if (!success)
|
||||||
|
g_return_if_fail_warning (G_LOG_DOMAIN, "g_date_set_time", "localtime() == NULL");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -186,22 +186,23 @@
|
|||||||
#include <sys/uio.h>
|
#include <sys/uio.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "glib-init.h"
|
|
||||||
#include "galloca.h"
|
#include "galloca.h"
|
||||||
#include "gbacktrace.h"
|
#include "gbacktrace.h"
|
||||||
#include "gcharset.h"
|
#include "gcharset.h"
|
||||||
#include "gconvert.h"
|
#include "gconvert.h"
|
||||||
#include "genviron.h"
|
#include "genviron.h"
|
||||||
|
#include "glib-init.h"
|
||||||
#include "glib-private.h"
|
#include "glib-private.h"
|
||||||
#include "gmain.h"
|
#include "gmain.h"
|
||||||
#include "gmem.h"
|
#include "gmem.h"
|
||||||
|
#include "gpattern.h"
|
||||||
#include "gprintfint.h"
|
#include "gprintfint.h"
|
||||||
#include "gtestutils.h"
|
|
||||||
#include "gthread.h"
|
|
||||||
#include "gstrfuncs.h"
|
#include "gstrfuncs.h"
|
||||||
#include "gstring.h"
|
#include "gstring.h"
|
||||||
#include "gpattern.h"
|
#include "gtestutils.h"
|
||||||
|
#include "gthread.h"
|
||||||
#include "gthreadprivate.h"
|
#include "gthreadprivate.h"
|
||||||
|
#include "gutilsprivate.h"
|
||||||
|
|
||||||
#if defined(__linux__) && !defined(__BIONIC__)
|
#if defined(__linux__) && !defined(__BIONIC__)
|
||||||
#include "gjournal-private.h"
|
#include "gjournal-private.h"
|
||||||
@ -2260,7 +2261,7 @@ g_log_writer_format_fields (GLogLevelFlags log_level,
|
|||||||
GString *gstring;
|
GString *gstring;
|
||||||
gint64 now;
|
gint64 now;
|
||||||
time_t now_secs;
|
time_t now_secs;
|
||||||
struct tm *now_tm;
|
struct tm now_tm;
|
||||||
gchar time_buf[128];
|
gchar time_buf[128];
|
||||||
|
|
||||||
/* Extract some common fields. */
|
/* Extract some common fields. */
|
||||||
@ -2313,9 +2314,8 @@ g_log_writer_format_fields (GLogLevelFlags log_level,
|
|||||||
/* Timestamp */
|
/* Timestamp */
|
||||||
now = g_get_real_time ();
|
now = g_get_real_time ();
|
||||||
now_secs = (time_t) (now / 1000000);
|
now_secs = (time_t) (now / 1000000);
|
||||||
now_tm = localtime (&now_secs);
|
if (_g_localtime (now_secs, &now_tm))
|
||||||
if (G_LIKELY (now_tm != NULL))
|
strftime (time_buf, sizeof (time_buf), "%H:%M:%S", &now_tm);
|
||||||
strftime (time_buf, sizeof (time_buf), "%H:%M:%S", now_tm);
|
|
||||||
else
|
else
|
||||||
strcpy (time_buf, "(error)");
|
strcpy (time_buf, "(error)");
|
||||||
|
|
||||||
|
@ -26,6 +26,8 @@
|
|||||||
#include "gtypes.h"
|
#include "gtypes.h"
|
||||||
#include "gtestutils.h"
|
#include "gtestutils.h"
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
void g_set_user_dirs (const gchar *first_dir_type,
|
void g_set_user_dirs (const gchar *first_dir_type,
|
||||||
@ -55,6 +57,8 @@ g_nearest_pow (gsize num)
|
|||||||
|
|
||||||
void _g_unset_cached_tmp_dir (void);
|
void _g_unset_cached_tmp_dir (void);
|
||||||
|
|
||||||
|
gboolean _g_localtime (time_t timet, struct tm *tm);
|
||||||
|
|
||||||
G_END_DECLS
|
G_END_DECLS
|
||||||
|
|
||||||
#endif /* __G_UTILS_PRIVATE_H__ */
|
#endif /* __G_UTILS_PRIVATE_H__ */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user