aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvatar Mike Kelly <pioto@pioto.org> 2009-10-15 11:20:56 -0400
committerAvatar Mike Kelly <pioto@pioto.org> 2009-10-15 13:48:39 -0400
commit6f41065c96a93c30d366eac1adc85cb325cb7e43 (patch)
tree19408e7933b13fe80a24f5636726357f62a92863
parenta9f59a6f152b3d326323d8afcdd8f22555f46e02 (diff)
downloadpaludis-6f41065c96a93c30d366eac1adc85cb325cb7e43.tar.gz
paludis-6f41065c96a93c30d366eac1adc85cb325cb7e43.tar.xz
Add pretty_print_time().
We're using localtime instead of gmtime, since gmtime is confusing for folks who aren't on GMT. This formats them in the same style as date(1), which is basically the same as what we were doing (the %c format), with the timezone added.
-rw-r--r--paludis/util/pretty_print.cc16
-rw-r--r--paludis/util/pretty_print.hh7
-rw-r--r--paludis/util/pretty_print_TEST.cc15
3 files changed, 36 insertions, 2 deletions
diff --git a/paludis/util/pretty_print.cc b/paludis/util/pretty_print.cc
index 7ea04f0..9fa3268 100644
--- a/paludis/util/pretty_print.cc
+++ b/paludis/util/pretty_print.cc
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2008 Mike Kelly
+ * Copyright (c) 2008, 2009 Mike Kelly
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -17,8 +17,11 @@
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <paludis/util/exception.hh>
#include <paludis/util/pretty_print.hh>
+#include <paludis/util/stringify.hh>
+#include <ctime>
#include <iomanip>
#include <sstream>
@@ -46,3 +49,14 @@ paludis::pretty_print_bytes(const long & bytes)
return val.str();
}
+std::string
+paludis::pretty_print_time(const time_t & t)
+{
+ char buf[255];
+
+ if (0 == std::strftime(buf, 255, "%a %b %d %T %Z %Y", std::localtime(&t)))
+ throw InternalError(PALUDIS_HERE, "strftime failed");
+
+ return std::string(buf);
+}
+
diff --git a/paludis/util/pretty_print.hh b/paludis/util/pretty_print.hh
index 17e097f..d45692b 100644
--- a/paludis/util/pretty_print.hh
+++ b/paludis/util/pretty_print.hh
@@ -1,7 +1,7 @@
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
- * Copyright (c) 2008 Mike Kelly
+ * Copyright (c) 2008, 2009 Mike Kelly
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
@@ -36,6 +36,11 @@ namespace paludis
*
*/
std::string pretty_print_bytes(const long & bytes) PALUDIS_VISIBLE;
+
+ /**
+ * Convert a time_t into a string with localtime.
+ */
+ std::string pretty_print_time(const time_t & t) PALUDIS_VISIBLE;
}
#endif
diff --git a/paludis/util/pretty_print_TEST.cc b/paludis/util/pretty_print_TEST.cc
index 9c16007..db8bc13 100644
--- a/paludis/util/pretty_print_TEST.cc
+++ b/paludis/util/pretty_print_TEST.cc
@@ -17,6 +17,8 @@
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#include <clocale>
+#include <cstdlib>
#include <paludis/util/pretty_print.hh>
#include <string>
#include <test/test_framework.hh>
@@ -53,5 +55,18 @@ namespace test_cases
TEST_CHECK_EQUAL(pretty_print_bytes(1537598292), "1.43 GBytes");
}
} test_case_pretty_print_bytes;
+
+ struct PrettyPrintTimeTests : TestCase
+ {
+ PrettyPrintTimeTests() : TestCase("pretty_print_time") { }
+
+ void run()
+ {
+ std::setlocale(LC_TIME, "C");
+ setenv("TZ", "America/New_York", 1);
+ TEST_CHECK_EQUAL(pretty_print_time(1234567890), "Fri Feb 13 18:31:30 EST 2009");
+ TEST_CHECK_EQUAL(pretty_print_time(1255617780), "Thu Oct 15 10:43:00 EDT 2009");
+ }
+ } test_case_pretty_print_time;
}