some small refactoring
This commit is contained in:
parent
5c925f2608
commit
8ea91f255b
75
ml_date.cpp
75
ml_date.cpp
|
|
@ -2,8 +2,8 @@
|
||||||
#include "ml_date.h"
|
#include "ml_date.h"
|
||||||
|
|
||||||
long now() { // get-universal-time
|
long now() { // get-universal-time
|
||||||
time_t t = std::time(0);
|
time_t t = std::time(nullptr);
|
||||||
long int now = static_cast<long int>(t);
|
auto now = static_cast<long int>(t);
|
||||||
|
|
||||||
return now;
|
return now;
|
||||||
}
|
}
|
||||||
|
|
@ -16,7 +16,7 @@ long get_gmt_localtime_offset() {
|
||||||
return offset;
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string date_to_string(const long datetime, const std::string format) {
|
std::string date_to_string(const long datetime, const std::string& format) {
|
||||||
// std::locale::global(std::locale("en-US.UTF8"));
|
// std::locale::global(std::locale("en-US.UTF8"));
|
||||||
|
|
||||||
time_t timestamp = datetime;
|
time_t timestamp = datetime;
|
||||||
|
|
@ -27,60 +27,59 @@ std::string date_to_string(const long datetime, const std::string format) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("date_to_string invalid date string or format");
|
throw std::runtime_error("date_to_string, invalid date string or format (" + std::to_string(datetime) + ", " + format + ")" );
|
||||||
}
|
}
|
||||||
|
|
||||||
time_t time_to_epoch ( const struct tm *ltm, int utcdiff ) {
|
time_t time_to_epoch ( const struct tm *ltm, int utcdiff ) {
|
||||||
const int mon_days [] = {0, 31, 31+28, 31+28+31,
|
const int mon_days [] = {0, 31, 31+28, 31+28+31,
|
||||||
31+28+31+30, 31+28+31+30+31, 31+28+31+30+31+30,
|
31+28+31+30, 31+28+31+30+31, 31+28+31+30+31+30,
|
||||||
31+28+31+30+31+30+31, 31+28+31+30+31+30+31+31, 31+28+31+30+31+30+31+31+30,
|
31+28+31+30+31+30+31, 31+28+31+30+31+30+31+31, 31+28+31+30+31+30+31+31+30,
|
||||||
31+28+31+30+31+30+31+31+30+31, 31+28+31+30+31+30+31+31+30+31+30, 31+28+31+30+31+30+31+31+30+31+30+31};
|
31+28+31+30+31+30+31+31+30+31, 31+28+31+30+31+30+31+31+30+31+30, 31+28+31+30+31+30+31+31+30+31+30+31};
|
||||||
|
|
||||||
long tyears, tdays, leaps, utc_hrs;
|
long tyears, tdays, leaps, utc_hrs;
|
||||||
int i;
|
|
||||||
|
|
||||||
tyears = ltm->tm_year - 70 ; // tm->tm_year is from 1900.
|
tyears = ltm->tm_year - 70 ; // tm->tm_year is from 1900.
|
||||||
leaps = (tyears + 2) / 4;
|
leaps = (tyears + 2) / 4;
|
||||||
// no of next two lines until year 2100.
|
// no of next two lines until year 2100.
|
||||||
//i = (ltm->tm_year – 100) / 100;
|
//int i = (ltm->tm_year – 100) / 100;
|
||||||
//leaps -= ( (i/4)*3 + i%4 );
|
//leaps -= ( (i/4)*3 + i%4 );
|
||||||
|
|
||||||
tdays = mon_days[ltm->tm_mon];
|
tdays = mon_days[ltm->tm_mon];
|
||||||
|
|
||||||
tdays += ltm->tm_mday-1; // days of month passed.
|
tdays += ltm->tm_mday-1; // days of month passed.
|
||||||
tdays = tdays + (tyears * 365) + leaps;
|
tdays = tdays + (tyears * 365) + leaps;
|
||||||
|
|
||||||
utc_hrs = ltm->tm_hour + utcdiff/3600; // for your time zone.
|
utc_hrs = ltm->tm_hour + utcdiff/3600; // for your time zone.
|
||||||
return (tdays * 86400) + (utc_hrs * 3600) + (ltm->tm_min * 60) + ltm->tm_sec;
|
return (tdays * 86400) + (utc_hrs * 3600) + (ltm->tm_min * 60) + ltm->tm_sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
long string_to_date(const std::string &datestr, const std::string &format) {
|
long string_to_date(const std::string &datestr, const std::string &format) {
|
||||||
// format for example "%d.%m.%Y";
|
// format for example "%d.%m.%Y";
|
||||||
|
|
||||||
struct tm tm;
|
struct tm tm{};
|
||||||
memset(&tm, 0, sizeof(tm));
|
memset(&tm, 0, sizeof(tm));
|
||||||
if (strptime(datestr.c_str(), format.c_str(), &tm)) {
|
if (strptime(datestr.c_str(), format.c_str(), &tm)) {
|
||||||
time_t curTime;
|
time_t curTime;
|
||||||
struct tm * timeinfo;
|
struct tm * timeinfo;
|
||||||
|
|
||||||
time(&curTime );
|
time(&curTime );
|
||||||
timeinfo = std::gmtime(&curTime);
|
timeinfo = std::gmtime(&curTime);
|
||||||
|
|
||||||
timeinfo->tm_year = tm.tm_year;
|
timeinfo->tm_year = tm.tm_year;
|
||||||
timeinfo->tm_mon = tm.tm_mon;
|
timeinfo->tm_mon = tm.tm_mon;
|
||||||
timeinfo->tm_mday = tm.tm_mday;
|
timeinfo->tm_mday = tm.tm_mday;
|
||||||
timeinfo->tm_hour = tm.tm_hour;
|
timeinfo->tm_hour = tm.tm_hour;
|
||||||
timeinfo->tm_min = tm.tm_min;
|
timeinfo->tm_min = tm.tm_min;
|
||||||
timeinfo->tm_sec = tm.tm_sec;
|
timeinfo->tm_sec = tm.tm_sec;
|
||||||
|
|
||||||
return time_to_epoch(timeinfo, 0);
|
return time_to_epoch(timeinfo, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
throw std::runtime_error("invalid date string or format (" + datestr + ", " + format + ")" );
|
throw std::runtime_error("string_to_date, invalid date string or format (" + datestr + ", " + format + ")" );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
long add_to_date(const long datetime, const long quantity, const std::string &part) {
|
long add_to_date(const long datetime, const int quantity, const std::string &part) {
|
||||||
// part is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'
|
// part is one of 'year', 'month', 'day', 'hour', 'minute' or 'second'
|
||||||
|
|
||||||
// very basic implementation, just for now - no timezones DST etc
|
// very basic implementation, just for now - no timezones DST etc
|
||||||
|
|
@ -100,7 +99,7 @@ long add_to_date(const long datetime, const long quantity, const std::string &pa
|
||||||
} else if (part == "second") {
|
} else if (part == "second") {
|
||||||
tm->tm_sec += quantity;
|
tm->tm_sec += quantity;
|
||||||
} else {
|
} else {
|
||||||
// TODO exception here
|
throw std::runtime_error("add_to_date, part to add (" + part + ")" );
|
||||||
}
|
}
|
||||||
|
|
||||||
return mktime(tm); // mktime is quite slow!
|
return mktime(tm); // mktime is quite slow!
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,9 @@ long now();
|
||||||
|
|
||||||
long get_gmt_localtime_offset();
|
long get_gmt_localtime_offset();
|
||||||
|
|
||||||
std::string date_to_string(const long datetime, const std::string format);
|
std::string date_to_string(const long datetime, const std::string &format);
|
||||||
|
|
||||||
long string_to_date(const std::string &datestr, const std::string &format);
|
long string_to_date(const std::string &datestr, const std::string &format);
|
||||||
|
|
||||||
long add_to_date(const long datetime, const long quantity, const std::string &part);
|
long add_to_date(const long datetime, const int quantity, const std::string &part);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -55,19 +55,17 @@ bool is_path_file(const std::string &path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_path_dir(const std::string &path) {
|
bool is_path_dir(const std::string &path) {
|
||||||
struct stat buf;
|
struct stat buf{};
|
||||||
stat(path.c_str(), &buf);
|
stat(path.c_str(), &buf);
|
||||||
return (bool) S_ISDIR(buf.st_mode);
|
return (bool) S_ISDIR(buf.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
int mk_path_dir(const std::string &path) {
|
int mk_path_dir(const std::string &path) {
|
||||||
int r = ::mkdir(path.c_str(), 0755);
|
return ::mkdir(path.c_str(), 0755);
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int rm_path_dir(const std::string &path) {
|
int rm_path_dir(const std::string &path) {
|
||||||
int r = ::rmdir(path.c_str());
|
return ::rmdir(path.c_str());
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MlValue exec_system_cmd(const std::string &cmd) {
|
MlValue exec_system_cmd(const std::string &cmd) {
|
||||||
|
|
|
||||||
|
|
@ -101,10 +101,10 @@ void MlPerfMon::print_results() const {
|
||||||
|
|
||||||
int i {0};
|
int i {0};
|
||||||
for(auto &p : v) {
|
for(auto &p : v) {
|
||||||
if (p.first.size() >= method_name_print_len)
|
if (p.first.size() >= method_name_print_len)
|
||||||
p.first.erase(method_name_print_len, std::string::npos);
|
p.first.erase(method_name_print_len, std::string::npos);
|
||||||
else
|
else
|
||||||
p.first.insert(p.first.end(), method_name_print_len - p.first.size(), ' ');
|
p.first.insert(p.first.end(), method_name_print_len - p.first.size(), ' ');
|
||||||
|
|
||||||
std::cerr << p.first << " " << p.second << std::endl;
|
std::cerr << p.first << " " << p.second << std::endl;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ std::vector<std::vector<std::string>> regexp_tokens(const std::string &str, cons
|
||||||
captured_subgroups.push_back(res[i]);
|
captured_subgroups.push_back(res[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (captured_subgroups.size() > 0)
|
if (!captured_subgroups.empty())
|
||||||
captured_groups.push_back(captured_subgroups);
|
captured_groups.push_back(captured_subgroups);
|
||||||
|
|
||||||
searchStart += res.position() + res.length();
|
searchStart += res.position() + res.length();
|
||||||
|
|
@ -119,7 +119,7 @@ std::string string_trim(std::string s, const std::string &chars_to_trim, const s
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string string_padd(const std::string &str, int pad_len, char fill_char, bool from_right) {
|
std::string string_padd(const std::string &str, int pad_len, char fill_char, bool from_right) {
|
||||||
int str_len = str.length();
|
auto str_len = str.length();
|
||||||
|
|
||||||
if (str_len == pad_len)
|
if (str_len == pad_len)
|
||||||
return str;
|
return str;
|
||||||
|
|
@ -154,6 +154,6 @@ size_t string_find_substr(const std::string & str, const std::string & pattern,
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t p = str.find(pattern, pos);
|
size_t p = str.find(pattern, pos);
|
||||||
|
|
||||||
return p != str.npos ? p : -1;
|
return p != str.npos ? p : -1;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue