From 1130d8ce71bf86eb5a0c5a375e74930286b98216 Mon Sep 17 00:00:00 2001 From: Jay Rainey Date: Tue, 1 Apr 2014 16:16:40 +0100 Subject: [PATCH] Added private methods to helper. Refs #9186. - These will be used in methods that write data to workspaces from catalog responses. --- .../ICat/inc/MantidICat/CatalogHelper.h | 6 ++++ .../Framework/ICat/src/CatalogHelper.cpp | 30 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogHelper.h b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogHelper.h index efa85e92b365..ebb57b081396 100644 --- a/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogHelper.h +++ b/Code/Mantid/Framework/ICat/inc/MantidICat/CatalogHelper.h @@ -49,6 +49,12 @@ namespace Mantid throw std::runtime_error(exception); } + + private: + // Convert a file size to human readable file format. + std::string bytesToString(int64_t &fileSize); + // Helper method that formats a given timestamp. + std::string formatDateTime(const time_t ×tamp, const std::string &format); }; } } diff --git a/Code/Mantid/Framework/ICat/src/CatalogHelper.cpp b/Code/Mantid/Framework/ICat/src/CatalogHelper.cpp index 84b89330c247..5286850fb1f3 100644 --- a/Code/Mantid/Framework/ICat/src/CatalogHelper.cpp +++ b/Code/Mantid/Framework/ICat/src/CatalogHelper.cpp @@ -1,9 +1,39 @@ #include "MantidICat/CatalogHelper.h" +#include "MantidKernel/DateAndTime.h" namespace Mantid { namespace ICat { + /** + * Convert a file size to human readable file format. + * @param fileSize :: The size in bytes of the file. + */ + std::string CatalogHelper::bytesToString(int64_t &fileSize) + { + const char* args[] = {"B", "KB", "MB", "GB"}; + std::vector units(args, args + 4); + unsigned order = 0; + + while (fileSize >= 1024 && order + 1 < units.size()) + { + order++; + fileSize = fileSize / 1024; + } + + return boost::lexical_cast(fileSize) + units.at(order); + } + + /** + * Formats a given timestamp to human readable datetime. + * @param timestamp :: Unix timestamp. + * @param format :: The desired format to output. + * @return string :: Formatted Unix timestamp. + */ + std::string CatalogHelper::formatDateTime(const time_t ×tamp, const std::string &format) + { + return Kernel::DateAndTime(boost::posix_time::from_time_t(timestamp)).toFormattedString(format); + } } }