radarlib  1.4.6
odimh5v20_format.hpp
Go to the documentation of this file.
1 /*
2  * Radar Library
3  *
4  * Copyright (C) 2009-2010 ARPA-SIM <urpsim@smr.arpa.emr.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Author: Guido Billi <guidobilli@gmail.com>
21  */
22 
27 #ifndef __RADAR_ODIMH5V20_FORMAT_HPP__
28 #define __RADAR_ODIMH5V20_FORMAT_HPP__
29 
30 /*===========================================================================*/
31 
32 #include <radarlib/time.hpp>
33 #include <radarlib/string.hpp>
36 
37 namespace OdimH5v20 {
38 
39 /*===========================================================================*/
40 /* FUNCTIONS TO FORMAT VALUES */
41 /*===========================================================================*/
42 
48 class Format
49 {
50 public:
51 
52  /*==============================================================*/
53  /* FUNZIONI DI CONVERSIONE TRA STRINGHE E VALORI */
54  /*==============================================================*/
55 
63  static std::string timeToYYYYMMDD(time_t value)
64  {
65  try
66  {
67  int year, month, day;
68  Radar::timeutils::splitYMD(value, year, month, day);
69  char buff[20];
70  snprintf(buff, sizeof(buff), "%04d%02d%02d", year, month, day);
71  return buff;
72  }
73  catch (std::exception& e)
74  {
75  std::ostringstream ss;
76  ss << "Cannot convert value '" << value << "' to a string: " << e.what();
77  throw OdimH5Exception(ss.str());
78  }
79  }
87  static std::string timeToYYYYMMDD(double value)
88  {
89  return timeToYYYYMMDD((time_t)value);
90  }
98  static std::string timeToHHMMSS(time_t value)
99  {
100  try
101  {
102  int hour, min, sec;
103  Radar::timeutils::splitHMS(value, hour, min, sec);
104  char buff[20];
105  snprintf(buff, sizeof(buff), "%02d%02d%02d", hour, min, sec);
106  return buff;
107  }
108  catch (std::exception& e)
109  {
110  std::ostringstream ss;
111  ss << "Cannot convert value '" << value << "' to a string: " << e.what();
112  throw OdimH5Exception(ss.str());
113  }
114  }
122  static std::string timeToHHMMSSsss(time_t value)
123  {
124  return timeToHHMMSSsss((double)value);
125  }
133  static std::string timeToHHMMSSsss(double value)
134  {
135  try
136  {
137  int hour, min, sec, msec;
138  Radar::timeutils::splitHMS(value, hour, min, sec, msec);
139  char buff[30];
140  snprintf(buff, sizeof(buff), "%02d%02d%02d.%03d", hour, min, sec, msec);
141  return buff;
142  }
143  catch (std::exception& e)
144  {
145  std::ostringstream ss;
146  ss << "Cannot convert value '" << value << "' to a string: " << e.what();
147  throw OdimH5Exception(ss.str());
148  }
149  }
157  static time_t YYYYMMDDToTime(const std::string& value)
158  {
159  try
160  {
161  int year, month, day;
162  if (sscanf(value.c_str(),"%04d%02d%02d", &year, &month, &day) != 3)
163  throw OdimH5FormatException("'" + value + "' is not a valid YYYYMMDD date string");
164  return Radar::timeutils::mktime(year, month, day, 0, 0, 0);
165  }
166  catch (std::invalid_argument& iae)
167  {
168  throw OdimH5FormatException("'" + value + "' is not a odimh5 date: " + iae.what());
169  }
170  }
178  static time_t HHMMSSToTime(const std::string& value)
179  {
180  try
181  {
182  int hour, min, sec;
183  if (sscanf(value.c_str(),"%02d%02d%02d", &hour, &min, &sec) != 3)
184  throw OdimH5FormatException("'" + value + "' is not a valid HHMMSS time string");
185  return Radar::timeutils::mktime(0, 0, 0, hour, min, sec);
186  }
187  catch (std::invalid_argument& iae)
188  {
189  throw OdimH5FormatException("'" + value + "' is not a odimh5 time: " + iae.what());
190  }
191  }
199  static double HHMMSSsssToTime(const std::string& value)
200  {
201  double dvalue;
202  if (sscanf(value.c_str(), "%lf", &dvalue) != 1)
203  throw OdimH5FormatException("'" + value + "' is not a odimh5 time (double value)");
204  long integer = (long)(dvalue * 1000);
205  integer = integer % 240000000; // per non saper ne leggere ne scrivere eliminiamo qualunque tempo oltre le 24 ore
206  long msec = integer % 1000; integer /= 1000;
207  long sec = integer % 100;
208  long min = (integer / 100) % 100;
209  long hour = integer / 10000;
210  return (double)(hour * 3600 + min * 60 + sec) + ((double)msec / 1000);
211  }
212 
213 
214 };
215 
216 /*===========================================================================*/
217 
218 }
219 
220 #endif
221 
222 
223 
224 
225 
226 
227 
228 
229 
230 
231 
232 
233 
234 
235 
236 
237 
238 
239 
240 
241 
242 
243 
244 
245 
246 
247 
248 
249 
250 
251 
252 
253 
254 
255 
256 
257 
258 
259 
260 
261 
262 
263 
264 
time_t mktime(int year, int month, int day)
Create a time from a date.
Definition: time.cpp:110
static std::string timeToYYYYMMDD(time_t value)
Convert a time_t value to string.
Definition: odimh5v20_format.hpp:63
static std::string timeToHHMMSSsss(time_t value)
Convert a time to a string.
Definition: odimh5v20_format.hpp:122
Functions about times and dates.
static std::string timeToYYYYMMDD(double value)
Convert a double value representing a time to string.
Definition: odimh5v20_format.hpp:87
HDF5File class.
Definition: odimh5v20_format.hpp:48
static std::string timeToHHMMSS(time_t value)
Convert a time to a string.
Definition: odimh5v20_format.hpp:98
void splitHMS(time_t absolute, int &hour, int &min, int &sec)
Extract day time informations from a time_t value.
Definition: time.cpp:197
Functions for string manipulation and conversion.
Interface classes between OdimH5 objects and HDF5 library.
static std::string timeToHHMMSSsss(double value)
Convert a time to a string.
Definition: odimh5v20_format.hpp:133
static time_t YYYYMMDDToTime(const std::string &value)
Convert a string to a time_t value.
Definition: odimh5v20_format.hpp:157
Constants and values used by OdimH5 library.
void splitYMD(time_t absolute, int &year, int &month, int &day)
Extract date informations from a time_t value.
Definition: time.cpp:178
static time_t HHMMSSToTime(const std::string &value)
Convert a string to a time_t value.
Definition: odimh5v20_format.hpp:178
OdimH5 generic error.
Definition: odimh5v20_exceptions.hpp:52
static double HHMMSSsssToTime(const std::string &value)
Convert a string to a time_t value.
Definition: odimh5v20_format.hpp:199
OdimH5 format error.
Definition: odimh5v20_exceptions.hpp:103