localtime and time

This is a discussion on localtime and time within the C Programming forums, part of the General Programming Boards category; Hello.. My code is: Code: unsigned long ta = time(0); tm *t = localtime(&ta); I wonder why I get (time_t ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    localtime and time

    Hello..

    My code is:

    Code:
    unsigned long ta = time(0);
    tm *t = localtime(&ta);
    I wonder why I get (time_t = unsigned long) :
    Code:
    error C2664: 'localtime' : cannot convert parameter 1 from 'unsigned long *__w64 ' to 'const time_t *'
    I use MSVC 2005.

    Thanks for help

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Because time returns a time_t and you're not using a time_t for the value returned. And it's struct tm, not just tm.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    But time_t is actually long.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Quote Originally Posted by http://msdn2.microsoft.com/en-us/library/323b6b3k.aspx
    time_t (__int64 or long integer)


    Represents time values in mktime, time, ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64, ctime_s, _ctime32_s, _ctime64_s, _wctime_s, _wctime32_s, _wctime64_s, ctime, _ctime32, _ctime64, _wctime, _wctime32, _wctime64 and gmtime, _gmtime32, _gmtime64. If _USE_32BIT_TIME_T is defined, time_t is a long integer. If not defined, it is a 64-bit integer.
    That's microsoft's take on it. I can't find it anywhere, but I don't think any standard guarantees time_t to be long.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by l2u
    But time_t is actually long.
    You're learning backwards if you believe this is true for the language.
    [...]
    time_t
    which are arithmetic types capable of representing times;
    It may not even be an integral type.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    If you are writing C code remember to set its option:
    Press Alt+F7
    Open C/C++ in left tree view>Advanced> set "Compile As" to Compile As C Code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  3. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 06:11 AM
  4. localtime(), time(). What happened?
    By Nutka in forum C Programming
    Replies: 7
    Last Post: 12-10-2002, 05:22 AM
  5. relating date....
    By Prakash in forum C Programming
    Replies: 3
    Last Post: 09-19-2001, 09:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21