commit a14d3cdd43ce840752e94a88dab60ce4fbdf0e92
Author: Ján Kupec
Date: Thu Jan 22 18:10:59 2009 +0100
Date(str,format) added.
---
zypp/Date.cc | 10 ++++++++++
zypp/Date.h | 17 +++++++++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/zypp/Date.cc b/zypp/Date.cc
index d5f2240..483bafd 100644
--- a/zypp/Date.cc
+++ b/zypp/Date.cc
@@ -30,6 +30,16 @@ namespace zypp
Date::Date( const std::string & seconds_r )
{ str::strtonum( seconds_r, _date ); }
+ Date::Date( const std::string & date_str, const std::string & format )
+ {
+ struct tm tm;
+ if ( ::strptime( date_str.c_str(), format.c_str(), &tm ) != NULL )
+ _date = ::timelocal( &tm );
+ else
+ throw DateFormatException(
+ str::form( "Invalid date format: '%s'", date_str.c_str() ) );
+ }
+
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : Date::form
diff --git a/zypp/Date.h b/zypp/Date.h
index 60bb906..a8b5314 100644
--- a/zypp/Date.h
+++ b/zypp/Date.h
@@ -16,6 +16,8 @@
#include <iosfwd>
#include <string>
+#include "zypp/base/Exception.h"
+
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
@@ -45,6 +47,14 @@ namespace zypp
/** Ctor taking time_t value as string. */
Date( const std::string & seconds_r );
+ /**
+ * Ctor from a \a date_str formatted using \a format.
+ *
+ * \throws DateFormatException in case \a date_str cannot be
+ * parsed according to \a format.
+ */
+ Date( const std::string & date_str, const std::string & format);
+
/** Return the current time. */
static Date now()
{ return ::time( 0 ); }
@@ -104,6 +114,13 @@ namespace zypp
inline std::ostream & operator<<( std::ostream & str, const Date & obj )
{ return str << obj.asString(); }
+ class DateFormatException : public Exception
+ {
+ public:
+ DateFormatException( const std::string & msg ) : Exception( msg )
+ {}
+ };
+
/////////////////////////////////////////////////////////////////
} // namespace zypp
///////////////////////////////////////////////////////////////////
--
To unsubscribe, e-mail: zypp-commit+unsubscribe@opensuse.org
For additional commands, e-mail: zypp-commit+help@opensuse.org