[grisbi-cvs] [SCM] grisbi branch, master, updated. upstream_version_0_9_3-19-g6040fc3

Philippe Delorme nobody at users.sourceforge.net
Mon Aug 29 19:42:34 CEST 2011


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "grisbi".

The branch, master has been updated
       via  6040fc34afb222bfe6476dd2d4df455770221a78 (commit)
       via  82740b4fdbe71c47faf1346cfec9e8ee130cbfb2 (commit)
      from  796b36bfa5b9e50d86de24bbed23620315a14adc (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 6040fc34afb222bfe6476dd2d4df455770221a78
Author: Philippe Delorme <philippedelorme at users.sourceforge.net>
Date:   Mon Aug 29 19:35:49 2011 +0200

    Added tests for new date parsing feature (month as string)

commit 82740b4fdbe71c47faf1346cfec9e8ee130cbfb2
Author: Philippe Delorme <philippedelorme at users.sourceforge.net>
Date:   Mon Aug 29 19:32:59 2011 +0200

    Fixed 1385: Transaction date revert to import date when importing .csv data
    
    Had been fixed by adding support of string month in date parsing. Dates likes 01/Jan/2011
    are now recognized. Case insensitive. Tests are coming in the next push.

-----------------------------------------------------------------------

Changes:
diff --git a/src/tests/utils_dates_cunit.c b/src/tests/utils_dates_cunit.c
index d458a26..6077056 100644
--- a/src/tests/utils_dates_cunit.c
+++ b/src/tests/utils_dates_cunit.c
@@ -51,6 +51,7 @@ static int utils_dates_cunit_init_suite ( void );
 /* START_EXTERN */
 /* END_EXTERN */
 
+
 /* The suite initialization function.
  * Returns zero on success, non-zero otherwise.
  */
@@ -59,6 +60,7 @@ int utils_dates_cunit_init_suite ( void )
     return 0;
 }
 
+
 /* The suite cleanup function.
  * Returns zero on success, non-zero otherwise.
  */
@@ -67,6 +69,7 @@ int utils_dates_cunit_clean_suite ( void )
     return 0;
 }
 
+
 void utils_dates_cunit__gsb_parse_date_string ( void )
 {
     GDate *date = NULL;
@@ -234,6 +237,68 @@ void utils_dates_cunit__gsb_parse_date_string ( void )
 
 
     /**
+     * date tests with string as months instead of integers
+     */
+
+    gsb_date_set_format_date ( "%m/%d/%Y" );
+
+    date = gsb_parse_date_string ( "Jan/31/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(1, g_date_get_month(date));
+    CU_ASSERT_EQUAL(31, g_date_get_day(date));
+    g_date_free( date );
+
+    date = gsb_parse_date_string ( "JAN/31/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(1, g_date_get_month(date));
+    CU_ASSERT_EQUAL(31, g_date_get_day(date));
+    g_date_free( date );
+
+    date = gsb_parse_date_string ( "jan/31/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(1, g_date_get_month(date));
+    CU_ASSERT_EQUAL(31, g_date_get_day(date));
+    g_date_free( date );
+
+    date = gsb_parse_date_string ( "Dec/31/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(12, g_date_get_month(date));
+    CU_ASSERT_EQUAL(31, g_date_get_day(date));
+    g_date_free( date );
+
+    /* invalid months */
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "January/01/2011" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "December/01/2011" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "MARCH/01/2011" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "MAR /01/2011" ) );
+    /* wrong order */
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "01/Jan/2011" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "31/Dec/2011" ) );
+
+    gsb_date_set_format_date ( "%d/%m/%Y" );
+
+    date = gsb_parse_date_string ( "14/Jul/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(7, g_date_get_month(date));
+    CU_ASSERT_EQUAL(14, g_date_get_day(date));
+    g_date_free( date );
+
+    date = gsb_parse_date_string ( "31/Dec/2011" );
+    CU_ASSERT_EQUAL(2011, g_date_get_year(date));
+    CU_ASSERT_EQUAL(12, g_date_get_month(date));
+    CU_ASSERT_EQUAL(31, g_date_get_day(date));
+    g_date_free( date );
+
+    /* invalid months */
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "01/MAR /2011" ) );
+    /* wrong order */
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "Jan/01/2011" ) );
+    CU_ASSERT_EQUAL ( NULL, gsb_parse_date_string ( "Dec/31/2011" ) );
+
+
+    /**
      * tear down
      */
     gsb_date_set_format_date ( NULL );
@@ -241,6 +306,8 @@ void utils_dates_cunit__gsb_parse_date_string ( void )
     gsb_regex_destroy ( );
 }
 
+
+
 CU_pSuite utils_dates_cunit_create_suite ( void )
 {
     CU_pSuite pSuite = CU_add_suite("utils_dates",
@@ -249,9 +316,11 @@ CU_pSuite utils_dates_cunit_create_suite ( void )
     if ( NULL == pSuite )
         return NULL;
 
-    if ( ( NULL == CU_add_test ( pSuite, "of gsb_parse_date_string()", utils_dates_cunit__gsb_parse_date_string ) )
-       )
+    if ( NULL == CU_add_test ( pSuite, "of gsb_parse_date_string()", 
+                               utils_dates_cunit__gsb_parse_date_string ) )
         return NULL;
 
     return pSuite;
 }
+
+
diff --git a/src/utils_dates.c b/src/utils_dates.c
index 5ec5217..c2cc964 100644
--- a/src/utils_dates.c
+++ b/src/utils_dates.c
@@ -28,6 +28,7 @@
 #include "include.h"
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #if defined(_MSC_VER) || defined (_MINGW)
 #include <winnls.h>
 #else
@@ -45,8 +46,7 @@
 /*END_INCLUDE*/
 
 /*START_STATIC*/
-/* format pour les dates */
-static gchar *format = NULL;
+static int gsb_date_get_month_from_string ( const gchar * );
 /*END_STATIC*/
 
 /*START_EXTERN*/
@@ -58,10 +58,21 @@ static gchar *format = NULL;
  * optional ( optional separator + 2 digits + 
  *            optional ( optional separator + 2 or 4 digits ) )
  */
-#define DATE_STRING_REGEX       "^(\\d{1,2})(?:[-/.:]?(\\d{1,2})(?:[-/.:]?(\\d{2}(?:\\d{2})?))?)?$"
+#define DATE_STRING_REGEX       "^(\\d{1,2}|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?:[-/.:]?(\\d{1,2}|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)(?:[-/.:]?(\\d{2}(?:\\d{2})?))?)?$"
 #define DATE_STRING_KEY         "date_string"
 
 
+
+/* months */
+static const gchar *months[] = {
+    "Jan", "Feb", "Mar", "Apr", 
+    "May", "Jun", "Jul", "Aug",
+    "Sep", "Oct", "Nov", "Dec"
+};
+
+/* format pour les dates */
+static gchar *format = NULL;
+
 /* save of the last date entried */
 static gchar *last_date = NULL;
 
@@ -305,8 +316,8 @@ gboolean gsb_date_check_entry ( GtkWidget *entry )
 
 /**
  * Create and try to return a GDate from a string representation of a date.
- * separator can be / . - :
- * and numbers can be stick (ex 01012001)
+ * separator can be "/.-:" and numbers can be stick (ex 01012001)
+ * Moreover, month can be specified as its non-localized string format (ex Jan)
  *
  * \param a string wich represent a date
  *
@@ -333,7 +344,10 @@ GDate *gsb_parse_date_string ( const gchar *date_string )
     {
         /* only for the first call */
         devel_debug ( DATE_STRING_KEY );
-        date_regex = gsb_regex_insert ( DATE_STRING_KEY, DATE_STRING_REGEX, 0, 0 );
+        date_regex = gsb_regex_insert ( DATE_STRING_KEY, 
+                                        DATE_STRING_REGEX, 
+                                        G_REGEX_CASELESS, 
+                                        0 );
         if ( ! date_regex )
         {
             /* big problem */
@@ -356,11 +370,21 @@ GDate *gsb_parse_date_string ( const gchar *date_string )
           i < num_tokens && j < num_fields;
           i ++, j ++ )
     {
+        /* the string can represent:
+         * EITHER   a number (1 for January)
+         * OR       a 3-length string (Jan for january)
+         * We assume this is an integer as default behaviour */
         gint nvalue = atoi ( tab_date[j] );
 
         switch ( date_tokens[i][0] )
         {
             case 'm':
+                /* If month is NOT an integer, nvalue = 0, and we have 
+                 * to convert month string into an integer. If string is 
+                 * not valid, the function returns 0 which is not a valid
+                 * month -> goto invalid -> right behaviour!!! */
+                if ( isalpha ( tab_date[j][0] ) != 0 )
+                    nvalue = gsb_date_get_month_from_string ( tab_date[j] );
                 if ( ! g_date_valid_month ( nvalue ) )
                     goto invalid;
                 g_date_set_month ( date, nvalue );
@@ -553,14 +577,7 @@ gchar *gsb_date_get_compiled_time ( void )
     tab = g_strsplit ( str, " ", -1 );
     g_free ( str );
 
-    for (i = 0; i < 12; i++)
-    {
-        if ( !strcmp ( tab[0], months[i] ) )
-        {
-          mois = i + 1;
-          break;
-        }
-    }
+    mois = gsb_date_get_month_from_string ( tab[0] );
 
     date = g_date_new_dmy ( atoi ( tab[1] ), mois, atoi ( tab[2] ) );
     g_strfreev (tab);
@@ -624,6 +641,46 @@ void gsb_date_set_format_date ( const gchar *format_date )
 }
 
 
+/**
+ * Returns the integer of the month, as in GDateMonth (ie 1 for January, 
+ * ..., 12 for December). This function is case-insensitive.
+ *
+ * \param month A 3-length string representing the month
+ *
+ * \return The integet from 1 to 12, or 0 otherwise 
+ */
+int gsb_date_get_month_from_string ( const gchar *month )
+{
+    int i;
+    gchar *tmp;
+
+    if ( !month )
+        return 0;
+
+    /* first put the case of it is expected:
+     * Uppercase first letter, and lower case for the two remaining */
+    tmp = g_strndup ( month, 3 );
+    tmp[0] = toupper ( tmp[0] );
+    tmp[1] = tolower ( tmp[1] );
+    tmp[2] = tolower ( tmp[2] );
+
+    /* find the month */
+    for (i = 0; i < 12; i ++)
+    {
+        if ( !strcmp ( tmp, months[i] ) )
+        {
+            g_free ( tmp );
+            /* return the real month number, so index array + 1 */
+            return i + 1;
+        }
+    }
+
+    g_free ( tmp );
+    return 0;
+}
+
+
+
 /* Local Variables: */
 /* c-basic-offset: 4 */
 /* End: */


hooks/post-receive
-- 
grisbi


More information about the cvs mailing list