code updates

This commit is contained in:
2021-10-11 17:26:20 +02:00
parent 450eb4b74f
commit cc639ee891
8 changed files with 141 additions and 106 deletions

View File

@@ -1,8 +1,7 @@
#include "ml_date.h"
long now() {
// get-universal-time
long now() { // get-universal-time
time_t t = std::time(0);
long int now = static_cast<long int>(t);
@@ -15,38 +14,36 @@ std::string date_to_string(const long datetime, const std::string format) {
time_t timestamp = datetime;
char mbstr[128];
if (std::strftime(mbstr, sizeof(mbstr), format.c_str(), std::localtime(&timestamp))) {
if (std::strftime(mbstr, sizeof(mbstr), format.c_str(), std::gmtime(&timestamp))) {
std::string result = {mbstr};
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");
}
time_t time_to_epoch ( const struct tm *ltm, int utcdiff ) {
// const int mon_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int mon_days [] = {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+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};
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+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};
long tyears, tdays, leaps, utc_hrs;
int i;
long tyears, tdays, leaps, utc_hrs;
int i;
tyears = ltm->tm_year - 70 ; // tm->tm_year is from 1900.
leaps = (tyears + 2) / 4; // no of next two lines until year 2100.
//i = (ltm->tm_year 100) / 100;
//leaps -= ( (i/4)*3 + i%4 );
tyears = ltm->tm_year - 70 ; // tm->tm_year is from 1900.
leaps = (tyears + 2) / 4;
// no of next two lines until year 2100.
//i = (ltm->tm_year 100) / 100;
//leaps -= ( (i/4)*3 + i%4 );
// tdays = 0;
// for (i=0; i < ltm->tm_mon; i++) tdays += mon_days[i];
tdays = mon_days[ltm->tm_mon-1];
tdays = mon_days[ltm->tm_mon];
tdays += ltm->tm_mday-1; // days of month passed.
tdays = tdays + (tyears * 365) + leaps;
tdays += ltm->tm_mday-1; // days of month passed.
tdays = tdays + (tyears * 365) + leaps;
utc_hrs = ltm->tm_hour + utcdiff/3600; // for your time zone.
return (tdays * 86400) + (utc_hrs * 3600) + (ltm->tm_min * 60) + ltm->tm_sec;
utc_hrs = ltm->tm_hour + utcdiff/3600; // for your time zone.
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) {
@@ -55,23 +52,25 @@ long string_to_date(const std::string &datestr, const std::string &format) {
struct tm tm;
memset(&tm, 0, sizeof(tm));
if (strptime(datestr.c_str(), format.c_str(), &tm)) {
time_t curTime;
struct tm * timeinfo;
time(&curTime );
timeinfo = localtime(&curTime);
timeinfo->tm_year = tm.tm_year;
timeinfo->tm_mon = tm.tm_mon;
timeinfo->tm_mday = tm.tm_mday;
timeinfo->tm_hour = tm.tm_hour;
timeinfo->tm_min = tm.tm_min;
timeinfo->tm_sec = tm.tm_sec;
return time_to_epoch(timeinfo, -1 * timeinfo->tm_gmtoff);
time_t curTime;
struct tm * timeinfo;
time(&curTime );
timeinfo = localtime(&curTime);
timeinfo->tm_year = tm.tm_year;
timeinfo->tm_mon = tm.tm_mon;
timeinfo->tm_mday = tm.tm_mday;
timeinfo->tm_hour = tm.tm_hour;
timeinfo->tm_min = tm.tm_min;
timeinfo->tm_sec = tm.tm_sec;
// int offset = -1 * timeinfo->tm_gmtoff; // for local time
int offset = 0;
return time_to_epoch(timeinfo, offset);
}
throw std::runtime_error("string_to_date, invalid date string or format");
throw std::runtime_error("invalid date string or format (" + datestr + ", " + format + ")" );
}
@@ -98,5 +97,5 @@ long add_to_date(const long datetime, const long quantity, const std::string &pa
// TODO exception here
}
return mktime(tm);
return mktime(tm); // mktime is quite slow!
}