/indra/llcommon/lldate.h

https://bitbucket.org/lindenlab/viewer-beta/ · C++ Header · 160 lines · 38 code · 22 blank · 100 comment · 4 complexity · 53d856ba7a7fb97f8d083aabbed1f4cd MD5 · raw file

  1. /**
  2. * @file lldate.h
  3. * @author Phoenix
  4. * @date 2006-02-05
  5. * @brief Declaration of a simple date class.
  6. *
  7. * $LicenseInfo:firstyear=2006&license=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. #ifndef LL_LLDATE_H
  29. #define LL_LLDATE_H
  30. #include <iosfwd>
  31. #include <string>
  32. #include "stdtypes.h"
  33. /**
  34. * @class LLDate
  35. * @brief This class represents a particular point in time in UTC.
  36. *
  37. * The date class represents a point in time after epoch - 1970-01-01.
  38. */
  39. class LL_COMMON_API LLDate
  40. {
  41. public:
  42. /**
  43. * @brief Construct a date equal to epoch.
  44. */
  45. LLDate();
  46. /**
  47. * @brief Construct a date equal to the source date.
  48. */
  49. LLDate(const LLDate& date);
  50. /**
  51. * @brief Construct a date from a seconds since epoch value.
  52. *
  53. * @pararm seconds_since_epoch The number of seconds since UTC epoch.
  54. */
  55. LLDate(F64 seconds_since_epoch);
  56. /**
  57. * @brief Construct a date from a string representation
  58. *
  59. * The date is constructed in the <code>fromString()</code>
  60. * method. See that method for details of supported formats.
  61. * If that method fails to parse the date, the date is set to epoch.
  62. * @param iso8601_date An iso-8601 compatible representation of the date.
  63. */
  64. LLDate(const std::string& iso8601_date);
  65. /**
  66. * @brief Return the date as in ISO-8601 string.
  67. *
  68. * @return A string representation of the date.
  69. */
  70. std::string asString() const;
  71. std::string asRFC1123() const;
  72. void toStream(std::ostream&) const;
  73. bool split(S32 *year, S32 *month = NULL, S32 *day = NULL, S32 *hour = NULL, S32 *min = NULL, S32 *sec = NULL) const;
  74. std::string toHTTPDateString (std::string fmt) const;
  75. static std::string toHTTPDateString (tm * gmt, std::string fmt);
  76. /**
  77. * @brief Set the date from an ISO-8601 string.
  78. *
  79. * The parser only supports strings conforming to
  80. * YYYYF-MM-DDTHH:MM:SS.FFZ where Y is year, M is month, D is day,
  81. * H is hour, M is minute, S is second, F is sub-second, and all
  82. * other characters are literal.
  83. * If this method fails to parse the date, the previous date is
  84. * retained.
  85. * @param iso8601_date An iso-8601 compatible representation of the date.
  86. * @return Returns true if the string was successfully parsed.
  87. */
  88. bool fromString(const std::string& iso8601_date);
  89. bool fromStream(std::istream&);
  90. bool fromYMDHMS(S32 year, S32 month = 1, S32 day = 0, S32 hour = 0, S32 min = 0, S32 sec = 0);
  91. /**
  92. * @brief Return the date in seconds since epoch.
  93. *
  94. * @return The number of seconds since epoch UTC.
  95. */
  96. F64 secondsSinceEpoch() const;
  97. /**
  98. * @brief Set the date in seconds since epoch.
  99. *
  100. * @param seconds The number of seconds since epoch UTC.
  101. */
  102. void secondsSinceEpoch(F64 seconds);
  103. /**
  104. * @brief Create an LLDate object set to the current time.
  105. *
  106. * @return The number of seconds since epoch UTC.
  107. */
  108. static LLDate now();
  109. /**
  110. * @brief Compare dates using operator< so we can order them using STL.
  111. *
  112. * @param rhs -- the right hand side of the comparison operator
  113. */
  114. bool operator<(const LLDate& rhs) const;
  115. /**
  116. * @brief Remaining comparison operators in terms of operator<
  117. * This conforms to the expectation of STL.
  118. *
  119. * @param rhs -- the right hand side of the comparison operator
  120. */
  121. bool operator>(const LLDate& rhs) const { return rhs < *this; }
  122. bool operator<=(const LLDate& rhs) const { return !(rhs < *this); }
  123. bool operator>=(const LLDate& rhs) const { return !(*this < rhs); }
  124. bool operator!=(const LLDate& rhs) const { return (*this < rhs) || (rhs < *this); }
  125. bool operator==(const LLDate& rhs) const { return !(*this != rhs); }
  126. /**
  127. * @brief Compare to epoch UTC.
  128. */
  129. bool isNull() const { return mSecondsSinceEpoch == 0.0; }
  130. bool notNull() const { return mSecondsSinceEpoch != 0.0; }
  131. private:
  132. F64 mSecondsSinceEpoch;
  133. };
  134. // Helper function to stream out a date
  135. LL_COMMON_API std::ostream& operator<<(std::ostream& s, const LLDate& date);
  136. // Helper function to stream in a date
  137. LL_COMMON_API std::istream& operator>>(std::istream& s, LLDate& date);
  138. #endif // LL_LLDATE_H