Improve build date conversion code

Don't use strptime() as it is not very portable, instead use a GDate and use the
code also for the date output in --version.
This commit is contained in:
Enrico Tröger 2013-03-17 17:16:32 +01:00
parent d270e6c690
commit fbce364182
4 changed files with 43 additions and 7 deletions

View File

@ -153,8 +153,7 @@ static GtkWidget *create_dialog(void)
gchar buffer[512];
gchar buffer2[128];
guint i, row = 0;
struct tm builddate_tm;
char builddate_local[255];
gchar *build_date;
dialog = gtk_dialog_new();
@ -227,10 +226,9 @@ static GtkWidget *create_dialog(void)
gtk_label_set_justify(GTK_LABEL(builddate_label), GTK_JUSTIFY_CENTER);
gtk_label_set_selectable(GTK_LABEL(builddate_label), TRUE);
gtk_label_set_use_markup(GTK_LABEL(builddate_label), TRUE);
memset(&builddate_tm, 0, sizeof(struct tm));
strptime(__DATE__, "%b %d %Y", &builddate_tm);
strftime(builddate_local, sizeof(builddate_local), GEANY_TEMPLATES_FORMAT_DATE, &builddate_tm);
g_snprintf(buffer2, sizeof(buffer2), _("(built on or after %s)"), builddate_local);
build_date = utils_parse_and_format_build_date(__DATE__);
g_snprintf(buffer2, sizeof(buffer2), _("(built on or after %s)"), build_date);
g_free(build_date);
g_snprintf(buffer, sizeof(buffer), BUILDDATE, buffer2);
gtk_label_set_markup(GTK_LABEL(builddate_label), buffer);
gtk_misc_set_padding(GTK_MISC(builddate_label), 2, 2);

View File

@ -568,13 +568,16 @@ static void parse_command_line_options(gint *argc, gchar ***argv)
if (show_version)
{
gchar *build_date = utils_parse_and_format_build_date(__DATE__);
printf(PACKAGE " %s (", main_get_version_string());
/* note for translators: library versions are printed after this */
printf(_("built on %s with "), __DATE__);
printf(_("built on %s with "), build_date);
printf(geany_lib_versions,
GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
printf(")\n");
g_free(build_date);
wait_for_input_on_windows();
exit(0);
}

View File

@ -52,6 +52,7 @@
#include "win32.h"
#include "project.h"
#include "ui_utils.h"
#include "templates.h"
#include "utils.h"
@ -2100,3 +2101,33 @@ gchar **utils_strv_join(gchar **first, gchar **second)
g_free(second);
return strv;
}
/* Try to parse a date using g_date_set_parse(). It doesn't take any format hint,
* obviously g_date_set_parse() uses some magic.
* The returned GDate object must be freed. */
GDate *utils_parse_date(const gchar *input)
{
GDate *date = g_date_new();
g_date_set_parse(date, input);
if (g_date_valid(date))
return date;
g_date_free(date);
return NULL;
}
gchar *utils_parse_and_format_build_date(const gchar *input)
{
gchar date_buf[255];
GDate *date = utils_parse_date(input);
if (date != NULL)
g_date_strftime(date_buf, sizeof(date_buf), GEANY_TEMPLATES_FORMAT_DATE, date);
return g_strdup(date_buf);
return g_strdup(input);
}

View File

@ -277,6 +277,10 @@ gchar *utils_str_remove_chars(gchar *string, const gchar *chars);
gchar **utils_copy_environment(const gchar **exclude_vars, const gchar *first_varname, ...) G_GNUC_NULL_TERMINATED;
GDate *utils_parse_date(const gchar *input);
gchar *utils_parse_and_format_build_date(const gchar *input);
G_END_DECLS
#endif