vendredi 16 août 2019

tm_wday returns a large integer outside 0-6 range

In using the tm structure in c++, i get values of tm_wday that are large integers (e.g. 4199040, much larger than the expected 0-6 range: ). Why would this happen? All the other values such as the year, month etc. are correct.

I have seen previous questions where the weekday seems to be calculated wrongly,i.e. is a different value within the 0-6 range than expected due to time zone differences etc. but I am baffled as to why i would get such a big number instead? It doesn't seem to be a memory location either (not a hex format number).

#include <stdio.h>
#include <iostream>
#include <time.h>


struct tm get_time(std::string timestamp_string = "2019.08.16D11:00:00"){

    struct tm tm;
    int hh, mm;
    int MM, DD, YY;
    float ss;
    const char * timestamp = timestamp_string.c_str();
    if (sscanf(timestamp,"%d.%d.%dD%d:%d:%f", &YY,&MM, &DD,&hh, &mm,&ss) != 6) std::cout<<"oops";

    tm.tm_year = YY - 1900;  // Years from 1900
    tm.tm_mon = MM - 1; // Months form January
    tm.tm_mday = DD;
    tm.tm_hour = hh;
    tm.tm_min = mm;
    tm.tm_sec = ss;
    tm.tm_isdst = 0;
    return tm;

}

int main(){
    struct tm tm = get_time("2019.08.16D11:00:00");
    std::cout<<"Year is: "<<tm.tm_year<<std::endl; //119 - is correct
    std::cout<<"Month since Jan is: "<<tm.tm_mon<<std::endl; //7 - is correct
    std::cout<<"Weekday is: "<<tm.tm_wday<<std::endl;//4199040- why is this so large?

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire