[libgda] Added gda_connection_get_date_format()



commit da4657cfa5fdc4be28fd9e6885025d453159574f
Author: Vivien Malerba <malerba gnome-db org>
Date:   Thu Sep 26 21:23:07 2013 +0200

    Added gda_connection_get_date_format()

 doc/C/libgda-sections.txt |    1 +
 libgda/gda-connection.c   |  130 +++++++++++++++++++++++++++++++++++++++++++++
 libgda/gda-connection.h   |    3 +
 libgda/libgda.symbols     |    1 +
 4 files changed, 135 insertions(+), 0 deletions(-)
---
diff --git a/doc/C/libgda-sections.txt b/doc/C/libgda-sections.txt
index 4d90534..1e17549 100644
--- a/doc/C/libgda-sections.txt
+++ b/doc/C/libgda-sections.txt
@@ -147,6 +147,7 @@ gda_connection_get_provider_name
 gda_connection_get_dsn
 gda_connection_get_cnc_string
 gda_connection_get_authentication
+gda_connection_get_date_format
 <SUBSECTION>
 gda_connection_get_events
 <SUBSECTION>
diff --git a/libgda/gda-connection.c b/libgda/gda-connection.c
index 83a130a..6c54969 100644
--- a/libgda/gda-connection.c
+++ b/libgda/gda-connection.c
@@ -65,6 +65,7 @@
 #include <gda-statement-priv.h>
 #include <sqlite/virtual/gda-vconnection-data-model.h>
 #include <libgda/gda-debug-macros.h>
+#include <libgda/gda-data-handler.h>
 
 #include <glib/gstdio.h>
 #include <fcntl.h>
@@ -1936,6 +1937,135 @@ gda_connection_get_authentication (GdaConnection *cnc)
 }
 
 /**
+ * gda_connection_get_date_format:
+ * @cnc: a #GdaConnection object
+ * @out_first: (out) (allow-none): the place to store the first part of the date, or %NULL
+ * @out_second: (out) (allow-none): the place to store the second part of the date, or %NULL
+ * @out_third: (out) (allow-none): the place to store the third part of the date, or %NULL
+ * @out_sep: (out) (allow-none): the place to store the separator (used between year, month and day parts) 
part of the date, or %NULL
+ * @error: (allow-none): a place to store errors, or %NULL
+ *
+ * This function allows you to determine the actual format for the date values.
+ *
+ * Returns: %TRUE if no error occurred
+ *
+ * Since: 5.2
+ */
+gboolean
+gda_connection_get_date_format (GdaConnection *cnc, GDateDMY *out_first,
+                               GDateDMY *out_second, GDateDMY *out_third, gchar *out_sep,
+                               GError **error)
+{
+       g_return_val_if_fail (GDA_IS_CONNECTION (cnc), FALSE);
+
+       GdaDataHandler *dh;
+       dh = gda_server_provider_get_data_handler_g_type (cnc->priv->provider_obj, cnc, G_TYPE_DATE);
+       if (!dh) {
+               g_set_error (error, GDA_SERVER_PROVIDER_ERROR, 
GDA_SERVER_PROVIDER_METHOD_NON_IMPLEMENTED_ERROR,
+                            "%s", _("Provider does not provide a GdaDataHandler for dates"));
+               return FALSE;
+       }
+
+       GDate *tdate;
+       tdate = g_date_new_dmy (15, 12, 2003);
+       g_assert (tdate && g_date_valid (tdate));
+
+       GValue *value;
+       value = gda_value_new (G_TYPE_DATE);
+       g_value_set_boxed (value, tdate);
+       g_date_free (tdate);
+
+       gchar *str;
+       str = gda_data_handler_get_str_from_value (dh, value);
+       gda_value_free (value);
+
+       /* parsing */
+       guint nb;
+       gchar *ptr;
+       GDateDMY order[3];
+       gchar sep;
+
+       /* 1st part */
+       for (nb = 0, ptr = str; *ptr; ptr++) {
+               if ((*ptr <= '9') && (*ptr >= '0'))
+                       nb = nb * 10 + (*ptr - '0');
+               else
+                       break;
+       }
+       if (nb == 2003)
+               order[0] = G_DATE_YEAR;
+       else if (nb == 12)
+               order[0] = G_DATE_MONTH;
+       else if (nb == 15)
+               order[0] = G_DATE_DAY;
+       else {
+               g_free (str);
+               return FALSE;
+       }
+
+       /* separator */
+       sep = *ptr;
+       if (!sep) {
+               g_free (str);
+               return FALSE;
+       }
+
+       /* 2nd part */
+       for (nb = 0, ptr++; *ptr; ptr++) {
+               if ((*ptr <= '9') && (*ptr >= '0'))
+                       nb = nb * 10 + (*ptr - '0');
+               else
+                       break;
+       }
+       if (nb == 2003)
+               order[1] = G_DATE_YEAR;
+       else if (nb == 12)
+               order[1] = G_DATE_MONTH;
+       else if (nb == 15)
+               order[1] = G_DATE_DAY;
+       else {
+               g_free (str);
+               return FALSE;
+       }
+
+       if (sep != *ptr) {
+               g_free (str);
+               return FALSE;
+       }
+
+       /* 3rd part */
+       for (nb = 0, ptr++; *ptr; ptr++) {
+               if ((*ptr <= '9') && (*ptr >= '0'))
+                       nb = nb * 10 + (*ptr - '0');
+               else
+                       break;
+       }
+       if (nb == 2003)
+               order[2] = G_DATE_YEAR;
+       else if (nb == 12)
+               order[2] = G_DATE_MONTH;
+       else if (nb == 15)
+               order[2] = G_DATE_DAY;
+       else {
+               g_free (str);
+               return FALSE;
+       }
+       g_free (str);
+
+       /* result */
+       if (out_first)
+               *out_first = order [0];
+       if (out_second)
+               *out_second = order [1];
+       if (out_third)
+               *out_third = order [2];
+       if (out_sep)
+               *out_sep = sep;
+
+       return TRUE;
+}
+
+/**
  * gda_connection_insert_row_into_table: (skip)
  * @cnc: an opened connection
  * @table: table's name to insert into
diff --git a/libgda/gda-connection.h b/libgda/gda-connection.h
index 3140121..331b52b 100644
--- a/libgda/gda-connection.h
+++ b/libgda/gda-connection.h
@@ -299,6 +299,9 @@ gboolean             gda_connection_perform_operation    (GdaConnection *cnc, Gd
 const gchar         *gda_connection_get_dsn              (GdaConnection *cnc);
 const gchar         *gda_connection_get_cnc_string       (GdaConnection *cnc);
 const gchar         *gda_connection_get_authentication   (GdaConnection *cnc);
+gboolean             gda_connection_get_date_format      (GdaConnection *cnc, GDateDMY *out_first,
+                                                         GDateDMY *out_second, GDateDMY *out_third, gchar 
*out_sep,
+                                                         GError **error);
 
 GdaStatement        *gda_connection_parse_sql_string     (GdaConnection *cnc, const gchar *sql, GdaSet 
**params,
                     GError **error);
diff --git a/libgda/libgda.symbols b/libgda/libgda.symbols
index 3b3a7f2..2d84342 100644
--- a/libgda/libgda.symbols
+++ b/libgda/libgda.symbols
@@ -127,6 +127,7 @@
        gda_connection_feature_get_type
        gda_connection_get_authentication
        gda_connection_get_cnc_string
+       gda_connection_get_date_format
        gda_connection_get_dsn
        gda_connection_get_events
        gda_connection_get_meta_store


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]