Date: Also allow conversion from/to string relative to UTC (previously just localtime).
[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     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;
51
52     enum TimeBase { TB_LOCALTIME, TB_UTC };
53
54     /** Default ctor: 0 */
55     Date()
56     : _date( 0 )
57     {}
58     /** Ctor taking time_t value. */
59     Date( ValueType date_r )
60     : _date( date_r )
61     {}
62     /** Ctor taking time_t value as string. */
63     Date( const std::string & seconds_r );
64
65     /**
66      * Ctor from a \a date_str (in localtime) formatted using \a format.
67      *
68      * \throws DateFormatException in case \a date_str cannot be
69      *         parsed according to \a format.
70      */
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 );
74
75     /** Return the current time. */
76     static Date now()
77     { return ::time( 0 ); }
78
79   public:
80     /** Conversion to time_t. */
81     operator ValueType() const
82     { return _date; }
83
84     /** \name Arithmetic operations.
85      * \c + \c - \c * \c / are provided via conversion to time_t.
86     */
87     //@{
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; }
92
93     Date & operator++(/*prefix*/) { _date += 1; return *this; }
94     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
95
96     Date operator++(int/*postfix*/) { return _date++; }
97     Date operator--(int/*postfix*/) { return _date--; }
98     //@}
99
100   public:
101     /** Return string representation according to format as localtime.
102      * \see 'man strftime' (which is used internaly) for valid
103      * conversion specifiers in format.
104      *
105      * \return An empty string on illegal format.
106      **/
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;
111
112     /** Default string representation of Date.
113      * The preferred date and time representation for the current locale.
114      **/
115     std::string asString() const
116     { return form( "%c" ); }
117
118     /** Convert to string representation of calendar time in
119      *  numeric form (like "1029255142").
120      **/
121     std::string asSeconds() const
122     { return form( "%s" ); }
123
124   private:
125     /** Calendar time.
126      * The number of seconds elapsed since 00:00:00 on January 1, 1970,
127      * Coordinated Universal Time (UTC).
128      **/
129     ValueType _date;
130   };
131   ///////////////////////////////////////////////////////////////////
132
133   /** \relates Date Stream output */
134   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
135   { return str << obj.asString(); }
136
137   class DateFormatException : public Exception
138   {
139   public:
140     DateFormatException( const std::string & msg ) : Exception( msg )
141     {}
142   };
143
144   /////////////////////////////////////////////////////////////////
145 } // namespace zypp
146 ///////////////////////////////////////////////////////////////////
147 #endif // ZYPP_DATE_H