TimeA2.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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. #ifndef _D_TIME_H_
  36. #define _D_TIME_H_
  37. #include "common.h"
  38. #include <stdint.h>
  39. #include <string>
  40. #include "a2time.h"
  41. namespace aria2 {
  42. class Time {
  43. private:
  44. struct timeval tv_;
  45. bool good_;
  46. struct timeval getCurrentTime() const;
  47. public:
  48. // The time value is initialized so that it represents the time at which
  49. // this object was created.
  50. Time();
  51. Time(const Time& time);
  52. Time(time_t sec);
  53. Time(const struct timeval& tv);
  54. ~Time();
  55. Time& operator=(const Time& time);
  56. bool operator<(const Time& time) const;
  57. // Makes this object's time value up to date.
  58. void reset();
  59. bool elapsed(time_t sec) const;
  60. bool elapsedInMillis(int64_t millis) const;
  61. time_t difference() const;
  62. time_t difference(const struct timeval& now) const;
  63. time_t difference(const Time& now) const
  64. {
  65. return difference(now.tv_);
  66. }
  67. int64_t differenceInMillis() const;
  68. int64_t differenceInMillis(const struct timeval& now) const;
  69. int64_t differenceInMillis(const Time& now) const
  70. {
  71. return differenceInMillis(now.tv_);
  72. }
  73. // Returns true if this object's time value is zero.
  74. bool isZero() const;
  75. int64_t getTimeInMicros() const;
  76. int64_t getTimeInMillis() const;
  77. // Returns this object's time value in seconds.
  78. time_t getTime() const;
  79. void setTimeInSec(time_t sec);
  80. bool isNewer(const Time& time) const;
  81. void advance(time_t sec);
  82. bool good() const;
  83. // Returns !good()
  84. bool bad() const;
  85. std::string toHTTPDate() const;
  86. // Currently timezone is assumed as GMT.
  87. static Time parse(const std::string& datetime, const std::string& format);
  88. // Currently timezone is assumed to GMT.
  89. static Time parseRFC1123(const std::string& datetime);
  90. // Currently timezone is assumed to GMT.
  91. static Time parseRFC850(const std::string& datetime);
  92. // Currently timezone is assumed to GMT. Basically the format is
  93. // RFC850, but year part is 4digit, eg 2008 This format appears in
  94. // original Netscape's PERSISTENT CLIENT STATE HTTP COOKIES
  95. // Specification. http://curl.haxx.se/rfc/cookie_spec.html
  96. static Time parseRFC850Ext(const std::string& datetime);
  97. // Currently timezone is assumed to GMT.
  98. // ANSI C's asctime() format
  99. static Time parseAsctime(const std::string& datetime);
  100. // Try parseRFC1123, parseRFC850, parseAsctime, parseRFC850Ext in
  101. // that order and returns the first "good" Time object returned by
  102. // these functions.
  103. static Time parseHTTPDate(const std::string& datetime);
  104. static Time null();
  105. };
  106. } // namespace aria2
  107. #endif // _D_TIME_H_