Thread: localtime curiosity

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    localtime curiosity

    Is there some reason that localtime takes a pointer to time_t instead of just time_t? I can't see that the time_t ever gets modified and the time function returns a time_t not a pointer, so you can't make a call like localtime(time(NULL),&tmStruct).

    Just curious...

  2. #2
    Registered User ralu.'s Avatar
    Join Date
    Feb 2009
    Location
    Bucharest, RO
    Posts
    32
    Function prototipe:

    struct tm *localtime(const time_t *timep);
    struct tm *localtime_r(const time_t *timep, struct tm *result);

    Man page:

    The localtime() function converts the calendar time timep to broken-
    time representation, expressed relative to the user’s specified time‐
    zone. The function acts as if it called tzset(3) and sets the external
    variables tzname with information about the current timezone, timezone
    with the difference between Coordinated Universal Time (UTC) and local
    standard time in seconds, and daylight to a non-zero value if daylight
    savings time rules apply during some part of the year. The return
    value points to a statically allocated struct which might be overwrit‐
    ten by subsequent calls to any of the date and time functions. The
    localtime_r() function does the same, but stores the data in a user-
    supplied struct. It need not set tzname, timezone, and daylight.
    I have stopped reading Stephen King novels. Now I just read C code instead.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Appreciate the reply, but I had no confusion about the function or how to use it. I was curious if anyone knew the rationale behind passing a pointer to time_t instead of just passing time_t.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in C structs are mostly passed by const pointer to save stack space (as in C++ they are passed by const reference). This Also prevents unneeded copying which can be time-consuming for big-structs
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    Agreed, possibly time_t is a struct in some implementations (none that I've seen, but...)

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by rjhanby View Post
    Agreed, possibly time_t is a struct in some implementations (none that I've seen, but...)
    nobody wants to change API when internal implementation changes... so APIs for types that could be changed to structs in the future are made as if the types are already structs
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Curiosity question...
    By Raigne in forum C++ Programming
    Replies: 2
    Last Post: 04-05-2007, 09:49 PM
  2. localtime and time
    By l2u in forum C Programming
    Replies: 5
    Last Post: 07-24-2006, 12:10 PM
  3. Just out of curiosity
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 12-01-2005, 05:37 PM
  4. Not important, only a curiosity...
    By BrownB in forum C Programming
    Replies: 35
    Last Post: 12-27-2004, 03:32 PM
  5. localtime(), time(). What happened?
    By Nutka in forum C Programming
    Replies: 7
    Last Post: 12-10-2002, 06:22 AM