Sophie

Sophie

distrib > Scientific%20Linux > 5x > x86_64 > by-pkgid > 2003d1abfa0c20ee77815f0da33e2c1c > files > 70

glibc-2.5-49.el5_5.5.src.rpm

2007-12-12  Ulrich Drepper  <drepper@redhat.com>

	* time/bug-getdate1.c (do_test): Don't use century values which
	aren't valid on 32-bit systems.

2007-12-08  Ulrich Drepper  <drepper@redhat.com>

	[BZ #5451]
	* time/getdate.c: Fix filling in default values.
	* time/bug-getdate1.c: New file.
	* time/Makefile: Add rules to build and run bug-getdate1.

--- libc/time/getdate.c	4 Sep 2003 08:25:11 -0000	1.12
+++ libc/time/getdate.c	10 Dec 2007 01:40:43 -0000	1.13
@@ -212,28 +212,31 @@ __getdate_r (const char *string, struct 
       tp->tm_sec = tm.tm_sec;
     }
 
+  /* Fill in the gaps.  */
+  if (tp->tm_hour == INT_MIN)
+    tp->tm_hour = 0;
+  if (tp->tm_min == INT_MIN)
+    tp->tm_min = 0;
+  if (tp->tm_sec == INT_MIN)
+    tp->tm_sec = 0;
+
   /* If no date is given, today is assumed if the given hour is
      greater than the current hour and tomorrow is assumed if
      it is less.  */
   if (tp->tm_hour >= 0 && tp->tm_hour <= 23
-      && tp->tm_year == INT_MIN && tp->tm_mon == INT_MIN
+      && tp->tm_mon == INT_MIN
       && tp->tm_mday == INT_MIN && tp->tm_wday == INT_MIN)
     {
-      tp->tm_year = tm.tm_year;
       tp->tm_mon = tm.tm_mon;
       tp->tm_mday = tm.tm_mday + ((tp->tm_hour - tm.tm_hour) < 0 ? 1 : 0);
       mday_ok = 1;
     }
 
-  /* Fill in the gaps.  */
+  /* More fillers.  */
   if (tp->tm_year == INT_MIN)
     tp->tm_year = tm.tm_year;
-  if (tp->tm_hour == INT_MIN)
-    tp->tm_hour = 0;
-  if (tp->tm_min == INT_MIN)
-    tp->tm_min = 0;
-  if (tp->tm_sec == INT_MIN)
-    tp->tm_sec = 0;
+  if (tp->tm_mon == INT_MIN)
+    tp->tm_mon = tm.tm_mon;
 
   /* Check if the day of month is within range, and if the time can be
      represented in a time_t.  We make use of the fact that the mktime
--- libc/time/Makefile	9 Feb 2007 01:33:54 -0000	1.111
+++ libc/time/Makefile	10 Dec 2007 01:41:39 -0000	1.112
@@ -36,7 +36,7 @@ distribute := datemsk
 tests	:= test_time clocktest tst-posixtz tst-strptime tst_wcsftime \
 	   tst-getdate tst-mktime tst-mktime2 tst-ftime_l tst-strftime \
 	   tst-mktime3 tst-strptime2 bug-asctime bug-asctime_r bug-mktime1 \
-	   tst-strptime3
+	   tst-strptime3 bug-getdate1
 
 include ../Rules
 
@@ -56,3 +56,5 @@ test_time-ARGS= EST5EDT CST
 
 tst-strptime-ENV = LOCPATH=${common-objpfx}localedata
 tst-ftime_l-ENV = LOCPATH=${common-objpfx}localedata
+
+bug-getdate1-ARGS = ${objpfx}bug-getdate1-fmt
--- libc/time/bug-getdate1.c	1 Jan 1970 00:00:00 -0000
+++ libc/time/bug-getdate1.c	12 Dec 2007 18:22:51 -0000	1.2
@@ -0,0 +1,146 @@
+/* BZ #5451 */
+#include <time.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static char *templ_filename;
+
+// Writes template given as parameter to file,
+// specified as the argument
+static void
+output_to_template_file (const char *str)
+{
+  FILE *fd = fopen (templ_filename, "w");
+  if (fd == NULL)
+    {
+      printf ("Can not open file for writing\n");
+      exit (1);
+    }
+
+  fprintf (fd, "%s\n", str);
+  fclose (fd);
+}
+
+// Calls getdate() function with specified parameter,
+// specified as the argument, also checks the contents of
+// file with template and prints the result
+static int
+process_getdate_on (const char *str)
+{
+  struct tm *res;
+  char templ[1000];
+  FILE *fd = fopen (templ_filename, "r");
+
+  if (fd == NULL)
+    {
+      printf ("Can not open file for reading\n");
+      exit (1);
+    }
+
+  if (fgets (templ, 1000, fd) == NULL)
+    {
+      printf ("Can not read file\n");
+      exit (1);
+    }
+  fclose (fd);
+
+  res = getdate (str);
+  if (res == NULL)
+    {
+      printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
+      printf ("Error number: %d\n\n", getdate_err);
+      return 1;
+    }
+  printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
+  printf ("Result is\n");
+  printf ("Seconds: %d\n", res->tm_sec);
+  printf ("Minutes: %d\n", res->tm_min);
+  printf ("Hour: %d\n", res->tm_hour);
+  printf ("Day of month: %d\n", res->tm_mday);
+  printf ("Month of year: %d\n", res->tm_mon);
+  printf ("Years since 1900: %d\n", res->tm_year);
+  printf ("Day of week: %d\n", res->tm_wday);
+  printf ("Day of year: %d\n", res->tm_yday);
+  printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
+  return 0;
+}
+
+static int
+do_test (int argc, char *argv[])
+{
+
+  templ_filename = argv[1];
+
+  setenv ("DATEMSK", templ_filename, 1);
+
+  /*
+   * The following 4 testcases reproduce the problem:
+   * 1. Templates "%S" and "%M" are not processed,
+   *    when used without "%H" template
+   */
+  int res = 0;
+  output_to_template_file ("%M");
+  res |= process_getdate_on ("1");
+
+  output_to_template_file ("%M %H");
+  res |= process_getdate_on ("1 2");
+
+  output_to_template_file ("%S");
+  res |= process_getdate_on ("1");
+
+  output_to_template_file ("%S %H");
+  res |= process_getdate_on ("1 2");
+
+  /*
+   * The following 9 testcases reproduce the problem:
+   * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
+   *    are not processed separately
+   */
+  output_to_template_file ("%Y");
+  process_getdate_on ("2001");
+
+  output_to_template_file ("%Y %m");
+  res |= process_getdate_on ("2001 3");
+
+  output_to_template_file ("%y");
+  res |= process_getdate_on ("70");
+
+  output_to_template_file ("%y %m");
+  res |= process_getdate_on ("70 3");
+
+  output_to_template_file ("%d");
+  res |= process_getdate_on ("06");
+
+  output_to_template_file ("%d %m");
+  res |= process_getdate_on ("25 3");
+
+  output_to_template_file ("%C");
+  res |= process_getdate_on ("20");
+
+  output_to_template_file ("%C %y %m");
+  res |= process_getdate_on ("20 3 2");
+
+  output_to_template_file ("%C %y");
+  res |= process_getdate_on ("20 5");
+
+  /*
+   * The following testcase reproduces the problem:
+   * 3. When template is "%Y %m", day of month is not set
+   *    to 1 as standard requires
+   */
+  output_to_template_file ("%Y %m");
+  res |= process_getdate_on ("2008 3");
+
+  return res;
+}
+
+#define PREPARE(argc, argv) \
+  if (argc < 2)								\
+    {									\
+      puts ("Command line: progname template_filename_full_path");	\
+      exit (1);								\
+    }									\
+  add_temp_file (argv[1])
+
+#define TEST_FUNCTION do_test (argc, argv)
+#include "../test-skeleton.c"