hobbes
a language, embedded compiler, and runtime for efficient dynamic expression evaluation, data storage and analysis
time.H
Go to the documentation of this file.
1 
2 #ifndef HOBBES_UTIL_TIME_HPP_INCLUDED
3 #define HOBBES_UTIL_TIME_HPP_INCLUDED
4 
5 #include <hobbes/util/str.H>
6 #include <string>
7 #include <string.h>
8 #include <sstream>
9 #include <ctime>
10 
11 namespace hobbes {
12 
13 /*******
14  * timespans (a number of microseconds)
15  *
16  * e.g.: 5s, 20m, 4day2h3m30s200ms456us, ...
17  *
18  * *****/
19 inline long readTimespan(std::string s) {
20  long r = 0;
21 
22  while (s.size() > 0) {
25 
26  long m = 1;
27  if (u.first == "us") { // microseconds
28  m = 1;
29  } else if (u.first == "ms") { // milliseconds
30  m = 1000;
31  } else if (u.first == "s") { // seconds
32  m = 1000000;
33  } else if (u.first == "m" || u.first == "min") { // minutes
34  m = 1000000*60;
35  } else if (u.first == "h" || u.first == "hour") { // hours
36  m = 1000000L*60*60;
37  } else if (u.first == "d" || u.first == "day") { // days
38  m = 1000000L*60*60*24;
39  }
40 
41  r += str::to<uint64_t>(p.first) * m;
42  s = u.second;
43  }
44 
45  return r;
46 }
47 
48 inline long readTimespan(const str::seq& ss) {
49  long r = 0;
50  for (auto s : ss) {
51  r += readTimespan(s);
52  }
53  return r;
54 }
55 
56 inline std::string showTimespan(long ts) {
57  static const long msus = 1000;
58  static const long sus = msus * 1000;
59  static const long mus = sus * 60;
60  static const long hus = mus * 60;
61  static const long dayus = hus * 24;
62 
63  uint64_t x = labs(ts);
64  uint64_t d = x / dayus; x -= d*dayus;
65  uint64_t h = x / hus; x -= h*hus;
66  uint64_t m = x / mus; x -= m*mus;
67  uint64_t s = x / sus; x -= s*sus;
68  uint64_t ms = x / msus; x -= ms*msus;
69  uint64_t us = x;
70 
71  std::ostringstream ss;
72  if (ts < 0) ss << "-";
73  if (d > 0) ss << d << "d";
74  if (h > 0) ss << h << "h";
75  if (m > 0) ss << m << "m";
76  if (s > 0 || (d == 0 && h == 0 && m == 0 && ms == 0 && us == 0)) {
77  ss << s << "s";
78  }
79  if (ms > 0) ss << ms << "ms";
80  if (us > 0) ss << us << "us";
81 
82  return ss.str();
83 }
84 
85 /*******
86  * times (microseconds since a base time)
87  *
88  * e.g.: 01:00:00.000000, 15:34:57.123456, ...
89  *
90  *******/
91 inline long mkTime(int h, int m, int s, int u) {
92  tm t;
93  memset(&t, 0, sizeof(t));
94 
95  t.tm_sec = s;
96  t.tm_min = m;
97  t.tm_hour = h;
98  t.tm_mday = 1;
99  t.tm_mon = 0;
100  t.tm_year = 70;
101  t.tm_isdst = 0;
102 
103  return (mktime(&t) * 1000 * 1000) + u;
104 }
105 
106 inline long readTime(const std::string& x) {
107  str::pair h_msu = str::lsplit(x, ":");
108  str::pair m_su = str::lsplit(h_msu.second, ":");
109  str::pair s_u = str::lsplit(m_su.second, ".");
110 
111  int h = h_msu.first.empty() ? 0 : str::to<int>(h_msu.first);
112  int m = m_su.first.empty() ? 0 : str::to<int>(m_su.first);
113  int s = s_u.first.empty() ? 0 : str::to<int>(s_u.first);
114  int u = s_u.second.empty() ? 0 : str::to<int>(s_u.second);
115 
116  return mkTime(h,m,s,u);
117 }
118 
119 inline std::string showTime(long x) {
120  int64_t s = x / (1000 * 1000);
121  int64_t us = x % (1000 * 1000);
122 
123  static char buf[256];
124  strftime(buf, sizeof(buf), "%H:%M:%S", localtime((time_t*)&s));
125 
126  std::ostringstream uss;
127  uss << us;
128 
129  std::ostringstream ss;
130  ss << buf;
131  ss << ".";
132  ss << std::string((uss.str().size() < 6) ? (6 - uss.str().size()) : 0, '0') << uss.str();
133 
134  return ss.str();
135 }
136 
137 /*******
138  * datetimes (microseconds since epoch)
139  *
140  * e.g.: 2015-01-01T01:00:00.000000, 1980-05-19T15:34:57.123456, ...
141  *
142  *******/
143 inline long mkDateTime(int y, int mon, int d, int h, int min, int s, int u) {
144  tm t;
145  memset(&t, 0, sizeof(t));
146 
147  t.tm_sec = s;
148  t.tm_min = min;
149  t.tm_hour = h;
150  t.tm_mday = d;
151  t.tm_mon = mon - 1;
152  t.tm_year = y - 1900;
153  t.tm_isdst = 0;
154 
155  return (mktime(&t) * 1000L * 1000L) + u;
156 }
157 
158 inline long timeFromDateTime(long x) {
159  int64_t s = x / (1000L * 1000L);
160  int64_t us = x % (1000L * 1000L);
161  tm* t = localtime((time_t*)&s);
162 
163  if (t) {
164  return mkTime(t->tm_hour, t->tm_min, t->tm_sec, us);
165  } else {
166  return mkTime(0, 0, 0, 0);
167  }
168 }
169 
170 inline long dateFromDateTime(long x) {
171  int64_t s = x / (1000L * 1000L);
172  tm t;
173  localtime_r((time_t*)&s, &t);
174 
175  t.tm_sec = 0;
176  t.tm_min = 0;
177  t.tm_hour = 0;
178 
179  return mktime(&t) * 1000L * 1000L;
180 }
181 
182 inline long readDateTime(const std::string& x) {
183  str::pair y_mdThmsu = str::lsplit(x, "-");
184  str::pair m_dThmsu = str::lsplit(y_mdThmsu.second, "-");
185  str::pair d_Thmsu = str::lsplit(m_dThmsu.second, "T");
186  str::pair h_msu = str::lsplit(d_Thmsu.second, ":");
187  str::pair m_su = str::lsplit(h_msu.second, ":");
188  str::pair s_u = str::lsplit(m_su.second, ".");
189 
190  int y = y_mdThmsu.first.empty() ? 0 : str::to<int>(y_mdThmsu.first);
191  int mon = m_dThmsu.first.empty() ? 0 : str::to<int>(m_dThmsu.first);
192  int d = d_Thmsu.first.empty() ? 0 : str::to<int>(d_Thmsu.first);
193 
194  int h = h_msu.first.empty() ? 0 : str::to<int>(h_msu.first);
195  int min = m_su.first.empty() ? 0 : str::to<int>(m_su.first);
196  int s = s_u.first.empty() ? 0 : str::to<int>(s_u.first);
197  int u = s_u.second.empty() ? 0 : str::to<int>(s_u.second);
198 
199  return mkDateTime(y, mon, d, h, min, s, u);
200 }
201 
202 inline std::string showDateTime(long x) {
203  int64_t s = x / (1000L * 1000L);
204  int64_t us = x % (1000L * 1000L);
205 
206  static char buf[256];
207  strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", localtime((time_t*)&s));
208 
209  std::ostringstream uss;
210  uss << us;
211 
212  std::ostringstream ss;
213  ss << buf;
214  ss << ".";
215  ss << std::string((uss.str().size() < 6) ? (6 - uss.str().size()) : 0, '0') << uss.str();
216 
217  return ss.str();
218 }
219 
220 }
221 
222 #endif
std::string showDateTime(long x)
Definition: time.H:202
pair lsplit(const std::string &s, const std::string &ss)
Definition: str.C:215
pair readWhile(bool(*P)(char), const std::string &s)
Definition: str.C:254
std::pair< std::string, std::string > pair
Definition: str.H:220
Definition: boot.H:7
std::string showTimespan(long ts)
Definition: time.H:56
long mkDateTime(int y, int mon, int d, int h, int min, int s, int u)
Definition: time.H:143
size_t r(const reader::MetaData &md, size_t o, T *t)
Definition: storage.H:1730
std::vector< std::string > seq
Definition: str.H:19
long timeFromDateTime(long x)
Definition: time.H:158
long mkTime(int h, int m, int s, int u)
Definition: time.H:91
std::string showTime(long x)
Definition: time.H:119
long dateFromDateTime(long x)
Definition: time.H:170
long readDateTime(const std::string &x)
Definition: time.H:182
long readTime(const std::string &x)
Definition: time.H:106
LexicalAnnotation m(const YYLTYPE &p)
Definition: hexpr.parse.C:127
long readTimespan(std::string s)
Definition: time.H:19
bool isDigit(char c)
Definition: str.C:467
bool isNotDigit(char c)
Definition: str.C:471