dnstime.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #include <string>
  16. #include <iomanip>
  17. #include <iostream>
  18. #include <sstream>
  19. #include <vector>
  20. #include "base64.h"
  21. #include "buffer.h"
  22. #include "messagerenderer.h"
  23. #include "name.h"
  24. #include "rrtype.h"
  25. #include "rrttl.h"
  26. #include "rdata.h"
  27. #include "rdataclass.h"
  28. #include <boost/lexical_cast.hpp>
  29. #include <stdio.h>
  30. #include <time.h>
  31. #include "dnstime.h"
  32. using namespace std;
  33. namespace isc {
  34. namespace dns {
  35. enum {
  36. DATE_LEN = 14 // YYYYMMDDHHmmSS
  37. };
  38. string
  39. DNSSECTimeToText(const time_t timeval)
  40. {
  41. struct tm *t = gmtime(&timeval);
  42. ostringstream oss;
  43. oss << setfill('0')
  44. << setw(4) << t->tm_year + 1900
  45. << setw(2) << t->tm_mon + 1
  46. << setw(2) << t->tm_mday
  47. << setw(2) << t->tm_hour
  48. << setw(2) << t->tm_min
  49. << setw(2) << t->tm_sec;
  50. return (oss.str());
  51. }
  52. static inline void
  53. checkRange(int min, int max, int value, const string& valname) {
  54. if ((value >= min) && (value <= max)) {
  55. return;
  56. }
  57. ostringstream oss;
  58. oss << "Invalid " << valname << " value: " << value;
  59. dns_throw(InvalidTime, oss.str().c_str());
  60. }
  61. static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  62. static inline bool
  63. isLeap(int y) {
  64. return ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0);
  65. }
  66. time_t
  67. DNSSECTimeFromText(const string& time_txt)
  68. {
  69. time_t timeval;
  70. // first try reading YYYYMMDDHHmmSS format
  71. int year, month, day, hour, minute, second;
  72. if (time_txt.length() != DATE_LEN ||
  73. sscanf(time_txt.c_str(), "%4d%2d%2d%2d%2d%2d",
  74. &year, &month, &day, &hour, &minute, &second) != 6)
  75. {
  76. ostringstream oss;
  77. oss << "Couldn't convert time value: " << time_txt;
  78. dns_throw(InvalidTime, oss.str().c_str());
  79. }
  80. checkRange(1970, 9999, year, "year");
  81. checkRange(1, 12, month, "month");
  82. checkRange(1, days[month - 1] + ((month == 2 && isLeap(year)) ? 1 : 0),
  83. day, "day");
  84. checkRange(0, 23, hour, "hour");
  85. checkRange(0, 59, minute, "minute");
  86. checkRange(0, 60, second, "second");
  87. timeval = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
  88. for (int m = 0; m < (month - 1); m++) {
  89. timeval += days[m] * 86400;
  90. }
  91. if (isLeap(year) && month > 2) {
  92. timeval += 86400;
  93. }
  94. for (int y = 1970; y < year; y++) {
  95. timeval += ((isLeap(y) ? 366 : 365 ) * 86400);
  96. }
  97. return (timeval);
  98. }
  99. }
  100. }