date related update

This commit is contained in:
VaclavT 2021-10-11 17:21:41 +02:00
parent ecbc3af789
commit c30432d175
2 changed files with 6 additions and 8 deletions

View File

@ -14,7 +14,7 @@ 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;
}
@ -32,13 +32,11 @@ time_t time_to_epoch ( const struct tm *ltm, int utcdiff ) {
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.
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;
// const int mon_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// for (i=0; i < ltm->tm_mon; i++) tdays += mon_days[i];
tdays = mon_days[ltm->tm_mon];
tdays += ltm->tm_mday-1; // days of month passed.
@ -67,8 +65,8 @@ long string_to_date(const std::string &datestr, const std::string &format) {
timeinfo->tm_min = tm.tm_min;
timeinfo->tm_sec = tm.tm_sec;
int offset = -1 * timeinfo->tm_gmtoff; // for local time
//int offset = 0;
// int offset = -1 * timeinfo->tm_gmtoff; // for local time
int offset = 0;
return time_to_epoch(timeinfo, offset);
}

View File

@ -100,7 +100,7 @@
(ut::define-test "result of (!= nil nil)" '(ut::assert-false (!= nil nil)))
; it is in local time
; it is not in local time
(ut::define-test "result of (str-to-date \"01.01.1970\" \"%d.%m.%Y\")" '(ut::assert-equal 0 (str-to-date "01.01.1970" "%d.%m.%Y")))
(ut::define-test "result of (date-add (str-to-date \"01.01.1970\" \"%d.%m.%Y\") 1 \"day\")" '(ut::assert-equal 86400 (date-add (str-to-date "01.01.1970" "%d.%m.%Y") 1 "day")))