From c7d0cdbc1d0b75dcc0b03b3da53920b901cb6579 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sat, 3 Dec 2016 21:17:29 -0500 Subject: [PATCH] Move the date time formatting to shared utils No need to have multiple copies of this code. --- src/gr-recipe-exporter.c | 12 --------- src/gr-recipe-importer.c | 42 ------------------------------ src/gr-recipe-store.c | 54 -------------------------------------- src/gr-utils.c | 56 ++++++++++++++++++++++++++++++++++++++++ src/gr-utils.h | 3 +++ 5 files changed, 59 insertions(+), 108 deletions(-) diff --git a/src/gr-recipe-exporter.c b/src/gr-recipe-exporter.c index e7b7a97..d7657c2 100644 --- a/src/gr-recipe-exporter.c +++ b/src/gr-recipe-exporter.c @@ -157,18 +157,6 @@ decide_dest_cb (AutoarCompressor *compressor, g_set_object (&exporter->dest, file); } -static char * -date_time_to_string (GDateTime *dt) -{ - return g_strdup_printf ("%d-%d-%d %d:%d:%d", - g_date_time_get_year (dt), - g_date_time_get_month (dt), - g_date_time_get_day_of_month (dt), - g_date_time_get_hour (dt), - g_date_time_get_minute (dt), - g_date_time_get_second (dt)); -} - static gboolean prepare_export (GrRecipeExporter *exporter, GError **error) diff --git a/src/gr-recipe-importer.c b/src/gr-recipe-importer.c index f93cbe2..089a3df 100644 --- a/src/gr-recipe-importer.c +++ b/src/gr-recipe-importer.c @@ -94,48 +94,6 @@ gr_recipe_importer_new (GtkWindow *parent) return importer; } -static GDateTime * -date_time_from_string (const char *string) -{ - g_auto(GStrv) s = NULL; - g_auto(GStrv) s1 = NULL; - g_auto(GStrv) s2 = NULL; - int year, month, day, hour, minute, second; - char *endy, *endm, *endd, *endh, *endmi, *ends; - - s = g_strsplit (string, " ", -1); - if (g_strv_length (s) != 2) - return NULL; - - s1 = g_strsplit (s[0], "-", -1); - if (g_strv_length (s1) != 3) - return NULL; - - s2 = g_strsplit (s[1], ":", -1); - if (g_strv_length (s1) != 3) - return NULL; - - year = strtol (s1[0], &endy, 10); - month = strtol (s1[1], &endm, 10); - day = strtol (s1[2], &endd, 10); - - hour = strtol (s2[0], &endh, 10); - minute = strtol (s2[1], &endmi, 10); - second = strtol (s2[2], &ends, 10); - - if (!*endy && !*endm && !*endd && - !*endh && !*endmi && !*ends && - 0 < month && month <= 12 && - 0 < day && day <= 31 && - 0 <= hour && hour < 24 && - 0 <= minute && minute < 60 && - 0 <= second && second < 60) - return g_date_time_new_utc (year, month, day, - hour, minute, second); - - return NULL; -} - static void cleanup_import (GrRecipeImporter *importer) { diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c index 64e9e45..22df5b6 100644 --- a/src/gr-recipe-store.c +++ b/src/gr-recipe-store.c @@ -69,60 +69,6 @@ gr_recipe_store_finalize (GObject *object) G_OBJECT_CLASS (gr_recipe_store_parent_class)->finalize (object); } -static char * -date_time_to_string (GDateTime *dt) -{ - return g_strdup_printf ("%d-%d-%d %d:%d:%d", - g_date_time_get_year (dt), - g_date_time_get_month (dt), - g_date_time_get_day_of_month (dt), - g_date_time_get_hour (dt), - g_date_time_get_minute (dt), - g_date_time_get_second (dt)); -} - -static GDateTime * -date_time_from_string (const char *string) -{ - g_auto(GStrv) s = NULL; - g_auto(GStrv) s1 = NULL; - g_auto(GStrv) s2 = NULL; - int year, month, day, hour, minute, second; - char *endy, *endm, *endd, *endh, *endmi, *ends; - - s = g_strsplit (string, " ", -1); - if (g_strv_length (s) != 2) - return NULL; - - s1 = g_strsplit (s[0], "-", -1); - if (g_strv_length (s1) != 3) - return NULL; - - s2 = g_strsplit (s[1], ":", -1); - if (g_strv_length (s1) != 3) - return NULL; - - year = strtol (s1[0], &endy, 10); - month = strtol (s1[1], &endm, 10); - day = strtol (s1[2], &endd, 10); - - hour = strtol (s2[0], &endh, 10); - minute = strtol (s2[1], &endmi, 10); - second = strtol (s2[2], &ends, 10); - - if (!*endy && !*endm && !*endd && - !*endh && !*endmi && !*ends && - 0 < month && month <= 12 && - 0 < day && day <= 31 && - 0 <= hour && hour < 24 && - 0 <= minute && minute < 60 && - 0 <= second && second < 60) - return g_date_time_new_utc (year, month, day, - hour, minute, second); - - return NULL; -} - static gboolean load_recipes (GrRecipeStore *self, const char *dir) diff --git a/src/gr-utils.c b/src/gr-utils.c index ccf45a6..603d6cd 100644 --- a/src/gr-utils.c +++ b/src/gr-utils.c @@ -20,6 +20,8 @@ #include "config.h" +#include + #include "gr-utils.h" /* load image rotated by angle to fit in width x height while preserving @@ -231,3 +233,57 @@ gr_utils_widget_set_css_simple (GtkWidget *widget, gr_utils_widget_set_css_internal (widget, class_name, str->str); } +char * +date_time_to_string (GDateTime *dt) +{ + return g_strdup_printf ("%d-%d-%d %d:%d:%d", + g_date_time_get_year (dt), + g_date_time_get_month (dt), + g_date_time_get_day_of_month (dt), + g_date_time_get_hour (dt), + g_date_time_get_minute (dt), + g_date_time_get_second (dt)); +} + +GDateTime * +date_time_from_string (const char *string) +{ + g_auto(GStrv) s = NULL; + g_auto(GStrv) s1 = NULL; + g_auto(GStrv) s2 = NULL; + int year, month, day, hour, minute, second; + char *endy, *endm, *endd, *endh, *endmi, *ends; + + s = g_strsplit (string, " ", -1); + if (g_strv_length (s) != 2) + return NULL; + + s1 = g_strsplit (s[0], "-", -1); + if (g_strv_length (s1) != 3) + return NULL; + + s2 = g_strsplit (s[1], ":", -1); + if (g_strv_length (s1) != 3) + return NULL; + + year = strtol (s1[0], &endy, 10); + month = strtol (s1[1], &endm, 10); + day = strtol (s1[2], &endd, 10); + + hour = strtol (s2[0], &endh, 10); + minute = strtol (s2[1], &endmi, 10); + second = strtol (s2[2], &ends, 10); + + if (!*endy && !*endm && !*endd && + !*endh && !*endmi && !*ends && + 0 < month && month <= 12 && + 0 < day && day <= 31 && + 0 <= hour && hour < 24 && + 0 <= minute && minute < 60 && + 0 <= second && second < 60) + return g_date_time_new_utc (year, month, day, + hour, minute, second); + + return NULL; +} + diff --git a/src/gr-utils.h b/src/gr-utils.h index 9354c04..21f7ef8 100644 --- a/src/gr-utils.h +++ b/src/gr-utils.h @@ -42,4 +42,7 @@ void container_remove_all (GtkContainer *container); void gr_utils_widget_set_css_simple (GtkWidget *widget, const char *css); +char * date_time_to_string (GDateTime *dt); +GDateTime * date_time_from_string (const char *string); + G_END_DECLS