From cc86994aec2ea3b7738d2d9617e16d97b87248d4 Mon Sep 17 00:00:00 2001
From: Markus Frank <markus.frank@cern.ch>
Date: Wed, 7 Sep 2016 08:58:33 +0000
Subject: [PATCH] Code cosmetics

---
 DDCore/include/DD4hep/Primitives.h |  7 ++++++
 DDCore/src/Primitives.cpp          | 36 ++++++++++++++++++++++++++++++
 DDDB/include/DDDB/DDDBReader.h     |  3 ---
 DDDB/src/DDDBAlignmentTest.cpp     |  3 +--
 DDDB/src/DDDBDerivedCondTest.cpp   |  2 +-
 DDDB/src/DDDBExecutor.cpp          |  2 +-
 DDDB/src/DDDBReader.cpp            | 20 ++---------------
 7 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/DDCore/include/DD4hep/Primitives.h b/DDCore/include/DD4hep/Primitives.h
index 0753aa293..06e472875 100644
--- a/DDCore/include/DD4hep/Primitives.h
+++ b/DDCore/include/DD4hep/Primitives.h
@@ -47,6 +47,13 @@ namespace DD4hep {
   inline unsigned int hash32(const std::string& key) {
     return hash32(key.c_str());
   }
+
+  /// Convert date into epoch time (seconds since 1970)
+  long int makeTime(int year, int month, int day,
+                    int hour=0, int minutes=0, int seconds=0);
+  
+  /// Convert date into epoch time (seconds since 1970)
+  long int makeTime(const std::string& date, const char* fmt="%d-%m-%Y %H:%M:%S");
   
   /// Specialized exception to be thrown if invalid handles are accessed
   class invalid_handle_exception : public std::runtime_error  {
diff --git a/DDCore/src/Primitives.cpp b/DDCore/src/Primitives.cpp
index 4f595606c..b50c25688 100644
--- a/DDCore/src/Primitives.cpp
+++ b/DDCore/src/Primitives.cpp
@@ -30,6 +30,42 @@ using   abi::__dynamic_cast;
 #endif
 #endif
 
+long int DD4hep::makeTime(int year, int month, int day,
+                          int hour, int minutes, int seconds)
+{
+  struct tm tm_init;
+  ::memset(&tm_init,0,sizeof(tm_init));
+  tm_init.tm_year  = year > 1900 ? year-1900 : year;
+  tm_init.tm_mon   = month;
+  tm_init.tm_mday  = day;
+  tm_init.tm_hour  = hour;
+  tm_init.tm_min   = minutes;
+  tm_init.tm_sec   = seconds;
+  tm_init.tm_isdst = -1;
+  long int ti = ::mktime(&tm_init);
+  if ( ti >= 0 ) return ti;
+  except("DD4hep","Invalid time data given for conversion to epoch: %d-%d-%d %02d:%02d:%02d",
+         year, month, day, hour, minutes, seconds);
+  return ti;
+}
+
+/// Convert date into epoch time (seconds since 1970)
+long int DD4hep::makeTime(const std::string& date, const char* fmt)  {
+  struct tm tm;
+  char* c = ::strptime(date.c_str(),fmt,&tm);
+  if ( 0 == c )   {
+    except("DD4hep",
+           "Invalid time format given for update:%s should be: %s",
+           date.c_str(), fmt);
+  }
+  long ti = ::mktime(&tm);
+  if ( ti >= 0 ) return ti;
+  except("DD4hep",
+         "Invalid time string given for conversion to epoch: %s (fmt='%s')",
+         date.c_str(), fmt);
+  return ti;
+}
+
 static const std::string __typeinfoName(const std::type_info& tinfo) {
   const char* class_name = tinfo.name();
   std::string result;
diff --git a/DDDB/include/DDDB/DDDBReader.h b/DDDB/include/DDDB/DDDBReader.h
index 0acac4be9..057802aec 100644
--- a/DDDB/include/DDDB/DDDBReader.h
+++ b/DDDB/include/DDDB/DDDBReader.h
@@ -52,9 +52,6 @@ namespace DD4hep {
       void setMatch(const std::string& dir)      { m_match = dir;   }
       /// Access data match
       const std::string& match() const           { return m_match;  }
-      /// Create time from ingredients
-      static long long int makeTime(int year, int month, int day, int hour=0, int minutes=0, int seconds=0); 
-      
       /// Access to local context
       virtual UserContext* context()    {  return &m_context;  }
       /// Resolve a given URI to a string containing the data
diff --git a/DDDB/src/DDDBAlignmentTest.cpp b/DDDB/src/DDDBAlignmentTest.cpp
index c02531ed1..598d48f0f 100644
--- a/DDDB/src/DDDBAlignmentTest.cpp
+++ b/DDDB/src/DDDBAlignmentTest.cpp
@@ -28,7 +28,6 @@
 #include "DDCond/ConditionsPool.h"
 #include "DDAlign/AlignmentsManager.h"
 
-#include "DDDB/DDDBReader.h"
 #include "DDDB/DDDBConversion.h"
 #include "DDDB/DDDBAlignmentUpdateCall.h"
 
@@ -172,7 +171,7 @@ namespace  {
   };
   //========================================================================
   long make_time(int argc, char** argv) {
-    long int time = DDDB::DDDBReader::makeTime(2016,4,1,12);
+    long int time = makeTime(2016,4,1,12);
     if ( argc>0 )  {
       struct tm tm;
       char* c = ::strptime(argv[0],"%d-%m-%Y %H:%M:%S",&tm);
diff --git a/DDDB/src/DDDBDerivedCondTest.cpp b/DDDB/src/DDDBDerivedCondTest.cpp
index 13c518ad9..3871c3d97 100644
--- a/DDDB/src/DDDBDerivedCondTest.cpp
+++ b/DDDB/src/DDDBDerivedCondTest.cpp
@@ -295,7 +295,7 @@ namespace  {
 
   /// Plugin function
   long dddb_derived_alignments(LCDD& lcdd, int argc, char** argv) {
-    long int long init_time = argc>0 ? *(long*)argv[0] : DDDB::DDDBReader::makeTime(2016,4,1,12);
+    long int long init_time = argc>0 ? *(long*)argv[0] : makeTime(2016,4,1,12);
     ConditionsManager manager = ConditionsManager::from(lcdd);
     ConditionsSelector selector(manager);
     int ret = selector.collectDependencies(lcdd.world(), 0);
diff --git a/DDDB/src/DDDBExecutor.cpp b/DDDB/src/DDDBExecutor.cpp
index ce956d869..5663f001e 100644
--- a/DDDB/src/DDDBExecutor.cpp
+++ b/DDDB/src/DDDBExecutor.cpp
@@ -161,7 +161,7 @@ static long load_xml_dddb(Geometry::LCDD& lcdd, int argc, char** argv) {
 
     /// Process XML
     if ( !sys_id.empty() )   {
-      long long int init_time = DDDBReader::makeTime(2016,4,1,12);
+      long long int init_time = makeTime(2016,4,1,12);
       const void* args[] = {0, sys_id.c_str(), "/", &init_time, 0};
       printout(INFO,"DDDBExecutor","+++ Processing DDDB: %s", sys_id.c_str());
       result = lcdd.apply("DDDB_Loader", 4, (char**)args);
diff --git a/DDDB/src/DDDBReader.cpp b/DDDB/src/DDDBReader.cpp
index 58077de48..1c2a85778 100644
--- a/DDDB/src/DDDBReader.cpp
+++ b/DDDB/src/DDDBReader.cpp
@@ -21,9 +21,9 @@
 // Framework includes
 #include "DDDB/DDDBReader.h"
 #include "DD4hep/Printout.h"
+#include "DD4hep/Primitives.h"
 
 // C/C++ include files
-#include <ctime>
 #include <cstring>
 
 using namespace std;
@@ -39,22 +39,6 @@ DDDBReader::DDDBReader(const std::string& dir)
 	m_context.event_time  = makeTime(2015,7,1,12,0,0);
 }
 
-
-long long int DDDBReader::makeTime(int year, int month, int day,
-                                   int hour, int minutes, int seconds)
-{
-  struct tm tm_init;
-  ::memset(&tm_init,0,sizeof(tm_init));
-  tm_init.tm_year  = year > 1900 ? year-1900 : year;
-  tm_init.tm_mon   = month;
-  tm_init.tm_mday  = day;
-  tm_init.tm_hour  = hour;
-  tm_init.tm_min   = minutes;
-  tm_init.tm_sec   = seconds;
-  tm_init.tm_isdst = -1;
-  return ::mktime(&tm_init);
-}
-
 /// Resolve a given URI to a string containing the data
 bool DDDBReader::load(const string& system_id, string& buffer)   {
   return XML::UriReader::load(system_id, buffer);
@@ -92,7 +76,7 @@ void DDDBReader::parserLoaded(const std::string& system_id)  {
 }
 
 /// Inform reader about a locally (e.g. by XercesC) handled source load
-void DDDBReader::parserLoaded(const std::string& system_id, UserContext* ctxt)  {
+void DDDBReader::parserLoaded(const std::string& /* system_id */, UserContext* ctxt)  {
   DDDBReaderContext* context = (DDDBReaderContext*)ctxt;
   context->valid_since = context->event_time;
   context->valid_until = context->event_time;
-- 
GitLab