1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
19 #include "zypp/base/Exception.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
29 /** Store and operate on date (time_t).
33 friend std::ostream & operator<<( std::ostream & str, const Date & obj );
37 typedef time_t ValueType;
39 static const ValueType second = 1;
40 static const ValueType minute = 60;
41 static const ValueType hour = 3600;
42 static const ValueType day = 86400;
43 static const ValueType month28 = 2419200;
44 static const ValueType month29 = 2505600;
45 static const ValueType month30 = 2592000;
46 static const ValueType month31 = 2678400;
47 static const ValueType month = month30;
48 static const ValueType year365 = 31536000;
49 static const ValueType year366 = 31622400;
50 static const ValueType year = year365;
52 enum TimeBase { TB_LOCALTIME, TB_UTC };
54 /** Default ctor: 0 */
58 /** Ctor taking time_t value. */
59 Date( ValueType date_r )
62 /** Ctor taking time_t value as string. */
63 Date( const std::string & seconds_r );
66 * Ctor from a \a date_str (in localtime) formatted using \a format.
68 * \throws DateFormatException in case \a date_str cannot be
69 * parsed according to \a format.
71 Date( const std::string & date_str, const std::string & format );
72 /** \overload with explicitly given \ref TimeBase. */
73 Date( const std::string & date_str, const std::string & format, TimeBase base_r );
75 /** Return the current time. */
77 { return ::time( 0 ); }
80 /** Conversion to time_t. */
81 operator ValueType() const
84 /** \name Arithmetic operations.
85 * \c + \c - \c * \c / are provided via conversion to time_t.
88 Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
89 Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
90 Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
91 Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
93 Date & operator++(/*prefix*/) { _date += 1; return *this; }
94 Date & operator--(/*prefix*/) { _date -= 1; return *this; }
96 Date operator++(int/*postfix*/) { return _date++; }
97 Date operator--(int/*postfix*/) { return _date--; }
101 /** Return string representation according to format as localtime.
102 * \see 'man strftime' (which is used internaly) for valid
103 * conversion specifiers in format.
105 * \return An empty string on illegal format.
107 std::string form( const std::string & format_r ) const
108 { return form( format_r, TB_LOCALTIME ); }
109 /** \overload with explicitly given \ref TimeBase. */
110 std::string form( const std::string & format_r, TimeBase base_r ) const;
112 /** Default string representation of Date.
113 * The preferred date and time representation for the current locale.
115 std::string asString() const
116 { return form( "%c" ); }
118 /** Convert to string representation of calendar time in
119 * numeric form (like "1029255142").
121 std::string asSeconds() const
122 { return form( "%s" ); }
126 * The number of seconds elapsed since 00:00:00 on January 1, 1970,
127 * Coordinated Universal Time (UTC).
131 ///////////////////////////////////////////////////////////////////
133 /** \relates Date Stream output */
134 inline std::ostream & operator<<( std::ostream & str, const Date & obj )
135 { return str << obj.asString(); }
137 class DateFormatException : public Exception
140 DateFormatException( const std::string & msg ) : Exception( msg )
144 /////////////////////////////////////////////////////////////////
146 ///////////////////////////////////////////////////////////////////
147 #endif // ZYPP_DATE_H