From 7ba6f3bef3782cca0a92bccee551d30d9c6832f4 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Tue, 31 Aug 2021 19:01:03 +0200 Subject: [PATCH] fix in date functions --- debug.lsp | 4 ++++ ml_date.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/debug.lsp b/debug.lsp index f546a30..350c841 100644 --- a/debug.lsp +++ b/debug.lsp @@ -1,3 +1,7 @@ +(define time (get-universal-time)) +(str-to-date (date-to-str time "%Y-%m-%d %H:%M:%S") "%Y-%m-%d %H:%M:%S") + + (define create_tbl_sql "create table prices (datetime integer, symbol varchar(8), prev_close float, open float, price float, change float, change_prct varchar(16))") (define insert_sql "insert into prices (datetime, symbol, prev_close, open, price, change, change_prct) values (1626979443, 'MPC', 54.08, 53.82, 53.63, -0.832101, '-0.83 %')") (define select_sql "select to_string(datetime, '%d.%m.%Y %H:%M:%S'), symbol, prev_close, open, price, change, change_prct from prices") diff --git a/ml_date.cpp b/ml_date.cpp index 834afd3..20246a6 100644 --- a/ml_date.cpp +++ b/ml_date.cpp @@ -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(t); @@ -23,16 +22,56 @@ std::string date_to_string(const long datetime, const std::string format) { return "invalid argument"; } -std::istringstream string_to_date_in_ss; +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}; + + 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 ); + +// tdays = 0; +// for (i=0; i < ltm->tm_mon; i++) tdays += mon_days[i]; + tdays = mon_days[ltm->tm_mon-1]; + + 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; +} + long string_to_date(const std::string &datestr, const std::string &format) { // format for example "%d.%m.%Y"; - string_to_date_in_ss.clear(); - string_to_date_in_ss.str(datestr); + 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); + } - date::sys_seconds tp; - date::from_stream(string_to_date_in_ss, format.c_str(), tp); - return tp.time_since_epoch().count(); + // throw Exception("invalid date string or format"); + return -1; } @@ -59,5 +98,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! }