CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question regarding the behaviour of gmtime() Linux system 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
    Join Date
    Jun 2009
    Posts
    118

    Re: regarding the behaviour of gmtime() Linux system function

    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: regarding the behaviour of gmtime() Linux system function

    The argument of mktime is a record representing the local time (according to your set timezone). gmtime on the other hand converts to UTC timezone. So according to your example result, your system's timezone is 3 hours off UTC.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  4. #4
    Join Date
    Jun 2009
    Posts
    118

    Re: regarding the behaviour of gmtime() Linux system function

    Quote Originally Posted by treuss View Post
    The argument of mktime is a record representing the local time (according to your set timezone). gmtime on the other hand converts to UTC timezone. So according to your example result, your system's timezone is 3 hours off UTC.
    My question was something else.

    Can you imagine any factor which may cause gmtime() or localtime() functions to behave differently for the same input at different times ?

    That is, I sometimes see "2010/04/02/07/05" and sometimes "2010/04/02/10/05" as output for the same input, i.e. 2010/04/02/10/00. I noticed this behaviour for both gmtime() and localtime() functions.

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: regarding the behaviour of gmtime() Linux system function

    I can only imagine that mktime runs differently because of different environment settings. E.g. if you run as user1 you might have TZ variable set, if you run as user2 you might have not and thus /etc/timezone is used.

    But probably you are running as the same user, so that's not the case.

    I recommend checking for wormholes outside your window.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    Join Date
    Jun 2009
    Posts
    118

    Re: regarding the behaviour of gmtime() Linux system function

    Quote Originally Posted by treuss View Post
    I can only imagine that mktime runs differently because of different environment settings. E.g. if you run as user1 you might have TZ variable set, if you run as user2 you might have not and thus /etc/timezone is used.

    But probably you are running as the same user, so that's not the case.

    I recommend checking for wormholes outside your window.
    This problem seems to be weird.

    At one execution of my program, time increment works fine, but on another run of my program at a later time, it does not. (I run it with the same user)

    After calling localtime() function, I check the tm_zone field (set by localtime()) of its return value, i.e., struct tm*, and observe it to be EET, which seems to be correct.

    What do you mean by wormhole and how can I check for them ?
    Last edited by aryan1; May 17th, 2010 at 03:27 AM.

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: regarding the behaviour of gmtime() Linux system function

    I think that was a joke. A wormhole in SciFi is a rift in the space-time continuum.

  8. #8
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: regarding the behaviour of gmtime() Linux system function

    Quote Originally Posted by aryan1 View Post
    This problem seems to be weird.

    At one execution of my program, time increment works fine, but on another run of my program at a later time, it does not. (I run it with the same user)
    I'm not 100% sure if this is the problem, but you are not setting timeinfo.tm_isdst, which means the value of the field is undefined when calling mktime. Try explicitly setting timeinfo.tm_isdst to -1 (information not available) to have the system try to figure out itself whether DST is in effect or not.
    Quote Originally Posted by aryan1 View Post
    What do you mean by wormhole and how can I check for them ?
    They look something like this.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured