Merged revisions 4705-4906 via svnmerge from
[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 ///////////////////////////////////////////////////////////////////
20 namespace zypp
21 { /////////////////////////////////////////////////////////////////
22
23   ///////////////////////////////////////////////////////////////////
24   //
25   //    CLASS NAME : Date
26   //
27   /** Store and operate on date (time_t).
28   */
29   class Date
30   {
31     friend std::ostream & operator<<( std::ostream & str, const Date & obj );
32
33   public:
34
35     typedef time_t ValueType;
36
37     /** Default ctor: 0 */
38     Date()
39     : _date( 0 )
40     {}
41     /** Ctor taking time_t value. */
42     Date( ValueType date_r )
43     : _date( date_r )
44     {}
45     /** Ctor taking time_t value as string. */
46     Date( const std::string & seconds_r );
47
48     /** Return the current time. */
49     static Date now()
50     { return ::time( 0 ); }
51
52   public:
53     /** Conversion to time_t. */
54     operator ValueType() const
55     { return _date; }
56
57     /** \name Arithmetic operations.
58      * \c + \c - \c * \c / are provided via conversion to time_t.
59     */
60     //@{
61     Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
62     Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
63     Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
64     Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
65
66     Date & operator++(/*prefix*/) { _date += 1; return *this; }
67     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
68
69     Date operator++(int/*postfix*/) { return _date++; }
70     Date operator--(int/*postfix*/) { return _date--; }
71     //@}
72
73   public:
74     /** Return string representation according to format.
75      * \see 'man strftime' (which is used internaly) for valid
76      * conversion specifiers in format.
77      *
78      * \return An empty string on illegal format.
79      **/
80     std::string form( const std::string & format_r ) const;
81
82     /** Default string representation of Date.
83      * The preferred date and time representation for the current locale.
84      **/
85     std::string asString() const
86     { return form( "%c" ); }
87
88     /** Convert to string representation of calendar time in
89      *  numeric form (like "1029255142").
90      **/
91     std::string asSeconds() const
92     { return form( "%s" ); }
93
94   private:
95     /** Calendar time.
96      * The number of seconds elapsed since 00:00:00 on January 1, 1970,
97      * Coordinated Universal Time (UTC).
98      **/
99     ValueType _date;
100   };
101   ///////////////////////////////////////////////////////////////////
102
103   /** \relates Date Stream output */
104   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
105   { return str << obj.asString(); }
106
107   /////////////////////////////////////////////////////////////////
108 } // namespace zypp
109 ///////////////////////////////////////////////////////////////////
110 #endif // ZYPP_DATE_H