This is the mail archive of the glibc-bugs@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug libc/5184] strftime stats /etc/localtime too much


------- Additional Comments From mann at vmware dot com  2008-08-13 00:22 -------
Why is strftime special?  If I write a program that repeatedly calls ctime()
and/or localtime(), it reads /etc/localtime only once.  But if I call strftime,
it reads /etc/localtime once for each call to strftime.  TZ is not set in my
environment.  This behavior is inconsistent with the claim that "All the time
functions are required to check TZ all the time.  Missing that envvar,
/etc/localtime is used."  Surely localtime and ctime are included in the set
"All the time functions."

Reading this file on every call is pretty ugly for programs that write log
messages and include a timestamp on each line formatted using strftime.

I'm using libc 2.6.1-1ubuntu10, from Ubuntu 7.10 on x86_64.

Here's a sample program to demonstrate.  Run it with no arguments and it will
open and read /etc/localtime 11 times.

#include <stdio.h>
#include <sys/time.h>
#include <time.h>

int
main(int argc, char **argv)
{
   int count = 10, i;

   if (argc > 1) {
      count = atoi(argv[1]);
   }

   for (i = 0; i < count; i++) {
      time_t now = time(NULL);
      printf("%s", ctime(&now));
   }

   for (i = 0; i < count; i++) {
      time_t now = time(NULL);
      struct tm ltime;

      localtime_r(&now, &ltime);
      printf("%02u:%02u:%02u\n", ltime.tm_hour, ltime.tm_min, ltime.tm_sec);
   }

   for (i = 0; i < count; i++) {
      time_t now = time(NULL);
      struct tm ltime;
      char stime[1024];

      localtime_r(&now, &ltime);
      strftime(stime, sizeof(stime), "%H:%M:%S", &ltime);
      printf("%s\n", stime);
   }
}


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


http://sourceware.org/bugzilla/show_bug.cgi?id=5184

------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]