Imported Upstream version 15.19.0
[platform/upstream/libzypp.git] / zypp / Date.h
index 9fe7cb9..2158d52 100644 (file)
@@ -17,6 +17,7 @@
 #include <string>
 
 #include "zypp/base/Exception.h"
+#include "zypp/base/EnumClass.h"
 
 ///////////////////////////////////////////////////////////////////
 namespace zypp
@@ -35,6 +36,7 @@ namespace zypp
   public:
 
     typedef time_t ValueType;
+    typedef time_t Duration;
 
     static const ValueType second      = 1;
     static const ValueType minute      = 60;
@@ -60,7 +62,7 @@ namespace zypp
     : _date( date_r )
     {}
     /** Ctor taking time_t value as string. */
-    Date( const std::string & seconds_r );
+    explicit Date( const std::string & seconds_r );
 
     /**
      * Ctor from a \a date_str (in localtime) formatted using \a format.
@@ -81,10 +83,13 @@ namespace zypp
     operator ValueType() const
     { return _date; }
 
-    /** \name Arithmetic operations.
-     * \c + \c - \c * \c / are provided via conversion to time_t.
-    */
+    /** \name Arithmetic operations. */
     //@{
+    Date operator+( const time_t rhs ) const { return _date + rhs; }
+    Date operator-( const time_t rhs ) const { return _date - rhs; }
+    Date operator*( const time_t rhs ) const { return _date * rhs; }
+    Date operator/( const time_t rhs ) const { return _date / rhs; }
+
     Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
     Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
     Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
@@ -102,7 +107,7 @@ namespace zypp
      * \see 'man strftime' (which is used internaly) for valid
      * conversion specifiers in format.
      *
-     * \return An empty string on illegal format.
+     * \return An empty string on illegal format, "0" if date is unspecified.
      **/
     std::string form( const std::string & format_r ) const
     { return form( format_r, TB_LOCALTIME ); }
@@ -121,7 +126,120 @@ namespace zypp
     std::string asSeconds() const
     { return form( "%s" ); }
 
-  private:
+  public:
+    /** \name Printing in various predefined formats */
+    //@{
+    /** Date formats for printing (use like 'enum class \ref DateFormat') */
+    struct EDateFormatDef { enum Enum {
+      none,    ///< ""
+      calendar,        ///< 2014-02-07
+      month,   ///< 2014-02
+      year,    ///< 2014
+      week,    ///< 2014-W06
+      weekday, ///< 2014-W06-5 (1 is Monday)
+      ordinal, ///< 2014-038
+    };};
+    typedef base::EnumClass<EDateFormatDef> DateFormat;        ///< 'enum class DateFormat'
+
+    /** Time formats for printing (use like 'enum class \ref TimeFormat') */
+    struct ETimeFormatDef { enum Enum {
+      none,    ///< ""
+      seconds, ///< 07:06:41
+      minutes, ///< 07:06
+      hours,   ///< 07
+    };};
+    typedef base::EnumClass<ETimeFormatDef> TimeFormat;        ///< 'enum class TimeFormat'
+
+    /** Timezone indicator for printing (use like 'enum class \ref TimeZoneFormat') */
+    struct ETimeZoneFormatDef { enum Enum {
+      none,    ///< ""
+      name,    ///< UTC, CET, ...
+      offset,  ///< +00[:00]
+    };};
+    typedef base::EnumClass<ETimeZoneFormatDef> TimeZoneFormat;        ///< 'enum class TimeZoneFormat'
+
+    /** Default format is <tt>'2014-02-07 07:06:41 CET'</tt>
+     * The default is \ref DateFormat::calendar, \ref TimeFormat::seconds, \ref TimeZoneFormat::name and
+     * \ref TB_LOCALTIME. For other formats you don't have to repeat all the defaults, just pass the
+     * values where you differ.
+     */
+    std::string print( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
+    /** \overload */
+    std::string print( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
+    { return print( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string print( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
+    { return print( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string print( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
+    { return print( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string print( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
+    { return print( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string print( TimeFormat timeFormat_r, TimeBase base_r ) const
+    { return print( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string print( DateFormat dateFormat_r, TimeBase base_r ) const
+    { return print( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string print( TimeBase base_r ) const
+    { return print( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
+
+    /** Convenience for printing the date only [<tt>'2014-02-07'</tt>]
+     * The default is \ref DateFormat::calendar and \ref TB_LOCALTIME
+     */
+    std::string printDate( DateFormat dateFormat_r = DateFormat::calendar, TimeBase base_r = TB_LOCALTIME ) const
+    { return print( dateFormat_r, TimeFormat::none, TimeZoneFormat::none, base_r ); }
+    /** \overload */
+    std::string printDate( TimeBase base_r ) const
+    { return printDate( DateFormat::calendar, base_r ); }
+
+    /** Convenience for printing the time only [<tt>'07:06:41 CET'</tt>]
+     * The default is \ref DateFormat::calendar and \ref TB_LOCALTIME
+     */
+    std::string printTime( TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
+    { return print( DateFormat::none, timeFormat_r, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string printTime( TimeZoneFormat timeZoneFormat_r , TimeBase base_r = TB_LOCALTIME ) const
+    { return printTime( TimeFormat::seconds, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string printTime( TimeFormat timeFormat_r , TimeBase base_r ) const
+    { return printTime( timeFormat_r, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string printTime( TimeBase base_r ) const
+    { return printTime( TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
+
+    /** Default ISO 8601 format is <tt>'2014-02-07T07:06:41+01'</tt>
+     * \note As timezone names are not used in ISO, \ref TimeZoneFormat::name is the same as
+     * \ref TimeZoneFormat::offset when printing in \ref TB_LOCALTIME. When printing \ref TB_UTC
+     * it uses a \c 'Z' to indicate UTC (Zulu time) rather than printing \c '+00'.
+     */
+    std::string printISO( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
+    /** \overload */
+    std::string printISO( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
+    { return printISO( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string printISO( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
+    { return printISO( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string printISO( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
+    { return printISO( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string printISO( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
+    { return printISO( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
+    /** \overload */
+    std::string printISO( TimeFormat timeFormat_r, TimeBase base_r ) const
+    { return printISO( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string printISO( DateFormat dateFormat_r, TimeBase base_r ) const
+    { return printISO( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
+    /** \overload */
+    std::string printISO( TimeBase base_r ) const
+    { return printISO( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
+    //@}
+
+ private:
     /** Calendar time.
      * The number of seconds elapsed since 00:00:00 on January 1, 1970,
      * Coordinated Universal Time (UTC).
@@ -134,6 +252,12 @@ namespace zypp
   inline std::ostream & operator<<( std::ostream & str, const Date & obj )
   { return str << obj.asString(); }
 
+  /** \relates Date XML output.
+   * Print \c time_t and \c text attribute. Allow alternate node name [date].
+   */
+  std::ostream & dumpAsXmlOn( std::ostream & str, const Date & obj, const std::string & name_r = "date" );
+
+  ///////////////////////////////////////////////////////////////////
   class DateFormatException : public Exception
   {
   public: