ROOT_Application  2.0
C++ Core modules and GUIStock
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Utils.cpp
Go to the documentation of this file.
1 
8 #include "Utils.h"
9 
10 #include <ctime>
11 #include <cstring>
12 
13 // for test time zone
14 /*
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <time.h>
18 #include <sys/time.h>
19 */
20 
21 namespace Utils
22 {
26 // better here, just move for test
27 //std::string PATH_DATA="/home/michael/workspace_kepler/ROOT_application/data";
28 
29 // if static avoids bug valgrind with Python UpdateFromWeb ? Solve other bug (utils in lib and prog) ?
30 static std::string PATH_DATA="/home/michael/workspace_kepler/ROOT_application/data";
31 // set directory for tests, unit-tests
32 //static const std::string PATH_DATA_TEST="/home/michael/workspace_kepler/ROOT_application/data_test";
33 //static std::string PATH_DATA="/home/michael/workspace_kepler/ROOT_application/data_test";
34 
35 
36 //void SetPathData( const std::string& new_path ) {
37 void SetPathData( std::string new_path ) {
38  Utils::PATH_DATA = new_path;
39  //PATH_DATA = new_path;
40 }
41 
42 std::string GetPathData() {return Utils::PATH_DATA;}
43 
44 // inline makes sense here ? call in loop, always same array
45 double fromStreamToDouble( std::iostream& stream, char delim )
46 {
47  char buf_value[31];
48  stream.getline( buf_value, 30, delim );
49  double t = std::atof(buf_value);
50  //std::cout << "fromStreamToDouble value " << t << std::endl;
51  return t;
52 }
53 // split a string into vectors of string.
54 // @param delimiters any string
55 //
56 void splitline(const std::string& str, const std::string& delimiters , std::vector<std::string>& tokens) {
57 // Skip delimiters at beginning.
58  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
59  // Find first "non-delimiter"
60  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
61 
62  while (std::string::npos != pos || std::string::npos != lastPos)
63  {
64  // Found a token, add it to the vector.
65  tokens.push_back(str.substr(lastPos, pos - lastPos));
66  // Skip delimiters. Note the "not_of"
67  lastPos = str.find_first_not_of(delimiters, pos);
68  // Find next "non-delimiter"
69  pos = str.find_first_of(delimiters, lastPos);
70  }
71 }
72 
73 // ok, working, print correct time
75  {
76  time_t tmp_time = 0;
77  time_t time_unix = 0;
78  // original code, do not account for timezone, was working before modifs ??
79  // before modif time is working ... with modifs -2 hours... to check
80  tmp_time = time ( &time_unix );
81  // localtime takes struct tm
82  //tmp_time = localtime( &time_unix );
83 #ifdef DEBUG_UTIL
84  std::cout << "GetTime_tToday get actual date " << tmp_time << std::endl;
85 #endif
86  return tmp_time;
87  }
88 
89 
90 // version C style, no care about timezone, no error valgrind
91 // strptime return nullptr if error, return an error code
92 //test C++11 thread-safe version of some functions
93 time_t StringToTime_t ( const std::string& str_date ) {
94 
95 #ifdef DEBUG_UTIL
96  std::cout << "Entry StringToTimeT str " << str_date << std::endl;
97  std::cout << "Size String " << str_date.size() << std::endl;
98 #endif
99  std::string date_string;
100  // correct initalization of a structure C-style, solved valgrind error in previous version
101  //struct tm date_tm;
102  //memset( &date_tm, 0, sizeof(struct tm));
103  // -1 important
104  struct tm date_tm = {0, 0, 0, 0, 0, 0, 0, 0, -1 , 0, nullptr };
105 
106  // format output, C++11 put_time not working yet.
107  // Here impose hour is present, maybe add a param as well
108  char *p = nullptr;
109  // read day and hour
110  if ( str_date.size() == 19 ) {
111  p = strptime ( str_date.c_str(),"%Y-%m-%d %H:%M:%S", &date_tm );
112  // only day to read
113  } else if ( str_date.size() == 10 ) {
114  p = strptime ( str_date.c_str(),"%Y-%m-%d", &date_tm );
115  }
116  // if size not good, still nullptr, no need to ckeck
117  // return a null pointer if an error occured
118  if ( p == nullptr ) { //|| p == 0 ) {
119  std::cout << "ERROR strptime " << std::endl;
120  return Utils::ERROR_StrToTime; //Error code -105
121  }
122  return mktime( &date_tm );
123 }
124 
125 time_t StringToTime_t ( const char * str ) {
126 
127 #ifdef DEBUG_UTIL
128  std::cout << "Entry StringToTimeT with char* str:" << str << std::endl;
129  std::cout << "len of str " << strlen(str) << std::endl;
130 #endif
131 
132  size_t str_len = strlen(str);
133  // -1 important
134  struct tm date_tm = {0, 0, 0, 0, 0, 0, 0, 0, -1 , 0, nullptr };
135 
136  // format output, C++11 put_time not working yet.
137  // Here impose hour is present, maybe add a param as well
138  char *p = nullptr;
139  // read day and hour
140  if ( str_len == 19 ) {
141  p = strptime ( str,"%Y-%m-%d %H:%M:%S", &date_tm );
142  // only day to read
143  } else if ( str_len == 10 ) {
144  p = strptime ( str,"%Y-%m-%d", &date_tm );
145  }
146  // if size not good, still nullptr, no need to ckeck
147  // return a null pointer if an error occured
148  if ( p == nullptr ) {
149  std::cout << "ERROR strptime " << std::endl;
150  return Utils::ERROR_StrToTime; //Error code -105
151  }
152  //std::cout << "return mktime " << mktime( &date_tm ) << std::endl;
153  return mktime( &date_tm );
154 }
155 
156 // here seems ok, localtime takes timezone into account
157 std::string Time_tToString ( const time_t& time, const bool b_hour )
158 {
159 
160  std::string date_string;
161  char tmp_char[25];
162  //tm timeinfo;
163 
164  struct tm *date_tm;
165  // correct initalization of a structure C-style, not needed if using pointer ??
166  // or will be set correctly with localtime function ??
167  //memset( &date_tm, 0, sizeof(struct tm));
168 
169  // convert time_t to struct tm
170  // seems a problem with valgrind, solved in glibc 2.14.1-4 only 2.13-2 available on ordi
171  // localtime use info about time zone, time() no, and mktime certainly yes ??
172  date_tm = localtime ( &time );
173 
174  // format the date
175  if ( b_hour )
176  strftime(tmp_char,25,"%Y-%m-%d %H:%M:%S",date_tm);
177  else
178  strftime ( tmp_char, 20,"%Y-%m-%d", date_tm );
179 
180  date_string.assign(tmp_char);
181  return date_string;
182 }
183 
184  // get back year, month day from the time_unix, if time_unix = 0 get actual date //
185  void GetDateYMD( time_t time_unix, int &year, int &month, int &day ) {
186 #ifdef DEBUG_UTIL
187  std::cout << "GetDateYMD " << time_unix << std::endl;
188 #endif
189 
190  struct tm * timeinfo;
191  // get time_t
192  time_t tmp_time = 0;
193 
194  if ( time_unix == 0 ) {
195  //std::cout << "get actual date " << std::endl;
196  // if 0 get actual time
197  tmp_time = time ( &time_unix );
198  //std::cout << "get actual date " << tmp_time << std::endl;
199  } else {
200  //std::cout << "time_unix not null " << time_unix << std::endl;
201  tmp_time = time_unix;
202  }
203 
204  timeinfo = localtime ( &tmp_time );
205  year = timeinfo->tm_year + 1900 ;
206  month = timeinfo->tm_mon + 1;
207  day = timeinfo->tm_mday;
208 }
209 
210 // strange error because of timezone, not sure the best
211 // keep for notes later
212 
213 // to see std, C++11 with time_point, g++4.8 bug still need strptime anyway
214 // see DataCSV::ExtractCSV, with different initialization
215 // new correct H:M:S
216 time_t StringToTime_t_old ( std::string str_date ) {
217  std::string date_string;
218 
219  // correct initalization of a structure C-style
220  //struct tm date_tm;
221  //memset( &date_tm, 0, sizeof(struct tm));
222 
223  // used in DataStock::ExtractCSVLine, correct dates time like this, shift for origin
224  // Some field specif to GNU; BSD, not in the standard
225  // http://www.delorie.com/gnu/docs/glibc/libc_435.html
226  /*
227  long int tm_gmtoff
228  This field describes the time zone that was used to compute this broken-down time value, including any adjustment for daylight saving; it is the number of seconds that you must add to UTC to get local time. You can also think of this as the number of seconds east of UTC. For example, for U.S. Eastern Standard Time, the value is -5*60*60. The tm_gmtoff field is derived from BSD and is a GNU library extension; it is not visible in a strict ISO C environment.
229 
230  const char *tm_zone
231  This field is the name for the time zone that was used to compute this broken-down time value. Like tm_gmtoff, this field is a BSD and GNU extension, and is not visible in a strict ISO C environment.
232  // -1 flag..not available ‘tm::tm_gmtoff',const char *tm_zone ;
233  */
234  // compile but wrong, -1 Save Day.. important
235  //struct tm date_tm = {0, 0, 0, 0, 0, 0, 0, 0 }; //, -1 , 0, nullptr }; //, nullptr};
236  // ‘tm::tm_gmtoff',const char *tm_zone ;
237  struct tm date_tm = {0, 0, 0, 0, 0, 0, 0, 0, -1 , 0, nullptr };
238 
239  //std::cout << "Entry StringToTimeT str " << str_date << std::endl;
240  //strptime ( str_date.c_str(),"%Y-%m-%d", &date_tm );
241  strptime ( str_date.c_str(),"%Y-%m-%d %H:%M:%S", &date_tm );
242 
243  //std::cout << " year " << date_tm.tm_year << " month " << date_tm.tm_mon << " heure " << date_tm.tm_hour << std::endl;
244  //std::cout << " year " << (*date_tm).tm_mon << " heure " << (*date_tm).tm_hour << std::endl;
245 
246  // see also, for functions tools
247  //http://stackoverflow.com/questions/1217566/general-way-to-manipulate-the-times-between-timezones-in-c?rq=1
248  // or for discussion about thread-safe, std::strftime
249  // http://kjellkod.wordpress.com/2013/01/22/exploring-c11-part-2-localtime-and-time-again/
250 
251  // here works for bug, but valgrind errors(warning in compil) and time now is wrong !
252  //http://stackoverflow.com/questions/3660983/c-time-t-problem
253  // *** enter in UTC mode
254  /*
255  char* oldTZ = getenv("TZ");
256  //empty
257  std::cout << "oldTZ " << oldTZ << std::endl;
258  //putenv("TZ=UTC");
259  std::string tmp_string="TZ=UTC";
260  putenv( const_cast<char *>(tmp_string.c_str()) );
261  //_tzset();
262  tzset();
263  */
264  // ***
265 
266  // call mktime, says opposite of localtime
267  //return mktime( &date_tm );
268  time_t ret = mktime( &date_tm );
269 
270  // *** Restore previous TZ
271  /*
272  if(oldTZ == NULL)
273  {
274  putenv("TZ=");
275  }
276  else
277  {
278  char buff[255];
279  sprintf(buff,"TZ=%s",oldTZ);
280  putenv(buff);
281  }*/
282  //tmp_string="TZ=Europe/Paris";
283  //putenv("TZ=Europe/Paris");
284  /*
285  tmp_string="TZ=Europe/Paris";
286  putenv( const_cast<char *>(tmp_string.c_str()) );
287  //_tzset();
288  tzset();
289  */
290  // ***
291  return ret;
292 }
293 
294 
295 } // end namespace Utils
double fromStreamToDouble(std::iostream &stream, char delim)
Read the next character of a stream as a double.
Definition: Utils.cpp:45
static std::string PATH_DATA
to modify before compilation to set from a Config file
Definition: Utils.cpp:30
void SetPathData(std::string new_path)
forced to use this function now.
Definition: Utils.cpp:37
Group general functions to convert to/from string, split line...
time_t GetTime_tToday()
Get the time_t of today.
Definition: Utils.cpp:74
const time_t ERROR_StrToTime
Error code returned by StringToTime_t if the string cannot be parsed correctly Must be a time_t...
Definition: Utils.h:51
time_t StringToTime_t_old(std::string str_date)
Definition: Utils.cpp:216
std::string Time_tToString(const time_t &time, const bool b_hour)
Return a string representing the date All times are expressed in the localtime.
Definition: Utils.cpp:157
void GetDateYMD(time_t time_unix, int &year, int &month, int &day)
Set year, month and day given an input time.
Definition: Utils.cpp:185
time_t StringToTime_t(const std::string &str_date)
Return the time_t from a string .
Definition: Utils.cpp:93
std::string GetPathData()
Definition: Utils.cpp:42
void splitline(const std::string &str, const std::string &delimiters, std::vector< std::string > &tokens)
splitline
Definition: Utils.cpp:56