ns_date.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* Import. */
  18. #include <arpa/nameser.h>
  19. #include <ctype.h>
  20. #include <errno.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #define SPRINTF(x) ((size_t)sprintf x)
  25. /* Forward. */
  26. static int datepart(const char *, int, int, int, int *);
  27. /* Public. */
  28. /*%
  29. * Convert a date in ASCII into the number of seconds since
  30. * 1 January 1970 (GMT assumed). Format is yyyymmddhhmmss, all
  31. * digits required, no spaces allowed.
  32. */
  33. uint32_t
  34. ns_datetosecs(const char *cp, int *errp) {
  35. struct tm time;
  36. uint32_t result;
  37. int mdays, i;
  38. static const int days_per_month[12] =
  39. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. if (strlen(cp) != 14U) {
  41. *errp = 1;
  42. return (0);
  43. }
  44. *errp = 0;
  45. memset(&time, 0, sizeof time);
  46. time.tm_year = datepart(cp + 0, 4, 1990, 9999, errp) - 1900;
  47. time.tm_mon = datepart(cp + 4, 2, 01, 12, errp) - 1;
  48. time.tm_mday = datepart(cp + 6, 2, 01, 31, errp);
  49. time.tm_hour = datepart(cp + 8, 2, 00, 23, errp);
  50. time.tm_min = datepart(cp + 10, 2, 00, 59, errp);
  51. time.tm_sec = datepart(cp + 12, 2, 00, 59, errp);
  52. if (*errp) /*%< Any parse errors? */
  53. return (0);
  54. /*
  55. * OK, now because timegm() is not available in all environments,
  56. * we will do it by hand. Roll up sleeves, curse the gods, begin!
  57. */
  58. #define SECS_PER_DAY ((uint32_t)24*60*60)
  59. #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
  60. result = time.tm_sec; /*%< Seconds */
  61. result += time.tm_min * 60; /*%< Minutes */
  62. result += time.tm_hour * (60*60); /*%< Hours */
  63. result += (time.tm_mday - 1) * SECS_PER_DAY; /*%< Days */
  64. /* Months are trickier. Look without leaping, then leap */
  65. mdays = 0;
  66. for (i = 0; i < time.tm_mon; i++)
  67. mdays += days_per_month[i];
  68. result += mdays * SECS_PER_DAY; /*%< Months */
  69. if (time.tm_mon > 1 && isleap(1900+time.tm_year))
  70. result += SECS_PER_DAY; /*%< Add leapday for this year */
  71. /* First figure years without leapdays, then add them in. */
  72. /* The loop is slow, FIXME, but simple and accurate. */
  73. result += (time.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */
  74. for (i = 70; i < time.tm_year; i++)
  75. if (isleap(1900+i))
  76. result += SECS_PER_DAY; /*%< Add leapday for prev year */
  77. return (result);
  78. }
  79. /* Private. */
  80. /*%
  81. * Parse part of a date. Set error flag if any error.
  82. * Don't reset the flag if there is no error.
  83. */
  84. static int
  85. datepart(const char *buf, int size, int min, int max, int *errp) {
  86. int result = 0;
  87. int i;
  88. for (i = 0; i < size; i++) {
  89. if (!isdigit((unsigned char)(buf[i])))
  90. *errp = 1;
  91. result = (result * 10) + buf[i] - '0';
  92. }
  93. if (result < min)
  94. *errp = 1;
  95. if (result > max)
  96. *errp = 1;
  97. return (result);
  98. }
  99. /*! \file */