Hi All,

Following C++ function is expected to add 5 minutes to a certain date and time in "Broken-down time" format, and output the new date and time in the same format again:

Code:
void incrementDateAndTime()
{
    struct tm timeinfo;
    int minuteToAdd = 5;

    timeinfo.tm_year = 110;//actual year is 110 + 1900 = 2010
    timeinfo.tm_mon = 3;//actual month is 3 + 1 = 4
    timeinfo.tm_mday = 2;
    timeinfo.tm_hour = 10;
    timeinfo.tm_min = 55;

    time_t rawtime = mktime ( &timeinfo );

    rawtime += minuteToAdd*60;//add 300 seconds to Coordinated Universal Time (UTC)

    //point A

    struct tm* timeinfoNew = gmtime ( &rawtime );

    year  = timeinfoNew->tm_year  + 1900;
    month = timeinfoNew->tm_mon +  1;
    day   = timeinfoNew->tm_mday;
    hour  = timeinfoNew->tm_hour;
    minute= timeinfoNew->tm_min;

    //point B
}
When printing rawtime variable in point B, I can verify that 300 seconds have been added to it succesfully. However, after converting UTC to Broken-down time format using gmtime() Linux system function and printing the resulting values (year, month, day, hour and minute) in point B, I noticed an undefined behaviour for gmtime() function -- it sometimes converts UTC succesfully, but sometimes not.

What could make its behaviour undefined ? or Is there something else wrong in my code ?

Thanks.