[buildd-tools-devel] [PATCH 5/6] Add get_current_time_as_string for date strings.

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


Consolidates the Boost.Date_Time usage into one utility function.
Now also drop the special handling of the Boost.Date_Time libraries.
---
 bin/dchroot/Makefile.am               |    2 +-
 bin/dchroot/dchroot-main-base.cc      |    5 +---
 bin/schroot/Makefile.am               |    2 +-
 bin/schroot/schroot-main.cc           |    5 +---
 configure.ac                          |    5 +--
 debian/changelog                      |    3 +-
 sbuild/sbuild-chroot-facet-session.cc |   47 ++++++++++++++-------------------
 sbuild/sbuild-session.cc              |    6 +---
 sbuild/sbuild-util.cc                 |    8 +++++
 sbuild/sbuild-util.h                  |    8 +++++
 test/Makefile.am                      |    2 +-
 11 files changed, 46 insertions(+), 47 deletions(-)

diff --git a/bin/dchroot/Makefile.am b/bin/dchroot/Makefile.am
index 9a419c1..94d7ea3 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 $(BOOST_DATE_TIME_LIBS)
+libdchroot_la_LIBADD = $(top_builddir)/bin/schroot/libschroot.la
 
 dchroot_SOURCES =			\
 	dchroot-chroot-config.h		\
diff --git a/bin/dchroot/dchroot-main-base.cc b/bin/dchroot/dchroot-main-base.cc
index dbc8d3b..38ed1e6 100644
--- a/bin/dchroot/dchroot-main-base.cc
+++ b/bin/dchroot/dchroot-main-base.cc
@@ -32,14 +32,11 @@
 #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;
@@ -64,7 +61,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 % to_simple_string(second_clock::local_time())
+    % this->program_name % VERSION % sbuild::get_current_time_as_string()
 	    << endl;
   if (this->use_dchroot_conf)
     {
diff --git a/bin/schroot/Makefile.am b/bin/schroot/Makefile.am
index 5340b05..cec9396 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 $(BOOST_DATE_TIME_LIBS)
+libschroot_all_la_LIBADD = libschroot.la
 
 schroot_SOURCES =			\
 	schroot.cc
diff --git a/bin/schroot/schroot-main.cc b/bin/schroot/schroot-main.cc
index de1646b..2dd1d5a 100644
--- a/bin/schroot/schroot-main.cc
+++ b/bin/schroot/schroot-main.cc
@@ -31,12 +31,9 @@
 #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;
 
@@ -62,7 +59,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 % to_simple_string(second_clock::local_time())
+    % this->program_name % VERSION % sbuild::get_current_time_as_string()
 	    << endl;
   std::cout << endl;
   this->config->print_chroot_config(this->chroots, std::cout);
diff --git a/configure.ac b/configure.ac
index 148813e..fa26358 100644
--- a/configure.ac
+++ b/configure.ac
@@ -694,17 +694,16 @@ define([testprog], [AC_LANG_PROGRAM([#include <boost/date_time/posix_time/posix_
                                    [boost::posix_time::second_clock::local_time()])])
 AC_LINK_IFELSE(testprog,
                [AC_MSG_RESULT([yes])
-	        BOOST_DATE_TIME_LIBS="-lboost_date_time"],
+	        BOOST_LIBS="${BOOST_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"],
+	        BOOST_LIBS="${BOOST_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 99bcabc..c8eafa6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -6,11 +6,12 @@ schroot (1.4.24-1) unstable; urgency=low
     - Drop date classes from sbuild-types.h.
     - Drop sbuild-types.c.
     - Convert code to use Boost.Date_Time for all dates.
+    - Introduce get_current_time_as_string function for date strings.
   * Don't include facet-source-clonable data in sessions.
   * Include session creation and last-run date in session keyfile:
     - Update session keyfile before and after session run.
 
- -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de>  Thu, 23 Jun 2011 16:05:10 +0200
+ -- Jan-Marek Glogowski <jan-marek.glogowski at muenchen.de>  Thu, 23 Jun 2011 16:48:27 +0200
 
 schroot (1.4.23-1) unstable; urgency=low
 
diff --git a/sbuild/sbuild-chroot-facet-session.cc b/sbuild/sbuild-chroot-facet-session.cc
index 57df478..f007843 100644
--- a/sbuild/sbuild-chroot-facet-session.cc
+++ b/sbuild/sbuild-chroot-facet-session.cc
@@ -38,7 +38,6 @@
 using boost::format;
 using std::endl;
 using namespace sbuild;
-using namespace boost::posix_time;
 
 chroot_facet_session::chroot_facet_session ():
   chroot_facet(),
@@ -84,33 +83,41 @@ chroot_facet_session::set_original_name (std::string const& name)
   this->original_chroot_name = name;
 }
 
-std::string const&
-chroot_facet_session::get_creation_date () const
+static void
+set_date(std::string const& date_str, std::string& date_val)
 {
-  return this->creation_date;
-}
+  using namespace boost::posix_time;
 
-void
-chroot_facet_session::set_creation_date (std::string const& date_str)
-{
   if (date_str.empty())
     {
-      this->creation_date = date_str;
+      date_val = date_str;
       return;
     }
 
   try
     {
       ptime pt(time_from_string(date_str));
-      this->creation_date = to_simple_string(pt);
+      date_val = to_simple_string(pt);
     }
   catch(std::exception& e)
     {
-      this->creation_date = "invalid";
+      date_val = "invalid";
     }
 }
 
 std::string const&
+chroot_facet_session::get_creation_date () const
+{
+  return this->creation_date;
+}
+
+void
+chroot_facet_session::set_creation_date(std::string const& date_str)
+{
+  ::set_date(date_str, this->creation_date);
+}
+
+std::string const&
 chroot_facet_session::get_last_run_date () const
 {
   return this->last_run_date;
@@ -119,27 +126,13 @@ chroot_facet_session::get_last_run_date () const
 void
 chroot_facet_session::set_last_run_date (std::string const& date_str)
 {
-  if (date_str.empty())
-    {
-      this->last_run_date = date_str;
-      return;
-    }
-
-  try
-    {
-      ptime pt(time_from_string(date_str));
-      this->last_run_date = to_simple_string(pt);
-    }
-  catch(std::exception& e)
-    {
-      this->last_run_date = "invalid";
-    }
+  ::set_date(date_str, this->last_run_date);
 }
 
 void
 chroot_facet_session::set_last_run_date ()
 {
-  this->last_run_date = to_simple_string(second_clock::local_time());
+  this->last_run_date = get_current_time_as_string();
 }
 
 void
diff --git a/sbuild/sbuild-session.cc b/sbuild/sbuild-session.cc
index 62570f5..a4d9e5c 100644
--- a/sbuild/sbuild-session.cc
+++ b/sbuild/sbuild-session.cc
@@ -50,13 +50,10 @@
 #include <syslog.h>
 
 #include <boost/format.hpp>
-#include <boost/date_time/posix_time/posix_time.hpp>
 
 using std::cout;
 using std::endl;
 using boost::format;
-using boost::posix_time::second_clock;
-using boost::posix_time::to_simple_string;
 using namespace sbuild;
 
 namespace
@@ -666,8 +663,7 @@ session::run_impl ()
 	      assert(chroot);
 	      psess = chroot->get_facet<chroot_facet_session>();
 	      assert(psess);
-	      psess->set_creation_date(
-		to_simple_string(second_clock::local_time()));
+	      psess->set_creation_date(get_current_time_as_string());
 	    }
 	  else
 	    psess = chroot->get_facet<chroot_facet_session>();
diff --git a/sbuild/sbuild-util.cc b/sbuild/sbuild-util.cc
index 0635867..90d3010 100644
--- a/sbuild/sbuild-util.cc
+++ b/sbuild/sbuild-util.cc
@@ -36,6 +36,7 @@
 #endif
 
 #include <boost/filesystem/convenience.hpp>
+#include <boost/date_time/posix_time/posix_time.hpp>
 
 using namespace sbuild;
 
@@ -504,6 +505,13 @@ sbuild::exec (std::string const& file,
   return status;
 }
 
+std::string
+sbuild::get_current_time_as_string()
+{
+  using namespace boost::posix_time;
+  return to_simple_string(second_clock::local_time());
+}
+
 sbuild::stat::stat (const char *file):
   file(file),
   fd(0),
diff --git a/sbuild/sbuild-util.h b/sbuild/sbuild-util.h
index cbf83e2..ed048a7 100644
--- a/sbuild/sbuild-util.h
+++ b/sbuild/sbuild-util.h
@@ -323,6 +323,14 @@ namespace sbuild
 	environment const& env);
 
   /**
+   * Return the current time as a formated date string.
+   *
+   * @returns the current time as a string.
+   */
+  std::string
+  get_current_time_as_string();
+
+  /**
    * Get file status.  stat(2) wrapper.
    */
   class stat
diff --git a/test/Makefile.am b/test/Makefile.am
index 3ea76a5..003f7f2 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -76,7 +76,7 @@ endif
 libtest_la_SOURCES = 	\
 	test-helpers.h	\
 	testmain.cc
-libtest_la_LIBADD = $(top_builddir)/sbuild/libsbuild.la $(CPPUNIT_LIBS) $(BOOST_DATE_TIME_LIBS)
+libtest_la_LIBADD = $(top_builddir)/sbuild/libsbuild.la $(CPPUNIT_LIBS)
 
 if BUILD_BLOCKDEV
 sbuild_chroot_blockdev_sources =	\
-- 
1.7.2.5




More information about the Buildd-tools-devel mailing list