Commit 74e024b9 authored by Alexander Lapshin's avatar Alexander Lapshin

+ get_ts, json loading data types, refactoring

parent c309791e
This diff is collapsed.
...@@ -111,4 +111,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ ...@@ -111,4 +111,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, svalue<TimeJ
return true; return true;
} }
\ No newline at end of file static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, svalue<TimeJD>& value, const bool exist = true)
{
std::map<std::string, std::string>::const_iterator itr;
bool* loaded = nullptr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
value = StrToDate(itr->second);
return true;
}
static bool loadjson(const std::map<std::string, std::string>& parent, const std::string& key, TimeJD& value, const bool exist = true, bool* loaded = nullptr)
{
std::map<std::string, std::string>::const_iterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const std::string& str = itr->second;
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
value = StrToDate(str);
return true;
}
...@@ -81,4 +81,10 @@ public: ...@@ -81,4 +81,10 @@ public:
val = std::stoi(str, nullptr); val = std::stoi(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, long long& val)
{
val = std::stoll(str, nullptr);
return true;
}
}; };
...@@ -144,7 +144,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute, ...@@ -144,7 +144,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
std::ostringstream ss; std::ostringstream ss;
if(!year_first) { if (!year_first) {
ss << std::setfill('0'); ss << std::setfill('0');
ss << std::setw(2) << day << "/"; ss << std::setw(2) << day << "/";
ss << std::setw(2) << month << "/"; ss << std::setw(2) << month << "/";
...@@ -152,7 +152,8 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute, ...@@ -152,7 +152,8 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
ss << std::setw(2) << d << ":"; ss << std::setw(2) << d << ":";
ss << std::setw(2) << m << ":";; ss << std::setw(2) << m << ":";;
ss << std::setw(2) << s; ss << std::setw(2) << s;
}else { }
else {
ss << std::setfill('0'); ss << std::setfill('0');
ss << std::setw(4) << year << "/"; ss << std::setw(4) << year << "/";
ss << std::setw(2) << month << "/"; ss << std::setw(2) << month << "/";
...@@ -161,7 +162,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute, ...@@ -161,7 +162,7 @@ static std::string DateToStr(int year, int month, int day, int hour, int minute,
ss << std::setw(2) << m << ":";; ss << std::setw(2) << m << ":";;
ss << std::setw(2) << s; ss << std::setw(2) << s;
} }
if (prec > 0) { if (prec > 0) {
ss << "."; ss << ".";
ss << std::setw(prec) << msec; ss << std::setw(prec) << msec;
...@@ -349,7 +350,7 @@ static void JToSystemTime(double J, int& Cyear, int& Cmonth, int& Cday, int& Cho ...@@ -349,7 +350,7 @@ static void JToSystemTime(double J, int& Cyear, int& Cmonth, int& Cday, int& Cho
a = (jd + 1) % 7; a = (jd + 1) % 7;
dd = day + J - jd + 0.5; dd = day + J - jd + 0.5;
if (BC) { if (BC) {
year = -year + 1; year = -year + 1;
Cyear = year; Cyear = year;
...@@ -500,6 +501,32 @@ inline TimeJD get_current_time() ...@@ -500,6 +501,32 @@ inline TimeJD get_current_time()
const auto s_now = get_current_time("%d/%m/%Y %H:%M:%S"); const auto s_now = get_current_time("%d/%m/%Y %H:%M:%S");
StrToDate(s_now, now); StrToDate(s_now, now);
return now; return now;
} }
inline long long get_ts()
{
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() / 1000;
}
inline long long get_ts(const TimeJD& date)
{
const TimeGD gd = date;
int jd;
double jf;
convertTimeGJ(
gd.Year,
gd.Month,
gd.Day,
gd.Hour,
gd.Min,
gd.Sec,
gd.Fraction,
jd,
jf);
return (jd + jf - 2440587.5) * 86400.0 * 1000;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment