tst-strptime2.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include <limits.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. static const struct
  5. {
  6. const char *fmt;
  7. long int gmtoff;
  8. } tests[] =
  9. {
  10. { "1113472456 +1000", 36000 },
  11. { "1113472456 -1000", -36000 },
  12. { "1113472456 +10", 36000 },
  13. { "1113472456 -10", -36000 },
  14. { "1113472456 +1030", 37800 },
  15. { "1113472456 -1030", -37800 },
  16. { "1113472456 +0030", 1800 },
  17. { "1113472456 -0030", -1800 },
  18. { "1113472456 -1330", LONG_MAX },
  19. { "1113472456 +1330", LONG_MAX },
  20. { "1113472456 -1060", LONG_MAX },
  21. { "1113472456 +1060", LONG_MAX },
  22. { "1113472456 1030", LONG_MAX },
  23. };
  24. #define ntests (sizeof (tests) / sizeof (tests[0]))
  25. int
  26. main (void)
  27. {
  28. int result = 0;
  29. for (int i = 0; i < ntests; ++i)
  30. {
  31. struct tm tm;
  32. if (strptime (tests[i].fmt, "%s %z", &tm) == NULL)
  33. {
  34. if (tests[i].gmtoff != LONG_MAX)
  35. {
  36. printf ("round %d: strptime unexpectedly failed\n", i);
  37. result = 1;
  38. }
  39. continue;
  40. }
  41. if (tm.tm_gmtoff != tests[i].gmtoff)
  42. {
  43. printf ("round %d: tm_gmtoff is %ld\n", i, (long int) tm.tm_gmtoff);
  44. result = 1;
  45. }
  46. }
  47. if (result == 0)
  48. puts ("all OK");
  49. return 0;
  50. }