[buildd-tools-devel] [PATCH 2/6] Use Boost.Date_Time for date handling.

Jan-Marek Glogowski glogow at fbihome.de
Tue Jun 28 10:27:54 UTC 2011


This converts all date format handling to use Boost.Date_Time.
As the main result we can drop the internal sbuild::date class.
---
 bin/dchroot/Makefile.am               |    2 +-
 bin/dchroot/dchroot-main-base.cc      |    5 +-
 bin/schroot-base/schroot-base-main.cc |   18 +++-
 bin/schroot/Makefile.am               |    2 +-
 bin/schroot/schroot-main.cc           |    5 +-
 configure.ac                          |   18 ++++
 debian/changelog                      |    7 +-
 debian/control                        |    2 +-
 sbuild/Makefile.am                    |    1 -
 sbuild/sbuild-types.cc                |   44 --------
 sbuild/sbuild-types.h                 |  177 ---------------------------------
 11 files changed, 52 insertions(+), 229 deletions(-)
 delete mode 100644 sbuild/sbuild-types.cc

diff --git a/bin/dchroot/Makefile.am b/bin/dchroot/Makefile.am
index 94d7ea3..9a419c1 100644
--- a/bin/dchroot/Makefile.am
+++ b/bin/dchroot/Makefile.am
@@ -40,7 +40,7 @@ libdchroot_la_SOURCES =			\
 	dchroot-main-base.cc		\
 	dchroot-session-base.h		\
 	dchroot-session-base.cc
-libdchroot_la_LIBADD = $(top_builddir)/bin/schroot/libschroot.la
+libdchroot_la_LIBADD = $(top_builddir)/bin/schroot/libschroot.la $(BOOST_DATE_TIME_LIBS)
 
 dchroot_SOURCES =			\
 	dchroot-chroot-config.h		\
diff --git a/bin/dchroot/dchroot-main-base.cc b/bin/dchroot/dchroot-main-base.cc
index ec61bf3..dbc8d3b 100644
--- a/bin/dchroot/dchroot-main-base.cc
+++ b/bin/dchroot/dchroot-main-base.cc
@@ -32,11 +32,14 @@
 #include <unistd.h>
 
 #include <boost/format.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 #include <syslog.h>
 
 using std::endl;
 using boost::format;
+using boost::posix_time::second_clock;
+using boost::posix_time::to_simple_string;
 using sbuild::_;
 using schroot::options_base;
 using namespace dchroot;
@@ -61,7 +64,7 @@ main_base::action_config ()
     // TRANSLATORS: %2% = program version
     // TRANSLATORS: %3% = current date
 	    << format(_("schroot configuration generated by %1% %2% on %3%"))
-    % this->program_name % VERSION % sbuild::date(time(0))
+    % this->program_name % VERSION % to_simple_string(second_clock::local_time())
 	    << endl;
   if (this->use_dchroot_conf)
     {
diff --git a/bin/schroot-base/schroot-base-main.cc b/bin/schroot-base/schroot-base-main.cc
index d6cd0b0..eea4272 100644
--- a/bin/schroot-base/schroot-base-main.cc
+++ b/bin/schroot-base/schroot-base-main.cc
@@ -31,9 +31,13 @@
 #include <syslog.h>
 
 #include <boost/format.hpp>
+#include <boost/date_time/gregorian/gregorian.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 using std::endl;
 using boost::format;
+using boost::gregorian::date_facet;
+using boost::posix_time::from_time_t;
 using sbuild::_;
 using namespace schroot_base;
 
@@ -55,11 +59,23 @@ main::~main ()
 void
 main::action_version (std::ostream& stream)
 {
+  std::ostringstream buf;
+  // TRANSLATORS: Format string for date representation:
+  // %d = day (number, e.g. 14)
+  // %b = month (three letters, e.g. Jul)
+  // %Y = year (four digits, e.g. 2006)
+  // If required, any of the Date Time IO Format Flags may be used
+  // instead as long as the day, month and year are clearly displayed
+  // in the equivalent standard method for your locale.
+  date_facet* facet = new date_facet(_("%d %b %Y"));
+  buf.imbue(std::locale(std::cout.getloc(), facet));
+  buf << from_time_t(RELEASE_DATE).date();
+
   // TRANSLATORS: %1% = program name
   // TRANSLATORS: %2% = program version
   // TRANSLATORS: %3% = release date
   format fmt(_("%1% (Debian sbuild) %2% (%3%)\n"));
-  fmt % this->program_name % VERSION % sbuild::gmdate(RELEASE_DATE);
+  fmt % this->program_name % VERSION % buf.str();
 
   format feature("  %1$-12s %2%\n");
 
diff --git a/bin/schroot/Makefile.am b/bin/schroot/Makefile.am
index cec9396..5340b05 100644
--- a/bin/schroot/Makefile.am
+++ b/bin/schroot/Makefile.am
@@ -43,7 +43,7 @@ libschroot_la_LIBADD = $(top_builddir)/bin/schroot-base/libschroot-base.la
 libschroot_all_la_SOURCES =		\
 	schroot-options.h		\
 	schroot-options.cc
-libschroot_all_la_LIBADD = libschroot.la
+libschroot_all_la_LIBADD = libschroot.la $(BOOST_DATE_TIME_LIBS)
 
 schroot_SOURCES =			\
 	schroot.cc
diff --git a/bin/schroot/schroot-main.cc b/bin/schroot/schroot-main.cc
index f6c2965..de1646b 100644
--- a/bin/schroot/schroot-main.cc
+++ b/bin/schroot/schroot-main.cc
@@ -31,9 +31,12 @@
 #include <unistd.h>
 
 #include <boost/format.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 using std::endl;
 using boost::format;
+using boost::posix_time::second_clock;
+using boost::posix_time::to_simple_string;
 using sbuild::_;
 using namespace schroot;
 
@@ -59,7 +62,7 @@ main::action_config ()
     // TRANSLATORS: %2% = program version
     // TRANSLATORS: %3% = current date
 	    << format(_("schroot configuration generated by %1% %2% on %3%"))
-    % this->program_name % VERSION % sbuild::date(time(0))
+    % this->program_name % VERSION % to_simple_string(second_clock::local_time())
 	    << endl;
   std::cout << endl;
   this->config->print_chroot_config(this->chroots, std::cout);
diff --git a/configure.ac b/configure.ac
index 45f795c..148813e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -362,6 +362,7 @@ AC_CHECK_HEADERS([boost/tuple/tuple.hpp],, [
 AC_CHECK_HEADERS([boost/format.hpp],, [AC_MSG_ERROR([Boost.Format (Boost C++ Libraries) is not installed, but is required by schroot])])
 AC_CHECK_HEADERS([boost/program_options.hpp],, [AC_MSG_ERROR([Boost.Program_options (Boost C++ Libraries) is not installed, but is required by schroot])])
 AC_CHECK_HEADERS([boost/type_traits.hpp],, [AC_MSG_ERROR([Boost.TypeTraits (Boost C++ Libraries) is not installed, but is required by schroot])])
+AC_CHECK_HEADERS([boost/date_time.hpp],, [AC_MSG_ERROR([Boost.Date_Time (Boost C++ Libraries) is not installed, but is required by schroot])])
 
 AC_CHECK_HEADERS([ext/stdio_filebuf.h],, [AC_MSG_ERROR([__gnu_cxx::stdio_filebuf (GNU libstdc++) is not installed, but is required by schroot])])
 
@@ -686,7 +687,24 @@ AC_LINK_IFELSE(testprog,
 	        AC_MSG_FAILURE([libboost_filesystem (Boost C++ Libraries) is not installed, but is required by schroot])])])
 LIBS="${saved_LIBS}"
 
+AC_MSG_CHECKING([for boost::date_time::posix_time in -lboost_date_time])
+saved_LIBS="${LIBS}"
+LIBS="${saved_LIBS} -lboost_date_time"
+define([testprog], [AC_LANG_PROGRAM([#include <boost/date_time/posix_time/posix_time.hpp>],
+                                   [boost::posix_time::second_clock::local_time()])])
+AC_LINK_IFELSE(testprog,
+               [AC_MSG_RESULT([yes])
+	        BOOST_DATE_TIME_LIBS="-lboost_date_time"],
+[LIBS="${saved_LIBS} -lboost_date_time-mt"
+ AC_LINK_IFELSE(testprog,
+               [AC_MSG_RESULT([yes])
+	        BOOST_DATE_TIME_LIBS="-lboost_date_time-mt"],
+               [AC_MSG_RESULT([no])
+	        AC_MSG_FAILURE([libboost_date_time (Boost C++ Libraries) is not installed, but is required by schroot])])])
+LIBS="${saved_LIBS}"
+
 AC_SUBST([BOOST_LIBS])
+AC_SUBST([BOOST_DATE_TIME_LIBS])
 
 AC_MSG_CHECKING([for __gnu_cxx::stdio_filebuf in libstdc++])
 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <ext/stdio_filebuf.h>
diff --git a/debian/changelog b/debian/changelog
index c68335b..3f9e82a 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,13 @@
 schroot (1.4.24-1) unstable; urgency=low
 
   * Drop special handling of Boost.Filesystem libs.
+  * Use Boost.Date_Time for date handling:
+    - Build-Depend on libboost-date-time1.46-dev.
+    - Drop date classes from sbuild-types.h.
+    - Drop sbuild-types.c.
+    - Convert code to use Boost.Date_Time for all dates.
 
- -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de>  Thu, 23 Jun 2011 14:35:25 +0200
+ -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de>  Thu, 23 Jun 2011 15:07:46 +0200
 
 schroot (1.4.23-1) unstable; urgency=low
 
diff --git a/debian/control b/debian/control
index 90bdd13..53d001a 100644
--- a/debian/control
+++ b/debian/control
@@ -3,7 +3,7 @@ Section: admin
 Priority: optional
 Maintainer: Debian buildd-tools Developers <buildd-tools-devel at lists.alioth.debian.org>
 Uploaders: Roger Leigh <rleigh at debian.org>
-Build-Depends: debhelper (>= 7.0.0), autotools-dev, pkg-config, libpam0g-dev, uuid-dev [!kfreebsd-any], liblockdev1-dev, libboost1.46-dev, libboost-program-options1.46-dev, libboost-regex1.46-dev, libboost-filesystem1.46-dev, gettext, libcppunit-dev
+Build-Depends: debhelper (>= 7.0.0), autotools-dev, pkg-config, libpam0g-dev, uuid-dev [!kfreebsd-any], liblockdev1-dev, libboost1.46-dev, libboost-program-options1.46-dev, libboost-regex1.46-dev, libboost-filesystem1.46-dev, libboost-date-time1.46-dev, gettext, libcppunit-dev
 Build-Depends-Indep: doxygen, graphviz
 Standards-Version: 3.9.1
 Vcs-Browser: http://git.debian.org/?p=buildd-tools/schroot.git
diff --git a/sbuild/Makefile.am b/sbuild/Makefile.am
index b0dcae8..6abb725 100644
--- a/sbuild/Makefile.am
+++ b/sbuild/Makefile.am
@@ -132,7 +132,6 @@ sbuild_public_cc_sources =			\
 	sbuild-personality.cc			\
 	sbuild-run-parts.cc			\
 	sbuild-session.cc			\
-	sbuild-types.cc				\
 	sbuild-util.cc
 
 if BUILD_PAM
diff --git a/sbuild/sbuild-types.cc b/sbuild/sbuild-types.cc
deleted file mode 100644
index 97abaca..0000000
--- a/sbuild/sbuild-types.cc
+++ /dev/null
@@ -1,44 +0,0 @@
-/* Copyright © 2006-2007  Roger Leigh <rleigh at debian.org>
- *
- * schroot is free software: you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * schroot is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see
- * <http://www.gnu.org/licenses/>.
- *
- *********************************************************************/
-
-#include <config.h>
-
-#include "sbuild-types.h"
-#include <sbuild/sbuild-i18n.h>
-
-using namespace sbuild;
-
-const char *
-sbuild::date_base::get_date_format () const
-{
-  // TRANSLATORS: Format string for date representation:
-  // %d = day (number, e.g. 14)
-  // %b = month (three letters, e.g. Jul)
-  // %Y = year (four digits, e.g. 2006)
-  // If required, any of the standard strftime(3)
-  // format specifiers may be used instead, as long as
-  // the day, month and year are clearly displayed in
-  // the equivalent standard method for your locale.
-  return _("%d %b %Y");
-}
-
-const char *
-sbuild::isodate::get_date_format () const
-{
-  return "%Y-%m-%dT%H:%M:%SZ";
-}
diff --git a/sbuild/sbuild-types.h b/sbuild/sbuild-types.h
index 26a5141..6419421 100644
--- a/sbuild/sbuild-types.h
+++ b/sbuild/sbuild-types.h
@@ -39,183 +39,6 @@ namespace sbuild
   /// A string set.
   typedef std::set<std::string> string_set;
 
-  /**
-   * A date representation.
-   */
-  class date_base
-  {
-  public:
-    /// Function pointer to split time into a std::tm.
-    typedef std::tm *(*break_time_func)(const time_t *timep, std:: tm *result);
-
-    /**
-     * The constructor.
-     *
-     * @param unix_time the time.
-     * @param break_time the function to split up the time.
-     */
-    date_base (time_t          unix_time,
-	       break_time_func break_time):
-      unix_time(unix_time),
-      break_time(break_time)
-    {}
-
-    /// The destructor.
-    virtual ~date_base ()
-    {}
-
-    /**
-     * Output the date to an ostream.
-     *
-     * @param stream the stream to output to.
-     * @param dt the date to output.
-     * @returns the stream.
-     */
-    template <class charT, class traits>
-    friend
-    std::basic_ostream<charT,traits>&
-    operator << (std::basic_ostream<charT,traits>& stream,
-		 date_base const&                  dt)
-    {
-      std::ios_base::iostate err = std::ios_base::goodbit;
-
-      std::tm dtm;
-      if ((dt.break_time(&dt.unix_time, &dtm)) == 0)
-	{
-	  err = std::ios_base::badbit;
-	}
-      else
-	{
-	  try
-	    {
-	      typename std::basic_ostream<charT, traits>::sentry sentry(stream);
-	      if (sentry)
-		{
-		  const std::basic_string<char>
-		    nfmt(dt.get_date_format());
-		  std::basic_string<charT> wfmt(nfmt.size(), 0);
-		  assert(nfmt.size() == wfmt.size());
-		  const char *nptr = nfmt.c_str();
-		  charT *wptr = const_cast<charT *>(wfmt.c_str());
-
-		  std::use_facet<std::ctype<charT> >(stream.getloc())
-		    .widen(nptr, nptr + nfmt.size(), wptr);
-
-		  typedef std::time_put<charT,std::ostreambuf_iterator<charT,traits> >
-		    time_type;
-		  if (std::use_facet<time_type>(stream.getloc())
-		      .put(stream, stream, stream.fill(),
-			   &dtm,
-			   wptr, wptr + wfmt.size())
-		      .failed())
-		    {
-		      err = std::ios_base::badbit;
-		    }
-		  stream.width(0);
-		}
-	    }
-	  catch (...)
-	    {
-	      bool flag = false;
-	      try
-		{
-		  stream.setstate(std::ios::failbit);
-		}
-	      catch (std::ios_base::failure const& discard)
-		{
-		  flag = true;
-		}
-	      if (flag)
-		throw;
-	    }
-	}
-
-      if (err)
-	stream.setstate(err);
-
-      return stream;
-    }
-
-  private:
-    /**
-     * Get the date formatting string.  This is used for output with
-     * the locale std::time_put facet.
-     *
-     * @returns a localised format string.
-     */
-    virtual const char *
-    get_date_format () const;
-
-    /// The time.
-    time_t          unix_time;
-    /// The function to split up the time.
-    break_time_func break_time;
-  };
-
-  /**
-   * A date representation in UTC.
-   */
-  class gmdate : public date_base
-  {
-  public:
-    /**
-     * The constructor.
-     *
-     * @param unix_time the time in UTC.
-     */
-    gmdate (time_t          unix_time):
-      date_base(unix_time, gmtime_r)
-    {}
-
-    /// The destructor.
-    virtual ~gmdate ()
-    {}
-  };
-
-  /**
-   * A date representation in local time.
-   */
-  class date : public date_base
-  {
-  public:
-    /**
-     * The constructor.
-     *
-     * @param unix_time the time in the local timezone.
-     */
-    date (time_t           unix_time):
-      date_base(unix_time, localtime_r)
-    {}
-
-    /// The destructor.
-    virtual ~date ()
-    {}
-  };
-
-  /**
-   * A date representation in ISO-8601 format.
-   */
-  class isodate : public date_base
-  {
-  public:
-    /**
-     * The constructor.
-     *
-     * @param unix_time the time in UTC.
-     */
-    isodate (time_t        unix_time):
-      date_base(unix_time, gmtime_r)
-    {}
-
-    /// The destructor.
-    virtual ~isodate ()
-    {}
-
-  private:
-    virtual const char *
-    get_date_format () const;
-  };
-
 }
 
 #endif /* SBUILD_TYPES_H */
-- 
1.7.2.5




More information about the Buildd-tools-devel mailing list