ignore
[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     //Date( const std::string & seconds_r ) : _date( fromSECONDS (seconds_r) ) {}
46
47     /** Return the current time. */
48     static Date now()
49     { return ::time( 0 ); }
50
51   public:
52     /** Conversion to time_t. */
53     operator ValueType() const
54     { return _date; }
55
56     /** \name Arithmetic operations.
57      * \c + \c - \c * \c / are provided via conversion to time_t.
58     */
59     //@{
60     Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
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
65     Date & operator++(/*prefix*/) { _date += 1; return *this; }
66     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
67
68     Date operator++(int/*postfix*/) { return _date++; }
69     Date operator--(int/*postfix*/) { return _date--; }
70     //@}
71
72   public:
73     /** Return string representation according to format.
74      * \see 'man strftime' (which is used internaly) for valid
75      * conversion specifiers in format.
76      *
77      * \return An empty string on illegal format.
78      **/
79     std::string form( const std::string & format_r ) const;
80
81     /** Default string representation of Date.
82      * The preferred date and time representation for the current locale.
83      **/
84     std::string asString() const
85     { return form( "%c" ); }
86
87     /** Convert to string representation of calendar time in
88      *  numeric form (like "1029255142").
89      **/
90     std::string asSeconds() const
91     { return form( "%s" ); }
92
93   private:
94     /** Calendar time.
95      * The number of seconds elapsed since 00:00:00 on January 1, 1970,
96      * Coordinated Universal Time (UTC).
97      **/
98     ValueType _date;
99   };
100   ///////////////////////////////////////////////////////////////////
101
102   /** \relates Date Stream output */
103   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
104   { return str << obj.asString(); }
105
106   /////////////////////////////////////////////////////////////////
107 } // namespace zypp
108 ///////////////////////////////////////////////////////////////////
109 #endif // ZYPP_DATE_H