Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef tools_s2time 4 #ifndef tools_s2time 5 #define tools_s2time 5 #define tools_s2time 6 6 7 #include <ctime> 7 #include <ctime> 8 #include <string> 8 #include <string> 9 #include <cstdio> //sscanf 9 #include <cstdio> //sscanf 10 10 11 namespace tools { 11 namespace tools { 12 12 13 inline bool s2time(const std::string& a_string 13 inline bool s2time(const std::string& a_string,time_t& a_time) { 14 int yy, _mm, dd, hh, mi, ss; 14 int yy, _mm, dd, hh, mi, ss; 15 if(::sscanf(a_string.c_str(), 15 if(::sscanf(a_string.c_str(), 16 "%d-%d-%d %d:%d:%d", 16 "%d-%d-%d %d:%d:%d", 17 &yy,&_mm,&dd,&hh,&mi,&ss)!=6) {a 17 &yy,&_mm,&dd,&hh,&mi,&ss)!=6) {a_time = 0;return false;} 18 struct tm tp; 18 struct tm tp; 19 tp.tm_year = yy-1900; 19 tp.tm_year = yy-1900; 20 tp.tm_mon = _mm-1; 20 tp.tm_mon = _mm-1; 21 tp.tm_mday = dd; 21 tp.tm_mday = dd; 22 tp.tm_hour = hh; 22 tp.tm_hour = hh; 23 tp.tm_min = mi; 23 tp.tm_min = mi; 24 tp.tm_sec = ss; 24 tp.tm_sec = ss; 25 tp.tm_isdst = 0; 25 tp.tm_isdst = 0; 26 a_time = ::mktime(&tp); 26 a_time = ::mktime(&tp); 27 return true; 27 return true; 28 } 28 } 29 29 30 inline bool time2s(std::string& a_s) { 30 inline bool time2s(std::string& a_s) { 31 time_t d; 31 time_t d; 32 if(::time(&d)==(time_t)-1) {a_s.clear();retu 32 if(::time(&d)==(time_t)-1) {a_s.clear();return false;} 33 char* _cstr = ::ctime(&d); 33 char* _cstr = ::ctime(&d); 34 _cstr[24] = '\0'; 34 _cstr[24] = '\0'; 35 a_s = _cstr; 35 a_s = _cstr; 36 return true; 36 return true; 37 } 37 } 38 38 39 } 39 } 40 40 41 #endif 41 #endif