PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 29#ifndef LL_LLDATE_H
 30#define LL_LLDATE_H
 31
 32#include <iosfwd>
 33#include <string>
 34
 35#include "stdtypes.h"
 36
 37/** 
 38 * @class LLDate
 39 * @brief This class represents a particular point in time in UTC.
 40 *
 41 * The date class represents a point in time after epoch - 1970-01-01.
 42 */
 43class LL_COMMON_API LLDate
 44{
 45public:
 46	/** 
 47	 * @brief Construct a date equal to epoch.
 48	 */
 49	LLDate();
 50
 51	/** 
 52	 * @brief Construct a date equal to the source date.
 53	 */
 54	LLDate(const LLDate& date);
 55
 56	/** 
 57	 * @brief Construct a date from a seconds since epoch value.
 58	 *
 59	 * @pararm seconds_since_epoch The number of seconds since UTC epoch.
 60	 */
 61	LLDate(F64 seconds_since_epoch);
 62
 63	/** 
 64	 * @brief Construct a date from a string representation
 65	 *
 66	 * The date is constructed in the <code>fromString()</code>
 67	 * method. See that method for details of supported formats.
 68	 * If that method fails to parse the date, the date is set to epoch.
 69	 * @param iso8601_date An iso-8601 compatible representation of the date.
 70	 */
 71	LLDate(const std::string& iso8601_date);
 72
 73	/** 
 74	 * @brief Return the date as in ISO-8601 string.
 75	 *
 76	 * @return A string representation of the date.
 77	 */
 78	std::string asString() const;
 79	std::string asRFC1123() const;
 80	void toStream(std::ostream&) const;
 81	bool split(S32 *year, S32 *month = NULL, S32 *day = NULL, S32 *hour = NULL, S32 *min = NULL, S32 *sec = NULL) const;
 82	std::string toHTTPDateString (std::string fmt) const;
 83	static std::string toHTTPDateString (tm * gmt, std::string fmt);
 84	/** 
 85	 * @brief Set the date from an ISO-8601 string.
 86	 *
 87	 * The parser only supports strings conforming to
 88	 * YYYYF-MM-DDTHH:MM:SS.FFZ where Y is year, M is month, D is day,
 89	 * H is hour, M is minute, S is second, F is sub-second, and all
 90	 * other characters are literal.
 91	 * If this method fails to parse the date, the previous date is
 92	 * retained.
 93	 * @param iso8601_date An iso-8601 compatible representation of the date.
 94	 * @return Returns true if the string was successfully parsed.
 95	 */
 96	bool fromString(const std::string& iso8601_date);
 97	bool fromStream(std::istream&);
 98	bool fromYMDHMS(S32 year, S32 month = 1, S32 day = 0, S32 hour = 0, S32 min = 0, S32 sec = 0);
 99
100	/** 
101	 * @brief Return the date in seconds since epoch.
102	 *
103	 * @return The number of seconds since epoch UTC.
104	 */
105	F64 secondsSinceEpoch() const;
106
107	/** 
108	 * @brief Set the date in seconds since epoch.
109	 *
110	 * @param seconds The number of seconds since epoch UTC.
111	 */
112	void secondsSinceEpoch(F64 seconds);
113    
114    /**
115     * @brief Create an LLDate object set to the current time.
116	 *
117	 * @return The number of seconds since epoch UTC.
118	 */
119    static LLDate now();
120
121	/** 
122	 * @brief Compare dates using operator< so we can order them using STL.
123	 *
124	 * @param rhs -- the right hand side of the comparison operator
125	 */
126	bool operator<(const LLDate& rhs) const;
127    
128	/** 
129	 * @brief Remaining comparison operators in terms of operator<
130     * This conforms to the expectation of STL.
131	 *
132	 * @param rhs -- the right hand side of the comparison operator
133	 */
134    bool operator>(const LLDate& rhs) const { return rhs < *this; }
135    bool operator<=(const LLDate& rhs) const { return !(rhs < *this); }
136    bool operator>=(const LLDate& rhs) const { return !(*this < rhs); }
137    bool operator!=(const LLDate& rhs) const { return (*this < rhs) || (rhs < *this); }
138    bool operator==(const LLDate& rhs) const { return !(*this != rhs); }
139
140	/**
141	 * @brief Compare to epoch UTC.
142	 */
143
144	bool isNull() const { return mSecondsSinceEpoch == 0.0; }
145	bool notNull() const { return mSecondsSinceEpoch != 0.0; }
146	 
147
148private:
149	F64 mSecondsSinceEpoch;
150};
151
152// Helper function to stream out a date
153LL_COMMON_API std::ostream& operator<<(std::ostream& s, const LLDate& date);
154
155// Helper function to stream in a date
156LL_COMMON_API std::istream& operator>>(std::istream& s, LLDate& date);
157
158
159
160#endif // LL_LLDATE_H