bug-getdate1.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* BZ #5451 */
  2. #include <time.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <support/temp_file.h>
  6. static char *templ_filename;
  7. // Writes template given as parameter to file,
  8. // specified as the argument
  9. static void
  10. output_to_template_file (const char *str)
  11. {
  12. FILE *fd = fopen (templ_filename, "w");
  13. if (fd == NULL)
  14. {
  15. printf ("Can not open file for writing\n");
  16. exit (1);
  17. }
  18. fprintf (fd, "%s\n", str);
  19. fclose (fd);
  20. }
  21. // Calls getdate() function with specified parameter,
  22. // specified as the argument, also checks the contents of
  23. // file with template and prints the result
  24. static int
  25. process_getdate_on (const char *str)
  26. {
  27. struct tm *res;
  28. char templ[1000];
  29. FILE *fd = fopen (templ_filename, "r");
  30. if (fd == NULL)
  31. {
  32. printf ("Can not open file for reading\n");
  33. exit (1);
  34. }
  35. if (fgets (templ, 1000, fd) == NULL)
  36. {
  37. printf ("Can not read file\n");
  38. exit (1);
  39. }
  40. fclose (fd);
  41. res = getdate (str);
  42. if (res == NULL)
  43. {
  44. printf ("Failed on getdate(\"%s\"), template is: %s", str, templ);
  45. printf ("Error number: %d\n\n", getdate_err);
  46. return 1;
  47. }
  48. printf ("Success on getdate(\"%s\"), template is: %s\n", str, templ);
  49. printf ("Result is\n");
  50. printf ("Seconds: %d\n", res->tm_sec);
  51. printf ("Minutes: %d\n", res->tm_min);
  52. printf ("Hour: %d\n", res->tm_hour);
  53. printf ("Day of month: %d\n", res->tm_mday);
  54. printf ("Month of year: %d\n", res->tm_mon);
  55. printf ("Years since 1900: %d\n", res->tm_year);
  56. printf ("Day of week: %d\n", res->tm_wday);
  57. printf ("Day of year: %d\n", res->tm_yday);
  58. printf ("Daylight Savings flag: %d\n\n", res->tm_isdst);
  59. return 0;
  60. }
  61. static int
  62. do_test (int argc, char *argv[])
  63. {
  64. templ_filename = argv[1];
  65. setenv ("DATEMSK", templ_filename, 1);
  66. /*
  67. * The following 4 testcases reproduce the problem:
  68. * 1. Templates "%S" and "%M" are not processed,
  69. * when used without "%H" template
  70. */
  71. int res = 0;
  72. output_to_template_file ("%M");
  73. res |= process_getdate_on ("1");
  74. output_to_template_file ("%M %H");
  75. res |= process_getdate_on ("1 2");
  76. output_to_template_file ("%S");
  77. res |= process_getdate_on ("1");
  78. output_to_template_file ("%S %H");
  79. res |= process_getdate_on ("1 2");
  80. /*
  81. * The following 9 testcases reproduce the problem:
  82. * 2. Templates "%Y", "%y", "%d", "%C", "%C %y"
  83. * are not processed separately
  84. */
  85. output_to_template_file ("%Y");
  86. process_getdate_on ("2001");
  87. output_to_template_file ("%Y %m");
  88. res |= process_getdate_on ("2001 3");
  89. output_to_template_file ("%y");
  90. res |= process_getdate_on ("70");
  91. output_to_template_file ("%y %m");
  92. res |= process_getdate_on ("70 3");
  93. output_to_template_file ("%d");
  94. res |= process_getdate_on ("06");
  95. output_to_template_file ("%d %m");
  96. res |= process_getdate_on ("25 3");
  97. output_to_template_file ("%C");
  98. res |= process_getdate_on ("20");
  99. output_to_template_file ("%C %y %m");
  100. res |= process_getdate_on ("20 3 2");
  101. output_to_template_file ("%C %y");
  102. res |= process_getdate_on ("20 5");
  103. /*
  104. * The following testcase reproduces the problem:
  105. * 3. When template is "%Y %m", day of month is not set
  106. * to 1 as standard requires
  107. */
  108. output_to_template_file ("%Y %m");
  109. res |= process_getdate_on ("2008 3");
  110. return res;
  111. }
  112. #define TEST_FUNCTION_ARGV do_test
  113. static void
  114. do_prepare (int argc, char **argv)
  115. {
  116. if (argc < 2)
  117. {
  118. puts ("Command line: progname template_filename_full_path");
  119. exit (1);
  120. }
  121. add_temp_file (argv[1]);
  122. }
  123. #define PREPARE do_prepare
  124. #include <support/test-driver.c>