libzypp  10.5.0
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 
00052     enum TimeBase { TB_LOCALTIME, TB_UTC };
00053 
00055     Date()
00056     : _date( 0 )
00057     {}
00059     Date( ValueType date_r )
00060     : _date( date_r )
00061     {}
00063     Date( const std::string & seconds_r );
00064 
00071     Date( const std::string & date_str, const std::string & format );
00073     Date( const std::string & date_str, const std::string & format, TimeBase base_r );
00074 
00076     static Date now()
00077     { return ::time( 0 ); }
00078 
00079   public:
00081     operator ValueType() const
00082     { return _date; }
00083 
00088     Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
00089     Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
00090     Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
00091     Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
00092 
00093     Date & operator++(/*prefix*/) { _date += 1; return *this; }
00094     Date & operator--(/*prefix*/) { _date -= 1; return *this; }
00095 
00096     Date operator++(int/*postfix*/) { return _date++; }
00097     Date operator--(int/*postfix*/) { return _date--; }
00099 
00100   public:
00107     std::string form( const std::string & format_r ) const
00108     { return form( format_r, TB_LOCALTIME ); }
00110     std::string form( const std::string & format_r, TimeBase base_r ) const;
00111 
00115     std::string asString() const
00116     { return form( "%c" ); }
00117 
00121     std::string asSeconds() const
00122     { return form( "%s" ); }
00123 
00124   private:
00129     ValueType _date;
00130   };
00132 
00134   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
00135   { return str << obj.asString(); }
00136 
00137   class DateFormatException : public Exception
00138   {
00139   public:
00140     DateFormatException( const std::string & msg ) : Exception( msg )
00141     {}
00142   };
00143 
00145 } // namespace zypp
00147 #endif // ZYPP_DATE_H