Find the answer to your Linux question:
Results 1 to 2 of 2
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 ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    43

    Question regarding the behaviour of gmtime() function

    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.

  2. #2
    Just Joined!
    Join Date
    Nov 2009
    Posts
    43
    To give an example to false looking result of gmtime(), given the following year/month/day/hour/minute input:

    2010/04/02/10/00

    The result returned by gmtime() is:

    2010/04/02/07/05, where 2010/04/02/10/05 is expected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...