time.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <time.h>
  2. #include "test.h"
  3. #ifndef NULL
  4. #error NULL not defined
  5. #endif
  6. #ifndef CLOCKS_PER_SEC
  7. #error CLOCKS_PER_SEC not defined
  8. #endif
  9. #define test_strftime(_fmt, _tm, _expected) do { \
  10. char buf[128]; \
  11. strftime(buf, sizeof(buf), _fmt, &_tm); \
  12. test_string(buf, _expected); \
  13. } while (0)
  14. void test_time_h(void)
  15. {
  16. struct tm tm;
  17. tm.tm_year = 2001 - 1900;
  18. tm.tm_mon = 7 - 1;
  19. tm.tm_mday = 4;
  20. tm.tm_hour = 13;
  21. tm.tm_min = 40;
  22. tm.tm_sec = 50;
  23. tm.tm_isdst = -1;
  24. testing_header("time.h");
  25. testing_comment("letting mktime() adjust fields");
  26. mktime(&tm);
  27. testing_comment("testing asctime()");
  28. test_string(asctime(&tm), "Wed Jul 4 13:40:50 2001\n");
  29. testing_comment("testing strftime()");
  30. test_strftime("%a", tm, "Wed");
  31. test_strftime("%A", tm, "Wednesday");
  32. test_strftime("%b", tm, "Jul");
  33. test_strftime("%B", tm, "July");
  34. test_strftime("%c", tm, "see 7.23.1");
  35. test_strftime("%C", tm, "01");
  36. test_strftime("%d", tm, "04");
  37. test_strftime("%D", tm, "07/04/01");
  38. test_strftime("%e", tm, " 4");
  39. test_strftime("%F", tm, "2001-07-04");
  40. test_strftime("%g", tm, "week_of_year");
  41. test_strftime("%G", tm, "2001");
  42. test_strftime("%h", tm, "Jul");
  43. test_strftime("%H", tm, "13");
  44. test_strftime("%I", tm, "01");
  45. test_strftime("%j", tm, "160");
  46. test_strftime("%m", tm, "07");
  47. test_strftime("%n", tm, "\n");
  48. test_strftime("%p", tm, "PM");
  49. test_strftime("%r", tm, "01:40");
  50. test_strftime("%R", tm, "13:40");
  51. test_strftime("%S", tm, "50");
  52. test_strftime("%t", tm, "\t");
  53. test_strftime("%T", tm, "13:40:50");
  54. test_strftime("%u", tm, "weekday_number");
  55. test_strftime("%U", tm, "week number");
  56. test_strftime("%V", tm, "week number");
  57. test_strftime("%w", tm, "weekday number");
  58. test_strftime("%W", tm, "week number");
  59. test_strftime("%x", tm, "date per 7.23.1");
  60. test_strftime("%X", tm, "time per 7.23.1");
  61. test_strftime("%y", tm, "01");
  62. test_strftime("%Y", tm, "2001");
  63. test_strftime("%z", tm, "tz_offset");
  64. test_strftime("%Z", tm, "tz_name");
  65. test_strftime("%%", tm, "%");
  66. testing_end();
  67. }