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,774 of 243,242   
   Michael Sanders to Kenny McCormack   
   Re: printf and time_t   
   05 Jan 26 08:32:30   
   
   From: porkchop@invalid.foo   
      
   On Mon, 5 Jan 2026 07:19:02 -0000 (UTC), Kenny McCormack wrote:   
      
   > Here's kind of an old chestnut...   
   >   
   > The question is: How can you reliably printf() a time_t value?   
   > What conversion spec should you use?   
   >   
   > The answer seems to be: try stuff until the compiler doesn't warn.   
   >   
   > Note that there is now %z for size_t, but there really should be a "Just do   
   > the right thing - you're the compiler, you know what type the arg is, do   
   > the right thing" spec.   
   >   
   > It turns out that in my use case, %lu works, but how can you know?   
   > Note, BTW, that another way to do it is to use %d and case the time_t value   
   > to (int), but that seems kludgey.   
      
   Hi Kenny not sure this will help you but check out ,   
   struct tm has these members (names/meanings/types are fixed   
   by the standard):   
      
   struct tm {   
       int tm_sec;   // seconds after the minute   [0, 60]  (leap second possible)   
       int tm_min;   // minutes after the hour     [0, 59]   
       int tm_hour;  // hours since midnight       [0, 23]   
       int tm_mday;  // day of the month           [1, 31]   
       int tm_mon;   // months since January       [0, 11]  (0 = Jan)   
       int tm_year;  // years since 1900   
       int tm_wday;  // days since Sunday          [0, 6]   (0 = Sun)   
       int tm_yday;  // days since January 1       [0, 365]   
       int tm_isdst; // Daylight Saving Time flag   
                    // >0 = DST in effect   
                    //  0 = not in effect   
                    // <0 = information not available   
   };   
      
   #include    
   #include    
      
   int main(void) {   
       time_t t = time(NULL);   
       struct tm tm;   
      
       localtime_r(&t, &tm);   
      
       printf("%04d-%02d-%02d %02d:%02d:%02d\n",   
              tm.tm_year + 1900,   
              tm.tm_mon + 1,   
              tm.tm_mday,   
              tm.tm_hour,   
              tm.tm_min,   
              tm.tm_sec);   
      
       return 0;   
   }   
      
   --   
   :wq   
   Mike Sanders   
      
   --- 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