From 30a20e3268c9b30cb94943ef5738d6fee5a95219 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Thu, 14 Jun 2012 22:15:31 +0200
Subject: [PATCH] sqlite3 is very picky about date formats

Make sure, our dates have the correct format:

  2012-05-21 works, but 2012-5-12 does not.

Haven't I already fixed this one a few months ago?
---
 modules/conop/src/compound.hh     | 10 +++++-----
 modules/conop/src/compound_lib.cc | 20 ++++++++++++++------
 2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/modules/conop/src/compound.hh b/modules/conop/src/compound.hh
index 83862af3b..45466a1cc 100644
--- a/modules/conop/src/compound.hh
+++ b/modules/conop/src/compound.hh
@@ -52,11 +52,11 @@ struct Date {
   
   static Date FromString(const StringRef& str)
   {
-    assert(str[4]=='-');
-    assert(str[7]=='-');
-    std::pair<bool, int> year=str.substr(0,4).to_int();
-    std::pair<bool, int> month=str.substr(5,2).to_int();
-    std::pair<bool, int> day=str.substr(8, 2).to_int();
+    std::vector<StringRef> parts=str.split('-');
+    assert(parts.size()==3);
+    std::pair<bool, int> year=parts[0].to_int();
+    std::pair<bool, int> month=parts[1].to_int();
+    std::pair<bool, int> day=parts[2].to_int();
     assert(year.first); assert(month.first); assert(day.first);
     return Date(year.second, month.second, day.second);
   }
diff --git a/modules/conop/src/compound_lib.cc b/modules/conop/src/compound_lib.cc
index c423e0a12..020c3468c 100644
--- a/modules/conop/src/compound_lib.cc
+++ b/modules/conop/src/compound_lib.cc
@@ -117,14 +117,22 @@ void CompoundLib::AddCompound(const CompoundPtr& compound)
     sqlite3_bind_text(stmt, 6, compound->GetFormula().c_str(),
                       compound->GetFormula().length(), NULL);
     std::stringstream ss;
-    ss << compound->GetCreationDate().year << "-" 
-       << compound->GetCreationDate().month << "-" 
-       << compound->GetCreationDate().day;
+    ss << compound->GetCreationDate().year << "-";
+    ss.fill('0');
+    ss.width(2);
+    ss << compound->GetCreationDate().month << "-";
+    ss.fill('0');
+    ss.width(2);
+    ss << compound->GetCreationDate().day;
     String date=ss.str();
     ss.str("");
-    ss << compound->GetModificationDate().year << "-" 
-       << compound->GetModificationDate().month << "-" 
-       << compound->GetModificationDate().day;
+    ss << compound->GetModificationDate().year << "-";
+    ss.fill('0');
+    ss.width(2);
+    ss << compound->GetModificationDate().month << "-";
+    ss.fill('0');
+    ss.width(2);
+    ss << compound->GetModificationDate().day;
     sqlite3_bind_text(stmt, 7, date.c_str(), date.length(), NULL);
     date=ss.str();
     sqlite3_bind_text(stmt, 8, date.c_str(), date.length(), NULL);
-- 
GitLab