Merge pull request #107 from JosephMcc/time-fix1

time-plugin: Use GDateTime
This commit is contained in:
Clement Lefebvre 2017-04-02 11:32:46 +01:00 committed by GitHub
commit 21e46cc751
1 changed files with 10 additions and 48 deletions

View File

@ -29,10 +29,6 @@
#endif #endif
#include <string.h> #include <string.h>
#include <time.h>
#include "xed-time-plugin.h"
#include <xed/xed-app.h>
#include <glib/gi18n-lib.h> #include <glib/gi18n-lib.h>
#include <glib.h> #include <glib.h>
@ -43,6 +39,8 @@
#include <libpeas-gtk/peas-gtk-configurable.h> #include <libpeas-gtk/peas-gtk-configurable.h>
#include <xed/xed-debug.h> #include <xed/xed-debug.h>
#include <xed/xed-utils.h> #include <xed/xed-utils.h>
#include <xed/xed-app.h>
#include "xed-time-plugin.h"
#define XED_TIME_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \ #define XED_TIME_PLUGIN_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
XED_TYPE_TIME_PLUGIN, \ XED_TYPE_TIME_PLUGIN, \
@ -71,7 +69,6 @@ static const gchar *formats[] =
"%a %d %b %Y %H:%M:%S", "%a %d %b %Y %H:%M:%S",
"%d/%m/%Y", "%d/%m/%Y",
"%d/%m/%y", "%d/%m/%y",
"%D",
"%A %d %B %Y", "%A %d %B %Y",
"%A %B %d %Y", "%A %B %d %Y",
"%Y-%m-%d", "%Y-%m-%d",
@ -88,9 +85,7 @@ static const gchar *formats[] =
"%I.%M %p", "%I.%M %p",
"%d/%m/%Y %H:%M:%S", "%d/%m/%Y %H:%M:%S",
"%d/%m/%y %H:%M:%S", "%d/%m/%y %H:%M:%S",
#if __GLIBC__ >= 2
"%a, %d %b %Y %H:%M:%S %z", "%a, %d %b %Y %H:%M:%S %z",
#endif
NULL NULL
}; };
@ -321,56 +316,23 @@ get_custom_format (XedTimePlugin *plugin)
static gchar * static gchar *
get_time (const gchar *format) get_time (const gchar *format)
{ {
gchar *out = NULL; gchar *out;
gchar *out_utf8 = NULL; GDateTime *now;
time_t clock;
struct tm *now;
size_t out_length = 0;
gchar *locale_format;
xed_debug (DEBUG_PLUGINS); xed_debug (DEBUG_PLUGINS);
g_return_val_if_fail (format != NULL, NULL); g_return_val_if_fail (format != NULL, NULL);
if (strlen (format) == 0) if (*format == '\0')
{ {
return g_strdup (" "); return g_strdup (" ");
} }
locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL); now = g_date_time_new_now_local ();
if (locale_format == NULL) out = g_date_time_format (now, format);
{ g_date_time_unref (now);
return g_strdup (" ");
}
clock = time (NULL); return out;
now = localtime (&clock);
do
{
out_length += 255;
out = g_realloc (out, out_length);
}
while (strftime (out, out_length, locale_format, now) == 0);
g_free (locale_format);
if (g_utf8_validate (out, -1, NULL))
{
out_utf8 = out;
}
else
{
out_utf8 = g_locale_to_utf8 (out, -1, NULL, NULL, NULL);
g_free (out);
if (out_utf8 == NULL)
{
out_utf8 = g_strdup (" ");
}
}
return out_utf8;
} }
static void static void