TimeA2.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2015 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "TimeA2.h"
  36. #include <cstring>
  37. #include "util.h"
  38. #include "array_fun.h"
  39. namespace aria2 {
  40. Time::Time() : tp_(Clock::now()), good_(true) {}
  41. Time::Time(time_t t) : tp_(Clock::from_time_t(t)), good_(true) {}
  42. void Time::reset()
  43. {
  44. tp_ = Clock::now();
  45. good_ = true;
  46. }
  47. Time::Clock::duration Time::difference() const { return Clock::now() - tp_; }
  48. Time::Clock::duration Time::difference(const Time& time) const
  49. {
  50. return time.tp_ - tp_;
  51. }
  52. void Time::setTimeFromEpoch(time_t t)
  53. {
  54. tp_ = Clock::from_time_t(t);
  55. good_ = true;
  56. }
  57. std::string Time::toHTTPDate() const
  58. {
  59. char buf[32];
  60. time_t t = getTimeFromEpoch();
  61. struct tm* tms = gmtime(&t); // returned struct is statically allocated.
  62. size_t r = strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", tms);
  63. return std::string(&buf[0], &buf[r]);
  64. }
  65. Time Time::parse(const std::string& datetime, const std::string& format)
  66. {
  67. struct tm tm;
  68. memset(&tm, 0, sizeof(tm));
  69. char* r = strptime(datetime.c_str(), format.c_str(), &tm);
  70. if (r != datetime.c_str() + datetime.size()) {
  71. return Time::null();
  72. }
  73. time_t thetime = timegm(&tm);
  74. if (thetime == -1) {
  75. if (tm.tm_year >= 2038 - 1900) {
  76. thetime = INT32_MAX;
  77. }
  78. }
  79. return Time(thetime);
  80. }
  81. Time Time::parseRFC1123(const std::string& datetime)
  82. {
  83. return parse(datetime, "%a, %d %b %Y %H:%M:%S GMT");
  84. }
  85. Time Time::parseRFC1123Alt(const std::string& datetime)
  86. {
  87. return parse(datetime, "%a, %d %b %Y %H:%M:%S +0000");
  88. }
  89. Time Time::parseRFC850(const std::string& datetime)
  90. {
  91. return parse(datetime, "%a, %d-%b-%y %H:%M:%S GMT");
  92. }
  93. Time Time::parseRFC850Ext(const std::string& datetime)
  94. {
  95. return parse(datetime, "%a, %d-%b-%Y %H:%M:%S GMT");
  96. }
  97. Time Time::parseAsctime(const std::string& datetime)
  98. {
  99. return parse(datetime, "%a %b %d %H:%M:%S %Y");
  100. }
  101. Time Time::parseHTTPDate(const std::string& datetime)
  102. {
  103. Time (*funcs[])(const std::string&) = {
  104. &parseRFC1123, &parseRFC1123Alt, &parseRFC850,
  105. &parseAsctime, &parseRFC850Ext,
  106. };
  107. for (auto func : funcs) {
  108. Time t = func(datetime);
  109. if (t.good()) {
  110. return t;
  111. }
  112. }
  113. return Time::null();
  114. }
  115. } // namespace aria2