Skip to content

Commit 5ce4e16

Browse files
authored
Add fromDbStringLocal() (#81)
1 parent 82a6106 commit 5ce4e16

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

trantor/utils/Date.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414

1515
#include "Date.h"
16+
#include "Funcs.h"
1617
#ifndef _WIN32
1718
#include <sys/time.h>
1819
#endif
@@ -273,6 +274,36 @@ std::string Date::toDbStringLocal() const
273274
}
274275
return buf;
275276
}
277+
Date Date::fromDbStringLocal(const std::string &datetime)
278+
{
279+
unsigned int year = {0}, month = {0}, day = {0}, hour = {0}, minute = {0},
280+
second = {0}, microSecond = {0};
281+
std::vector<std::string> &&v = splitString(datetime, " ");
282+
if (2 == v.size())
283+
{
284+
// date
285+
std::vector<std::string> date = splitString(v[0], "-");
286+
if (3 == date.size())
287+
{
288+
year = std::stol(date[0]);
289+
month = std::stol(date[1]);
290+
day = std::stol(date[2]);
291+
std::vector<std::string> time = splitString(v[1], ":");
292+
if (2 < time.size())
293+
{
294+
hour = std::stol(time[0]);
295+
minute = std::stol(time[1]);
296+
second = std::stol(time[2]);
297+
if (3 < time.size())
298+
{
299+
microSecond = std::stol(time[3]);
300+
}
301+
}
302+
}
303+
}
304+
return std::move(
305+
trantor::Date(year, month, day, hour, minute, second, microSecond));
306+
}
276307
std::string Date::toCustomedFormattedStringLocal(const std::string &fmtStr,
277308
bool showMicroseconds) const
278309
{

trantor/utils/Date.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ class Date
129129
*/
130130
std::string toDbStringLocal() const;
131131

132+
// From DB string to trantor local time zone
133+
/*
134+
* Inverse of toDbStringLocal()
135+
*/
136+
static Date fromDbStringLocal(const std::string &datetime);
137+
132138
void toCustomedFormattedString(const std::string &fmtStr,
133139
char *str,
134140
size_t len) const; // UTC

trantor/utils/Funcs.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,35 @@
1414

1515
#pragma once
1616
#include <algorithm>
17+
#include <vector>
1718
namespace trantor
1819
{
1920
inline uint64_t hton64(uint64_t n)
2021
{
2122
static const int one = 1;
22-
static const char sig = *(char*)&one;
23+
static const char sig = *(char *)&one;
2324
if (sig == 0)
2425
return n; // for big endian machine just return the input
25-
char* ptr = reinterpret_cast<char*>(&n);
26+
char *ptr = reinterpret_cast<char *>(&n);
2627
std::reverse(ptr, ptr + sizeof(uint64_t));
2728
return n;
2829
}
2930
inline uint64_t ntoh64(uint64_t n)
3031
{
3132
return hton64(n);
3233
}
34+
inline std::vector<std::string> splitString(const std::string &s,
35+
const std::string &delimiter)
36+
{
37+
size_t last = 0;
38+
size_t next = 0;
39+
std::vector<std::string> v;
40+
while ((next = s.find(delimiter, last)) != std::string::npos)
41+
{
42+
v.push_back(s.substr(last, next - last));
43+
last = next + 1;
44+
}
45+
v.push_back(s.substr(last));
46+
return std::move(v);
47+
}
3348
} // namespace trantor

0 commit comments

Comments
 (0)