SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
Time.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #include "swcdb/core/Exception.h"
8 #include "swcdb/core/Time.h"
9 
10 #include <ratio>
11 #include <ctime>
12 
13 extern "C" {
14 #include <sys/time.h>
15 }
16 
17 
18 namespace SWC { namespace Time {
19 
21 int64_t parse_digit(int& err, const char** bufp,
22  int8_t default_v = 0) noexcept {
23  errno = 0;
24  while(**bufp && **bufp == '0') ++*bufp;
25  if(**bufp) {
26  char* last = nullptr;
27  int64_t v = strtoll(*bufp, &last, 0);
28  if(errno)
29  err = errno;
30  else
31  *bufp = last;
32  return v;
33  }
34  return default_v;
35 }
36 
37 int64_t parse_ns(int& err, const std::string& buf) {
38  /*if(!strptime(ptr, "%Y/%m/%d %H:%M:%S", &info)) {
39  err = EINVAL;
40  return ns;
41  }*/
42 
43  const char* ptr = buf.c_str();
44  int64_t ns = parse_digit(err, &ptr);
45  if(err)
46  return 0;
47  if(!*ptr)
48  return ns;
49  if(*ptr != '/') {
50  err = EINVAL;
51  return 0;
52  }
53 
54  struct tm info;
55  info.tm_yday = info.tm_wday = 0;
56  info.tm_isdst = -1;
57  info.tm_year = ns - 1900;
58  info.tm_mon = parse_digit(err, &++ptr, 1);
59  if(err)
60  return 0;
61  if(info.tm_mon < 1 || info.tm_mon > 12) {
62  err = EINVAL;
63  return 0;
64  }
65  info.tm_mon -= 1;
66  info.tm_mday = 1;
67  info.tm_hour = 0;
68  info.tm_min = 0;
69  info.tm_sec = 0;
70 
71  if(*ptr == '/') {
72  info.tm_mday = parse_digit(err, &++ptr, 1);
73  if(err)
74  return 0;
75  if(info.tm_mday < 1 || info.tm_mday > 31) {
76  err = EINVAL;
77  return 0;
78  }
79  if(*ptr == ' ') {
80  info.tm_hour = parse_digit(err, &++ptr);
81  if(err)
82  return 0;
83  if(info.tm_hour < 0 || info.tm_hour > 23) {
84  err = EINVAL;
85  return 0;
86  }
87  if(*ptr == ':') {
88  info.tm_min = parse_digit(err, &++ptr);
89  if(err)
90  return 0;
91  if(info.tm_min < 0 || info.tm_min > 59) {
92  err = EINVAL;
93  return 0;
94  }
95  if(*ptr == ':') {
96  info.tm_sec = parse_digit(err, &++ptr);
97  if(err)
98  return 0;
99  if(info.tm_sec < 0 || info.tm_sec > 59) {
100  err = EINVAL;
101  return 0;
102  }
103  if(*ptr == '.') {
104  ns = mktime(&info) * 1000000000;
105  ++ptr;
106  uint32_t res = 0;
107  for(int places = 100000000;
108  *ptr && std::isdigit(*ptr) && places >= 1;
109  places/=10, ++ptr) {
110  res += (*ptr - 48) * places;
111  }
112  if(*ptr) {
113  err = EINVAL;
114  return 0;
115  }
116  if(res) {
117  if(ns < 0) {
118  (ns -= 1000000000 - res) += 1000000000;
119  } else {
120  (ns += res + 1000000000) -= 1000000000;
121  }
122  }
123  return ns;
124  }
125  }
126  }
127  }
128  }
129 
130  return mktime(&info) * 1000000000;
131 }
132 
133 std::string fmt_ns(int64_t ns) {
134  int64_t secs = ns/1000000000;
135  time_t t_secs = secs;
136 
137  std::string nanos;
138  if(ns < 0) {
139  uint32_t n = -ns + secs * 1000000000;
140  if(n > 0) {
141  nanos = std::to_string(1000000000 - n);
142  --t_secs; // borrow a second towrads nanos
143  }
144  } else {
145  nanos = std::to_string(ns - secs * 1000000000);
146  }
147 
148  char res[20];
149  std::strftime(res, 20, "%Y/%m/%d %H:%M:%S", std::gmtime(&t_secs));
150 
151  if(nanos.size() < 9)
152  nanos.insert(nanos.cbegin(), 9-nanos.size(), '0');
153  return std::string(res) + "." + nanos;
154 }
155 
156 }}
157 
SWC_CAN_INLINE
#define SWC_CAN_INLINE
Definition: Compat.h:102
SWC::Time::parse_digit
SWC_CAN_INLINE int64_t parse_digit(int &err, const char **bufp, int8_t default_v=0) noexcept
Definition: Time.cc:21
Exception.h
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC::Time::parse_ns
int64_t parse_ns(int &err, const std::string &buf)
Definition: Time.cc:37
Time.h
SWC::Core::to_string
SWC_CAN_INLINE std::string to_string(const BitFieldInt< T, SZ > &v)
Definition: BitFieldInt.h:263
SWC::Time::fmt_ns
std::string fmt_ns(int64_t ns)
Definition: Time.cc:133