0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
TimeInline.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable 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
17  * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
25 #ifndef HYPERTABLE_TIMEINLINE_H
26 #define HYPERTABLE_TIMEINLINE_H
27 
28 #include <Common/System.h>
29 #include <Common/Time.h>
30 
31 #include <stdexcept>
32 
33 namespace Hypertable {
34 
39 #ifndef HT_DELIM_CHECK
40 # define HT_DELIM_CHECK(_l_, _c_) do { \
41  if ((_l_) != (_c_)) \
42  throw std::runtime_error("expected " #_l_ " to be " #_c_); \
43  } while (0)
44 #endif
45 
46 #ifndef HT_RANGE_CHECK
47 # define HT_RANGE_CHECK(_v_, _min_, _max_) do { \
48  if ((_v_) < (_min_) || (_v_) > (_max_)) \
49  throw std::range_error(#_v_ " must be between " #_min_ " and " #_max_); \
50  } while (0)
51 #endif
52 
63 inline int64_t
64 parse_ts(const char *ts) {
65  const int64_t G = 1000000000LL;
66  tm tv;
67  char *last;
68  int64_t ns=0;
69 
71  tv.tm_year = strtol(ts, &last, 10) - 1900;
72  HT_DELIM_CHECK(*last, '-');
73  tv.tm_mon = strtol(last + 1, &last, 10) - 1;
74  HT_RANGE_CHECK(tv.tm_mon, 0, 11);
75  HT_DELIM_CHECK(*last, '-');
76  tv.tm_mday = strtol(last + 1, &last, 10);
77  HT_RANGE_CHECK(tv.tm_mday, 1, 31);
78 
79  if (*last == 0)
80  return mktime(&tv) * G;
81 
82  HT_DELIM_CHECK(*last, ' ');
83  tv.tm_hour = strtol(last + 1, &last, 10);
84  HT_RANGE_CHECK(tv.tm_hour, 0, 23);
85  HT_DELIM_CHECK(*last, ':');
86  tv.tm_min = strtol(last + 1, &last, 10);
87  HT_RANGE_CHECK(tv.tm_min, 0, 59);
88 
89  if (*last == 0)
90  return mktime(&tv) * G;
91 
92  HT_DELIM_CHECK(*last, ':');
93 
94  tv.tm_sec = strtol(last + 1, &last, 10);
95  HT_RANGE_CHECK(tv.tm_sec, 0, 59);
96 
97  // nanoseconds
98  if (*last == ':')
99  ns = strtol(last+1, &last, 10);
100  else if (*last == '.')
101  ns = (int64_t)(strtod(last, 0) * 1000000000.0);
102 
103  return (int64_t)(mktime(&tv) * G + ns);
104 }
105 
108 } // namespace Hypertable
109 
110 #endif /* HYPERTABLE_TIMEINLINE_H */
Retrieves system information (hardware, installation directory, etc)
#define HT_DELIM_CHECK(_l_, _c_)
Definition: TimeInline.h:40
static void initialize_tm(struct tm *tmval)
Initialize struct tm.
Definition: System.cc:94
const uint64_t G
Definition: Properties.h:158
Time related declarations.
int64_t parse_ts(const char *ts)
Inline function which parses a string with a timestamp in localtime and returns a 64bit nanosecond ti...
Definition: TimeInline.h:64
Hypertable definitions
#define HT_RANGE_CHECK(_v_, _min_, _max_)
Definition: TimeInline.h:47