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 /** Default ctor: 0 */
43 /** Ctor taking time_t value. */
44 Date( ValueType date_r )
47 /** Ctor taking time_t value as string. */
48 Date( const std::string & seconds_r );
51 * Ctor from a \a date_str formatted using \a format.
53 * \throws DateFormatException in case \a date_str cannot be
54 * parsed according to \a format.
56 Date( const std::string & date_str, const std::string & format);
58 /** Return the current time. */
60 { return ::time( 0 ); }
63 /** Conversion to time_t. */
64 operator ValueType() const
67 /** \name Arithmetic operations.
68 * \c + \c - \c * \c / are provided via conversion to time_t.
71 Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
72 Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
73 Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
74 Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
76 Date & operator++(/*prefix*/) { _date += 1; return *this; }
77 Date & operator--(/*prefix*/) { _date -= 1; return *this; }
79 Date operator++(int/*postfix*/) { return _date++; }
80 Date operator--(int/*postfix*/) { return _date--; }
84 /** Return string representation according to format.
85 * \see 'man strftime' (which is used internaly) for valid
86 * conversion specifiers in format.
88 * \return An empty string on illegal format.
90 std::string form( const std::string & format_r ) const;
92 /** Default string representation of Date.
93 * The preferred date and time representation for the current locale.
95 std::string asString() const
96 { return form( "%c" ); }
98 /** Convert to string representation of calendar time in
99 * numeric form (like "1029255142").
101 std::string asSeconds() const
102 { return form( "%s" ); }
106 * The number of seconds elapsed since 00:00:00 on January 1, 1970,
107 * Coordinated Universal Time (UTC).
111 ///////////////////////////////////////////////////////////////////
113 /** \relates Date Stream output */
114 inline std::ostream & operator<<( std::ostream & str, const Date & obj )
115 { return str << obj.asString(); }
117 class DateFormatException : public Exception
120 DateFormatException( const std::string & msg ) : Exception( msg )
124 /////////////////////////////////////////////////////////////////
126 ///////////////////////////////////////////////////////////////////
127 #endif // ZYPP_DATE_H