fork(7) download
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. static int
  7. is_valid_iso8601_date_value(char *in)
  8. {
  9. struct tm result;
  10. char **f;
  11. char *ret;
  12. char *formats[] = {
  13. "%Y",
  14. "%Y-%m",
  15. "%y-%m",
  16. "%Y-%m-%d",
  17. "%y-%m-%d",
  18. "%Y%m%d",
  19. "%y%m%d",
  20. "%Y-%m-%d %T",
  21. "%y-%m-%d %T",
  22. "%Y%m%d%H%M%S",
  23. "%y%m%d%H%M%S",
  24. "%Y-%m-%dT%T",
  25. "%y-%m-%dT%T",
  26. "%Y-%m-%dT%TZ",
  27. "%y-%m-%dT%TZ",
  28. "%Y-%m-%d %TZ",
  29. "%y-%m-%d %TZ",
  30. "%Y%m%dT%TZ",
  31. "%y%m%dT%TZ",
  32. "%Y%m%d %TZ",
  33. "%y%m%d %TZ",
  34. NULL
  35. };
  36.  
  37. memset(&result, 0, sizeof(result));
  38.  
  39. for (f = formats; f && *f; f++)
  40. {
  41. ret = strptime(in, *f, &result);
  42. if (ret && *ret == '\0')
  43. return 1;
  44. }
  45.  
  46. return 0;
  47. }
  48.  
  49. int main(void) {
  50.  
  51. char date_str[] = "2010-01-01T01:01:01Z";
  52.  
  53. if (is_valid_iso8601_date_value(date_str))
  54. {
  55. printf("%s is a valid iso8601 date!", date_str);
  56. return 0;
  57. }
  58. else
  59. {
  60. printf("%s is not a valid iso8601 date!", date_str);
  61. return 1;
  62. }
  63. }
  64.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
2010-01-01T01:01:01Z is a valid iso8601 date!