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,800 of 243,242   
   Michael S to Lew Pitcher   
   Re: printf and time_t   
   05 Jan 26 19:23:51   
   
   From: already5chosen@yahoo.com   
      
   On Mon, 5 Jan 2026 16:23:09 -0000 (UTC)   
   Lew Pitcher  wrote:   
      
   > On Mon, 05 Jan 2026 10:51:38 +0200, Michael S wrote:   
   >   
   > > On Mon, 5 Jan 2026 00:17:07 -0800   
   > > Andrey Tarasevich  wrote:   
   > >   
   > >> On Sun 1/4/2026 11:19 PM, Kenny McCormack wrote:   
   > >> > The question is: How can you reliably printf() a time_t value?   
   > >> > What conversion spec should you use?   
   > >>   
   > >> You can't. As far as the language is concerned, `time_t` is   
   > >> intended to be an opaque type. It has to be a real type, so it is   
   > >> either an integer of a plain floating-point type. But other than   
   > >> that nothing is known about it. There's really no point in   
   > >> printing it.   
   > >>   
   > >> If you still want to, you can do it in some implementation-specific   
   > >> way. Which still immediately means that you can't do it "reliably",   
   > >> if I understand what you mean correctly.   
   > >>   
   > >   
   > > I can't think about situation in which casting time_t value to 'long   
   > > long' can go wrong.   
   >   
   > As Andrey pointed out, time_t can resolve to a floatingpoint type,   
   > so, "long long" would go wrong if the implementation typedefs it to   
   >  float, or   
   >  double, or   
   >  long double.   
   >   
      
   Reading literally what you wrote makes no sense, so I assume that you   
   meant that 'it' above constitutes time_t rather than 'long long'.   
      
   The rest of the post assumes 64-bit 'long long'.   
   'typedef float time_t' where float==IEEE binary32 is a bad idea, because   
   you lose your 1sec resolution after 7 months. I.e. with Unix epoch you   
   lost it a year or two before Ritchi finished his first C compiler.   
   'typedef double time_t' means that you lost 1 sec resolution ~3   
   orders of magnitude before you got a chance to overflow 'long long'.   
      
   Only in case of 'typedef long double time_t' there is a chance that   
   overflow happens before resolution is lost. But barely so for 80-bit   
   long double format prevalent on i386/AMD64 computers.   
      
   --- 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