Remove obsolete ResStatus bits.
[platform/upstream/libzypp.git] / zypp / Date.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Date.h
10  *
11 */
12 #ifndef ZYPP_DATE_H
13 #define ZYPP_DATE_H
14
15 #include <ctime>
16 #include <iosfwd>
17 #include <string>
18
19 #include "zypp/base/Exception.h"
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ///////////////////////////////////////////////////////////////////
26   //
27   //    CLASS NAME : Date
28   //
29   /** Store and operate on date (time_t).
30   */
31   class Date
32   {
33     friend std::ostream & operator<<( std::ostream & str, const Date & obj );
34
35   public:
36
37     typedef time_t ValueType;
38
39     /** Default ctor: 0 */
40     Date()
41     : _date( 0 )
42     {}
43     /** Ctor taking time_t value. */
44     Date( ValueType date_r )
45     : _date( date_r )
46     {}
47     /** Ctor taking time_t value as string. */
48     Date( const std::string & seconds_r );
49
50     /**
51      * Ctor from a \a date_str formatted using \a format.
52      *
53      * \throws DateFormatException in case \a date_str cannot be
54      *         parsed according to \a format.
55      */
56     Date( const std::string & date_str, const std::string & format);
57
58     /** Return the current time. */
59     static Date now()
60     { return ::time( 0 ); }
61
62   public:
63     /** Conversion to time_t. */
64     operator ValueType() const
65     { return _date; }
66
67     /** \name Arithmetic operations.
68      * \c + \c - \c * \c / are provided via conversion to time_t.
69     */
70     //@{
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; }
75
76     Date & operator++(/*prefix*/) { _date += 1; return *this; }
77     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
78
79     Date operator++(int/*postfix*/) { return _date++; }
80     Date operator--(int/*postfix*/) { return _date--; }
81     //@}
82
83   public:
84     /** Return string representation according to format.
85      * \see 'man strftime' (which is used internaly) for valid
86      * conversion specifiers in format.
87      *
88      * \return An empty string on illegal format.
89      **/
90     std::string form( const std::string & format_r ) const;
91
92     /** Default string representation of Date.
93      * The preferred date and time representation for the current locale.
94      **/
95     std::string asString() const
96     { return form( "%c" ); }
97
98     /** Convert to string representation of calendar time in
99      *  numeric form (like "1029255142").
100      **/
101     std::string asSeconds() const
102     { return form( "%s" ); }
103
104   private:
105     /** Calendar time.
106      * The number of seconds elapsed since 00:00:00 on January 1, 1970,
107      * Coordinated Universal Time (UTC).
108      **/
109     ValueType _date;
110   };
111   ///////////////////////////////////////////////////////////////////
112
113   /** \relates Date Stream output */
114   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
115   { return str << obj.asString(); }
116
117   class DateFormatException : public Exception
118   {
119   public:
120     DateFormatException( const std::string & msg ) : Exception( msg )
121     {}
122   };
123
124   /////////////////////////////////////////////////////////////////
125 } // namespace zypp
126 ///////////////////////////////////////////////////////////////////
127 #endif // ZYPP_DATE_H