home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,808 of 243,242   
   Keith Thompson to James Kuyper   
   Re: printf and time_t   
   05 Jan 26 14:41:26   
   
   From: Keith.S.Thompson+u@gmail.com   
      
   James Kuyper  writes:   
   > On 2026-01-05 11:34, David Brown wrote:   
   > ...   
   >> As I understand it, time_t is intended to be suitable for holding a   
   >> number of seconds ...   
   >   
   > The standard says nothing about that.   
   >   
   >> ... (it is used for that purpose in struct timespec). ...   
   >   
   > The standard says nothing to connect time_t to struct timespec.   
      
   Yes, but also no.   
      
   struct timespec contains at least the following members, in any order:   
      
       time_t tv_sec;  // whole seconds — ≥ 0   
       long   tv_nsec; // nanoseconds — [0, 999999999]   
      
   But a footnote says:   
      
       The tv_sec member is a linear count of seconds and may not have   
       the normal semantics of a time_t.   
      
   This makes for a simpler implementation *if* the time_t value   
   returned by time(), and operated on by difftime(), mktime(), ctime(),   
   gmtime(), and localtime(), happens to hold a linear count of seconds   
   (as it does on most systems).   
      
   It's odd that time_t is used for two potentially very different   
   purposes, one as a member of struct timespec and another as used   
   in all other contexts.  And I would guess that a lot of code that   
   uses struct timespec *assumes* that its time_t member has the same   
   semantics value returned by time(NULL).   
      
   For example, as I write this the time is 2026-01-05 22:32:57.881 UTC.   
   The corresponding value returned by time() is 1767652377 (seconds   
   since the 1970 epoch, no milliseconds).  An implementation could   
   represent the current time (the value returned by time(NULL) as a   
   64-bit integer with the value 20260105223257881.  But timespec_get()   
   would still have to set the tv_sec member to 1767652377.   
      
   It might have been cleaner either to require that time_t represents a   
   count of seconds, or to use a type other than time_t for the tv_sec   
   member of struct timespec.   
      
   I know there are systems that use something other than seconds   
   since 1970 in the underlying time representation, but are there any   
   C implementations that don't use seconds since 1970?  (POSIX and   
   Windows both specify that.)   
      
   --   
   Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com   
   void Void(void) { Void(); } /* The recursive call of the void */   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca