dnstime.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. void
  36. DNSSECTimeToText(const time_t timeval, string& s)
  37. {
  38. struct tm *t = gmtime(&timeval);
  39. s.reserve(14); // YYYYMMDDHHmmSS
  40. ostringstream oss(s);
  41. oss << setfill('0') << setw(4) << t->tm_year + 1900
  42. << setw(2) << t->tm_mon + 1 << t->tm_mday
  43. << t->tm_hour << t->tm_min << t->tm_sec;
  44. s = oss.str();
  45. }
  46. static inline void
  47. checkRange(int min, int max, int value, const string& valname) {
  48. if ((value >= min) && (value <= max)) {
  49. return;
  50. }
  51. ostringstream oss;
  52. oss << "Invalid " << valname << " value: " << value;
  53. dns_throw(InvalidTime, oss.str().c_str());
  54. }
  55. static int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  56. static inline bool
  57. isLeap(int y) {
  58. return ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0);
  59. }
  60. time_t
  61. DNSSECTimeFromText(const string& time_txt)
  62. {
  63. time_t timeval;
  64. // first try reading YYYYMMDDHHmmSS format
  65. int year, month, day, hour, minute, second;
  66. if (sscanf(time_txt.c_str(), "%4d%2d%2d%2d%2d%2d",
  67. &year, &month, &day, &hour, &minute, &second) == 6) {
  68. checkRange(1970, 9999, year, "year");
  69. checkRange(1, 12, month, "month");
  70. checkRange(1, days[month - 1] + ((month == 2 && isLeap(year)) ? 1 : 0),
  71. day, "day");
  72. checkRange(0, 23, hour, "hour");
  73. checkRange(0, 59, minute, "minute");
  74. checkRange(0, 60, second, "second");
  75. timeval = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
  76. for (int m = 0; m < (month - 1); m++)
  77. timeval += days[m] * 86400;
  78. if (isLeap(year) && month > 2)
  79. timeval += 86400;
  80. for (int y = 1970; y < year; y++) {
  81. timeval += ((isLeap(y) ? 366 : 365 ) * 86400);
  82. }
  83. return (timeval);
  84. }
  85. ostringstream oss;
  86. oss << "Couldn't convert time value: " << time_txt;
  87. dns_throw(InvalidTime, oss.str().c_str());
  88. }
  89. }
  90. }