glom r1587 - in branches/glom-1-6: . glom/libglom/data_structure regression_tests



Author: murrayc
Date: Tue Apr 22 15:24:14 2008
New Revision: 1587
URL: http://svn.gnome.org/viewvc/glom?rev=1587&view=rev

Log:
2008-04-22  Murray Cumming  <murrayc murrayc com>

* glom/libglom/data_structure/glomconversions.cc:
parse_time(): Fall back to using strptime(), in various formats, 
so we can parse 01:00 PM and 13:00, instead of parsing these as 
01:00 AM.

* Makefile.am:
* configure.in:
* regression_tests/Makefile.am:
* regression_tests/test_parsing_time.cc: Added a test for this time 
parsing.

Added:
   branches/glom-1-6/regression_tests/
   branches/glom-1-6/regression_tests/Makefile.am
   branches/glom-1-6/regression_tests/test_parsing_time.cc
Modified:
   branches/glom-1-6/ChangeLog
   branches/glom-1-6/configure.in
   branches/glom-1-6/glom/libglom/data_structure/glomconversions.cc

Modified: branches/glom-1-6/configure.in
==============================================================================
--- branches/glom-1-6/configure.in	(original)
+++ branches/glom-1-6/configure.in	Tue Apr 22 15:24:14 2008
@@ -207,6 +207,7 @@
     po/Makefile.in \
   macros/Makefile \
   examples/Makefile \
+  regression_tests/Makefile \
   glom.desktop.in
 )
 

Modified: branches/glom-1-6/glom/libglom/data_structure/glomconversions.cc
==============================================================================
--- branches/glom-1-6/glom/libglom/data_structure/glomconversions.cc	(original)
+++ branches/glom-1-6/glom/libglom/data_structure/glomconversions.cc	Tue Apr 22 15:24:14 2008
@@ -77,7 +77,7 @@
 
 
 Glib::ustring Conversions::format_tm(const tm& tm_data, const std::locale& locale, const char* format)
-{
+{   
   //This is based on docs found here:
   //http://www.roguewave.com/support/docs/sourcepro/stdlibref/time-put.html
 
@@ -95,6 +95,7 @@
   tp.put(the_stream /* iter to beginning of stream */, the_stream, ' ' /* fill */, &tm_data, format, format + strlen(format) /* 'E' */ /* use locale's alternative format */);
 
   Glib::ustring text = the_stream.str();
+  //std::cout << "DEBUG: format_tm(): result from tp.put: " << text << std::endl;
 
   if(locale == std::locale("") /* The user's current locale */)
   {
@@ -109,6 +110,7 @@
 #endif
   }
 
+  //std::cout << "DEBUG: format_tm(): returning: " << text << std::endl;
   return text; //TODO: Use something like Glib::locale_to_utf8()?
 
   /*
@@ -597,6 +599,7 @@
 
 tm Conversions::parse_time(const Glib::ustring& text, const std::locale& locale, bool& success)
 {
+  //std::cout << "parse_time(): text=" << text << std::endl;
   //The sequence of statements here seems to be very fragile. If you move things then it stops working.
 
   //return parse_tm(text, locale, 'X' /* time */);
@@ -638,17 +641,60 @@
     success = true;
     return the_c_time;
   }
-  else
+  
+  //Fall back to strptime():
+  //It seems to be well known that time_get<> can parse much less than what time_put<> can generate:
+  // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2070.html
+  //
+  //Try various formats:
+  
+  memset(&the_c_time, 0, sizeof(the_c_time));
+  char* lastchar = strptime(text.c_str(), "%r" /* 12-hour clock time using the AM/PM notation */, &the_c_time);
+  if(lastchar)
+  {
+    success = true;
+    return the_c_time;
+  }
+  
+  memset(&the_c_time, 0, sizeof(the_c_time));
+  lastchar = strptime(text.c_str(), "%X" /* The time, using the locale's time format. */, &the_c_time);
+  if(lastchar)
+  {
+    //std::cout << "DEBUG: parse_time(): %X: text=" << text << " was parsed as: hour=" << the_c_time.tm_hour << ", min=" << the_c_time.tm_min  << ", sec=" << the_c_time.tm_sec << std::endl;
+    success = true;
+    return the_c_time;
+  }
+  
+  //Note: strptime() with "%OI" parses "01:00 PM" incorrectly as 01:00, though it claims to parse successfully.
+  
+  memset(&the_c_time, 0, sizeof(the_c_time));
+  lastchar = strptime(text.c_str(), "%c" /* alternative 12-hour clock */, &the_c_time);
+  if(lastchar)
   {
-    //std::cout << "  tg.get_time() failed" << text << std::endl;
+    //std::cout << "DEBUG: parse_time(): %c: text=" << text << " was parsed as: hour=" << the_c_time.tm_hour << ", min=" << the_c_time.tm_min  << ", sec=" << the_c_time.tm_sec << std::endl;
+    success = true;
+    return the_c_time;
+  }
+  
+  //This seems to be the only one that can parse "01:00 PM":
+  memset(&the_c_time, 0, sizeof(the_c_time));
+  lastchar = strptime(text.c_str(), "%I : %M %p" /* 12 hours clock with AM/PM, without seconds. */, &the_c_time);
+  if(lastchar)
+  {
+    //std::cout << "DEBUG: parse_time(): %I : %M %p: text=" << text << " was parsed as: hour=" << the_c_time.tm_hour << ", min=" << the_c_time.tm_min  << ", sec=" << the_c_time.tm_sec << std::endl;
+    success = true;
+    return the_c_time;
+  }
+ 
+  //std::cout << "  DEBUG: strptime(%c) failed on text=" << text << std::endl;
 
-    //tm blank_time = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
-    tm blank_time ;
-    memset(&blank_time , 0, sizeof(blank_time ));
+  //Nothing worked:
+  //tm blank_time = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+  tm blank_time ;
+  memset(&blank_time , 0, sizeof(blank_time ));
 
-    success = false;
-    return blank_time;
-  }
+  success = false;
+  return blank_time;
 }
 
 

Added: branches/glom-1-6/regression_tests/Makefile.am
==============================================================================
--- (empty file)
+++ branches/glom-1-6/regression_tests/Makefile.am	Tue Apr 22 15:24:14 2008
@@ -0,0 +1,17 @@
+AM_CPPFLAGS = -I top_srcdir@/ $(GLOM_CFLAGS) $(PYTHON_INCLUDES) \
+           $(MAEMO_LAUNCHER_CFLAGS) \
+           -DGLOM_GLADEDIR=\""$(gladedir)/"\" \
+           -DLOCALEDIR=\""$(datadir)/locale"\" \
+           -DPREFIX=\""$(prefix)"\" \
+           -DSYSCONFDIR=\""$(sysconfdir)"\" \
+           -DLIBDIR=\""$(libdir)"\" \
+           -DDATADIR=\""$(datadir)"\" \
+           -DGLOM_XSLTDIR=\""$(glomxsltdir)/"\" \
+           -DGLOM_EXAMPLES_DIR=\""$(glom_examples_dir)"\" \
+           -DGLOM_ICON_DIR=\""$(glom_icon_dir)"\"
+
+noinst_PROGRAMS = test_parsing_time
+
+test_parsing_time_SOURCES = test_parsing_time.cc
+test_parsing_time_LDADD = $(GLOM_LIBS) $(top_builddir)/glom/libglom/libglom.la
+

Added: branches/glom-1-6/regression_tests/test_parsing_time.cc
==============================================================================
--- (empty file)
+++ branches/glom-1-6/regression_tests/test_parsing_time.cc	Tue Apr 22 15:24:14 2008
@@ -0,0 +1,47 @@
+#include <glom/libglom/data_structure/glomconversions.h>
+
+int main(int argc, char* argv[])
+{
+  Gnome::Gda::init("test", "1.0", argc, argv);
+
+  const Glib::ustring time_text_input = "01:00 PM";
+  std::cout << "time_text_input=" << time_text_input << std::endl;
+
+  bool success = false;
+  const Gnome::Gda::Value value = 
+    Glom::Conversions::parse_value(Glom::Field::TYPE_TIME, time_text_input, success);
+
+  if(!success)
+  {
+    std::cerr << "Failed: parse_value() failed." << std::endl;
+    return -1; //Failed.
+  }
+
+  const Gnome::Gda::Time parsed_time = value.get_time();
+  //std::cout << "debug: Parsed Time: hour=" <<  parsed_time.hour << ", minute=" <<  parsed_time.minute << ", second=" <<  parsed_time.second << std::endl;
+
+  if(parsed_time.hour != 13)
+  {
+    std::cerr << "Failed: The parsed hour was " <<  parsed_time.hour << "instead of 13" << std::endl;
+    return -1; //Failed.
+  }
+
+  if(parsed_time.minute != 0)
+  {
+    std::cerr << "Failed: The parsed minute was " <<  parsed_time.minute << "instead of 0" << std::endl;
+    return -1; //Failed.
+  }
+
+  const Glib::ustring time_text_parsed = 
+    Glom::Conversions::get_text_for_gda_value(Glom::Field::TYPE_TIME, value);
+
+
+  std::cout << "time_text_parsed=" << time_text_parsed << std::endl;
+  return 0; //Success.
+
+  //This extra check would fail if :00 seconds are added to the text:
+  //if(time_text_input == time_text_parsed)
+  //   return 0; //Success.
+  //else
+  //   return -1; //Failed.
+}



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