Time (struct tm) Stuck At Specific Date

I have created a class, in this class, there is a function that "tests" time by checking if the time value was actually incremented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <ctime>
class timeTest
{
public:
    timeTest()
    :
    tt(std::time(nullptr)),
    lt(0),
    str(std::localtime(&tt))
    {
    }

    ~timeTest(){} // tm has not newed anything, nothing to clean up.

    std::tm* incHourTest()
    {
        while (lt != tt)
        {
            str->tm_hour++;
            lt = tt;
            tt = std::mktime(str);
            str = std::localtime(&tt);
        }
        return this->str;
    }
private:
    std::time_t tt, lt; // tt is the current value, lt is the previous value.
    std::tm* str; // structure ptr
};


1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    timeTest object;
    std::tm* ptr = object.incHourTest();
    std::cout << ptr->tm_mday << "." << ptr->tm_mon + 1 << "." << ptr->tm_year + 1900 <<
    " : " << ptr->tm_hour << ":" << ptr->tm_min << ":" << ptr->tm_sec << std::endl;
    return 0;
}


When running this code, the time gets stuck on 31.3.2013 : 1:mm:ss.
The minutes and seconds do not matter. The thing is that I can not increment the time by 1 hour, trying to do so by 60 minutes or 3600 seconds also doesn't work.

What am I doing wrong?
Last edited on
Running it on 64bit linux--seems to keep going infinitely. I wouldn't expect incHourTest() to keep looping but that's for you to consider I guess.
Last edited on
Yeah I'm running on 32-bit Windows 7 using gcc with C::B.

Right now it keeps looping until it gets "stuck", where increasing the value does not actually change it. I suspect the function std::mktime(std::tm*) to contain flaws.

Anyone on 32-bit system wanna test?
Alright I figured it out, had to do with DST. Now I just inc/dec the time value directly by 3600 (seconds in an hour), works fine. This only stalls for an hour on dates like the 28th of October 2001 @ 2 AM, but that's okay, it steps over on the second iteration.
Topic archived. No new replies allowed.