diff --git a/DDCore/include/DD4hep/Primitives.h b/DDCore/include/DD4hep/Primitives.h index 0753aa293111ad7011efa87e8b1eb7425cd512f1..06e472875c0d3b02517c75a4c20c2caa57b77f77 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 4f595606c5a2542075717b093a2f49b0b10410cf..b50c256882ab34f82fe14fbf8165988cde5c42b2 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 0acac4be9d36f21ccf71fc6d9d185ae8df1d743c..057802aec05072762c377dc655c354d25ce07890 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 c02531ed124095765e18fe3734018d0d35d64ee9..598d48f0f59be0260e83c8a00c77912f6642a335 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 13c518ad91bb2915ad3f4d3c131afecd48620be2..3871c3d97529d9d6805ddcbc1051286c9abc342b 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 ce956d869ee2194361f42aadd532a48ee6b29798..5663f001eff167f9caa235f2077395dbe1ad9ba7 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 58077de482bb9edde86646cb73a7bfef8616a6ce..1c2a8577839874fc0d4757205a1684ec8f48cf7d 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;