CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: time_t

  1. #1
    Join Date
    Feb 2009
    Posts
    326

    time_t

    Hi,

    I have a doubt regarding time_t

    I am initializing a time_t variable to 0.
    When I display it, it shows "Thu Jan 1 07:30:00 1970" ?
    I expected it to Thu Jan 1 00:0:00 1970 since t1 was initialized with 0

    Pls let me know why this happens and how it can be rectified

    Note:
    Operating System - OS X (Mac)
    Compiler - GCC

    Given below is the code

    Code:
    /*
    
    Aim - To set time_t variable to 01-Jan-1970 00:00:00
    */
    
    #include <iostream>
    #include <time.h>
    
    int main()
    {
        system("clear");
    
    
        time_t t1(0);
        
        std :: cout << "t1 = " << ctime(&t1) << std :: endl;                                                          
        
        //Why does this display "Thu Jan  1 07:30:00 1970" ?
        //I expected it to "Thu Jan  1 00:0:00 1970",  since t1 was initialized with 0
        
        
    
        return(0);
    }

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: time_t

    I assume you live in a time zone which is 7.30 hours away from GMT zone (London).


    You may use functions like gmtime to get UCT times or use tzset to set your timezone accordingly.

  3. #3
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: time_t

    That would put you on a longitude somewhere between Laos and the Philippines?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: time_t

    Thanks a ton !! I wouldn't have guessed that in my dreams !!

    Windows shows it as 8....

    Strange it actually needs to be 8hrs .... not sure why it is 7:30 ... though the timezone is properly configured on the system.

    Thanks again !!

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: time_t

    Singapore?
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: time_t

    Quote Originally Posted by Muthuveerappan View Post
    Thanks a ton !! I wouldn't have guessed that in my dreams !!

    Windows shows it as 8....

    Strange it actually needs to be 8hrs .... not sure why it is 7:30 ... though the timezone is properly configured on the system.

    Thanks again !!
    You may check for the values in _timezone and _tzname[0], _tzname[1]. These global variables are declared in time.h.

  7. #7
    Join Date
    Feb 2009
    Posts
    326

    Re: time_t

    Thanks
    ------------
    Thanks a lot !!

    gmtime actually resolves the problem because it is the GMT time

    I did check timezone, tzname[0], tzname[1], it doesn't show the correct names, but thats ok.

    I guess gmtime should do it for me.

    Question
    ---------------
    I am new to this library, according to what i understand, time_t can only store upto the nearest second. Correct me if i am wrong

    Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: time_t

    time_t is usually in seconds, which, in a 32 system, will cause a problem in the year 2038

    1970 + 2^31 seconds = 2038. Sometime about the 18th January.

    http://en.wikipedia.org/wiki/Year_2038_problem
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  9. #9
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: time_t

    Quote Originally Posted by Muthuveerappan View Post
    Thanks
    ------------
    Thanks a lot !!

    gmtime actually resolves the problem because it is the GMT time

    I did check timezone, tzname[0], tzname[1], it doesn't show the correct names, but thats ok.

    I guess gmtime should do it for me.

    Question
    ---------------
    I am new to this library, according to what i understand, time_t can only store upto the nearest second. Correct me if i am wrong

    Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?
    Yes, it is timeb (or _timeb) structure that additionally contains milliseconds. Use ftime to get a timeb struct.

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: time_t

    Quote Originally Posted by Muthuveerappan View Post
    Is there a data type that is capable of storing milliseconds or is time_t capable of storing it ?
    boost::posix_time::ptime
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  11. #11
    Join Date
    Feb 2009
    Posts
    326

    Re: time_t

    Thanks a lot !!

    Just feel timeb might be compiler specific (I could be completely wrong)

    We are working on mac and windows... so i thought it would be better to use boost library (posix time).

    the name posix worries me... if it would work on windows ....

    hopefully it would work on windows as well.

  12. #12
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: time_t

    Quote Originally Posted by Muthuveerappan View Post
    hopefully it would work on windows as well.
    It does.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  13. #13
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: time_t

    Quote Originally Posted by Muthuveerappan View Post
    Thanks a lot !!

    Just feel timeb might be compiler specific (I could be completely wrong)

    We are working on mac and windows... so i thought it would be better to use boost library (posix time).

    the name posix worries me... if it would work on windows ....

    hopefully it would work on windows as well.
    ftime and timeb were available for any platform.

    Code:
    /* get local timestamp */
    std::string getTimestampAsString(struct timeb* pTimB)
    {
        char            szTimFmt[]      = "%Y%m%d %H%M%S";
        char            szTim[64]        = { '\0' };
        struct timeb    tim;            
        struct tm*      pTim            = NULL;
    
        /* get current time in milliseconds */
        if (pTimB == NULL)
            ftime(&tim);
        else
            memcpy(&tim, pTimB, sizeof(struct timeb));
    
        /* convert time part to local time  */
        pTim = localtime(&tim.time);
        /* convert to ascii                 */
        strftime(szTim, sizeof(szTim), szTimFmt, pTim);
        std::ostringstream oss;
        oss << szTim << "." << std::setw(3) << std::setfill('\0') << std::right << tim.millitm;
        return oss.str();
    }

Tags for this Thread

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