lldate.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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=viewergpl$
  8. *
  9. * Copyright (c) 2006-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #ifndef LL_LLDATE_H
  35. #define LL_LLDATE_H
  36. #include <iosfwd>
  37. #include <string>
  38. #include "llerror.h"
  39. #include "stdtypes.h"
  40. // Represents a point in time after UNIX epoch (1970-01-01 00:00:00 UTC).
  41. class LLDate
  42. {
  43. protected:
  44. LOG_CLASS(LLDate);
  45. public:
  46. // Constructs a date equal to the UTC epoch start date.
  47. LL_INLINE constexpr LLDate()
  48. : mSecondsSinceEpoch(0.0)
  49. {
  50. }
  51. // Constructs a date from a number of seconds since the UTC epoch value.
  52. LL_INLINE LLDate(F64 seconds_since_epoch)
  53. : mSecondsSinceEpoch(seconds_since_epoch)
  54. {
  55. }
  56. // Constructs a date from a string representation
  57. // The date is constructed in the fromString() method. See that method for
  58. // details of supported formats. If that method fails to parse the date,
  59. // the date is set to epoch.
  60. LLDate(const std::string& iso8601_date);
  61. // Returns the date as in ISO-8601 string.
  62. std::string asString() const;
  63. // A more "human readable" timestamp format: same as ISO-8601, but with the
  64. // "T" between date and time replaced with a space and the "Z" replaced
  65. // with " UTC"
  66. std::string asTimeStamp(bool with_utc = true) const;
  67. void toStream(std::ostream&) const;
  68. bool split(S32* year, S32* month = NULL, S32* day = NULL,
  69. S32* hour = NULL, S32* min = NULL, S32* sec = NULL) const;
  70. std::string toHTTPDateString(const char* fmt) const;
  71. static std::string toHTTPDateString(tm* gmt, const char* fmt);
  72. // These two methods set the date from an ISO-8601 string. The parser only
  73. // supports strings conforming to YYYYF-MM-DDTHH:MM:SS.FFZ where Y is year,
  74. // M is month, D is day, H is hour, M is minute, S is second, F is sub-
  75. // second, and all other characters are literal. If these method fail to
  76. // parse the date, the previous date is retained. Reurn true if the string
  77. // was successfully parsed.
  78. bool fromString(const std::string& iso8601_date);
  79. bool fromStream(std::istream&);
  80. bool fromYMDHMS(S32 year, S32 month = 1, S32 day = 0, S32 hour = 0,
  81. S32 min = 0, S32 sec = 0);
  82. // Returns the date in seconds since epoch.
  83. LL_INLINE F64 secondsSinceEpoch() const { return mSecondsSinceEpoch; }
  84. // Sets the date in seconds since epoch.
  85. LL_INLINE void secondsSinceEpoch(F64 seconds) { mSecondsSinceEpoch = seconds; }
  86. // Creates a LLDate object set to the current time.
  87. static LLDate now();
  88. // Compares dates using operator< so we can order them using STL.
  89. bool operator<(const LLDate& rhs) const;
  90. // Remaining comparison operators in terms of operator<
  91. // This conforms to the expectation of STL.
  92. LL_INLINE bool operator>(const LLDate& rhs) const { return rhs < *this; }
  93. LL_INLINE bool operator<=(const LLDate& rhs) const { return !(rhs < *this); }
  94. LL_INLINE bool operator>=(const LLDate& rhs) const { return !(*this < rhs); }
  95. LL_INLINE bool operator!=(const LLDate& rhs) const { return *this < rhs || rhs < *this; }
  96. LL_INLINE bool operator==(const LLDate& rhs) const { return !(*this != rhs); }
  97. // Compare to epoch UTC.
  98. LL_INLINE bool isNull() const { return mSecondsSinceEpoch == 0.0; }
  99. LL_INLINE bool notNull() const { return mSecondsSinceEpoch != 0.0; }
  100. private:
  101. F64 mSecondsSinceEpoch;
  102. };
  103. // Helper function to stream out a date
  104. std::ostream& operator<<(std::ostream& s, const LLDate& date);
  105. // Helper function to stream in a date
  106. std::istream& operator>>(std::istream& s, LLDate& date);
  107. #endif // LL_LLDATE_H