Date.h

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_DATE_H
00013 #define ZYPP_DATE_H
00014 
00015 #include <ctime>
00016 #include <iosfwd>
00017 #include <string>
00018 
00019 #include "zypp/base/Exception.h"
00020 
00022 namespace zypp
00023 { 
00024 
00026   //
00027   //    CLASS NAME : Date
00028   //
00031   class Date
00032   {
00033     friend std::ostream & operator<<( std::ostream & str, const Date & obj );
00034 
00035   public:
00036 
00037     typedef time_t ValueType;
00038 
00039     static const ValueType second       = 1;
00040     static const ValueType minute       = 60;
00041     static const ValueType hour         = 3600;
00042     static const ValueType day          = 86400;
00043     static const ValueType month28      = 2419200;
00044     static const ValueType month29      = 2505600;
00045     static const ValueType month30      = 2592000;
00046     static const ValueType month31      = 2678400;
00047     static const ValueType month        = month30;
00048     static const ValueType year365      = 31536000;
00049     static const ValueType year366      = 31622400;
00050     static const ValueType year         = year365;
00051 
00053     Date()
00054     : _date( 0 )
00055     {}
00057     Date( ValueType date_r )
00058     : _date( date_r )
00059     {}
00061     Date( const std::string & seconds_r );
00062 
00069     Date( const std::string & date_str, const std::string & format);
00070 
00072     static Date now()
00073     { return ::time( 0 ); }
00074 
00075   public:
00077     operator ValueType() const
00078     { return _date; }
00079 
00084     Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
00085     Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
00086     Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
00087     Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
00088 
00089     Date & operator++(/*prefix*/) { _date += 1; return *this; }
00090     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
00091 
00092     Date operator++(int/*postfix*/) { return _date++; }
00093     Date operator--(int/*postfix*/) { return _date--; }
00095 
00096   public:
00103     std::string form( const std::string & format_r ) const;
00104 
00108     std::string asString() const
00109     { return form( "%c" ); }
00110 
00114     std::string asSeconds() const
00115     { return form( "%s" ); }
00116 
00117   private:
00122     ValueType _date;
00123   };
00125 
00127   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
00128   { return str << obj.asString(); }
00129 
00130   class DateFormatException : public Exception
00131   {
00132   public:
00133     DateFormatException( const std::string & msg ) : Exception( msg )
00134     {}
00135   };
00136 
00138 } // namespace zypp
00140 #endif // ZYPP_DATE_H

doxygen