Skip to content

Commit

Permalink
Move the date time formatting to shared utils
Browse files Browse the repository at this point in the history
No need to have multiple copies of this code.
  • Loading branch information
Matthias Clasen committed Dec 4, 2016
1 parent 98bc87e commit c7d0cdb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 108 deletions.
12 changes: 0 additions & 12 deletions src/gr-recipe-exporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 0 additions & 42 deletions src/gr-recipe-importer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
54 changes: 0 additions & 54 deletions src/gr-recipe-store.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
56 changes: 56 additions & 0 deletions src/gr-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "config.h"

#include <stdlib.h>

#include "gr-utils.h"

/* load image rotated by angle to fit in width x height while preserving
Expand Down Expand Up @@ -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;
}

3 changes: 3 additions & 0 deletions src/gr-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c7d0cdb

Please sign in to comment.