libcamgm
Date.hpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | |
3 | _ _ _ _ __ _ |
4 | | | | | | \_/ | / \ | | |
5 | | | | | | |_| | / /\ \ | | |
6 | | |__ | | | | | | / ____ \ | |__ |
7 | |____||_| |_| |_|/ / \ \|____| |
8 | |
9 | core library |
10 | |
11 | (C) SUSE Linux Products GmbH |
12 \----------------------------------------------------------------------/
13 
14  File: Date.hpp
15 
16  Author: Michael Andres
17 
18 /-*/
19 #ifndef CA_MGM_DATE_HPP
20 #define CA_MGM_DATE_HPP
21 
22 #include <ctime>
23 #include <iosfwd>
24 #include <string>
25 
26 #include <ca-mgm/Exception.hpp>
27 
29 namespace ca_mgm
30 {
31 
33  //
34  // CLASS NAME : Date
35  //
38  class Date
39  {
40  friend std::ostream & operator<<( std::ostream & str, const Date & obj );
41 
42  public:
43 
44  typedef time_t ValueType;
45 
47  Date()
48  : _date( 0 )
49  {}
51  Date( ValueType date_r )
52  : _date( date_r )
53  {}
55  Date( const std::string & seconds_r );
56 
63  Date( const std::string & date_str, const std::string & format, bool utc = false);
64 
66  static Date now()
67  { return ::time( 0 ); }
68 
69  public:
71  operator ValueType() const
72  { return _date; }
73 
78  Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
79  Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
80  Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
81  Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
82 
83  Date & operator++(/*prefix*/) { _date += 1; return *this; }
84  Date & operator--(/*prefix*/) { _date -= 1; return *this; }
85 
86  Date operator++(int/*postfix*/) { return _date++; }
87  Date operator--(int/*postfix*/) { return _date--; }
89 
90  public:
97  std::string form( const std::string & format_r, bool utc = false ) const;
98 
102  std::string asString() const
103  { return form( "%c" ); }
104 
108  std::string asSeconds() const
109  { return form( "%s" ); }
110 
111  private:
117  };
119 
121  inline std::ostream & operator<<( std::ostream & str, const Date & obj )
122  { return str << obj.asString(); }
123 
125 } // namespace ca_mgm
127 #endif // CA_MGM_DATE_HPP
std::ostream & operator<<(std::ostream &str, const Date &obj)
Definition: Date.hpp:121
Definition: Date.hpp:38
time_t ValueType
Definition: Date.hpp:44
Date()
Definition: Date.hpp:47
std::string asString() const
Definition: Date.hpp:102
Date operator--(int)
Definition: Date.hpp:87
static Date now()
Definition: Date.hpp:66
Date(ValueType date_r)
Definition: Date.hpp:51
ValueType _date
Definition: Date.hpp:116
std::string asSeconds() const
Definition: Date.hpp:108
friend std::ostream & operator<<(std::ostream &str, const Date &obj)
std::string form(const std::string &format_r, bool utc=false) const
Date & operator*=(const time_t rhs)
Definition: Date.hpp:80
Date & operator+=(const time_t rhs)
Definition: Date.hpp:78
Date & operator/=(const time_t rhs)
Definition: Date.hpp:81
Common LiMaL exceptions.
Date & operator-=(const time_t rhs)
Definition: Date.hpp:79
Date & operator--()
Definition: Date.hpp:84
Date & operator++()
Definition: Date.hpp:83
Date operator++(int)
Definition: Date.hpp:86