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