diff options
author | ipknHama <ipknhama@gmail.com> | 2014-08-08 03:53:36 +0900 |
---|---|---|
committer | ipknHama <ipknhama@gmail.com> | 2014-08-08 03:53:36 +0900 |
commit | 3018ff063acb747065adea877e3e2ac9423f5233 (patch) | |
tree | 110f1a6596242b7837c16f88b4838d6fd93dc93c /include/datetime.h | |
parent | a0c93f5b84cc11b30bc6320ac26127832ef8bf7a (diff) | |
parent | 001c66b378d200bdecc8a4a692c5a73a7d426b8f (diff) | |
download | crow-3018ff063acb747065adea877e3e2ac9423f5233.tar.gz crow-3018ff063acb747065adea877e3e2ac9423f5233.zip |
Merge branch 'directory-restructuring'
Directory structure changed.
Crow becomes a complete header-only library with http_parser_merged.h.
Provides amalgamated crow_all.h (all feature in one header file).
Diffstat (limited to 'include/datetime.h')
-rw-r--r-- | include/datetime.h | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/include/datetime.h b/include/datetime.h new file mode 100644 index 0000000..0a47379 --- /dev/null +++ b/include/datetime.h @@ -0,0 +1,77 @@ +#pragma once + +#include <string> +#include <boost/date_time/local_time/local_time.hpp> +#include <boost/filesystem.hpp> + +namespace crow +{ + // code from http://stackoverflow.com/questions/2838524/use-boost-date-time-to-parse-and-create-http-dates + class DateTime + { + public: + DateTime() + : m_dt(boost::local_time::local_sec_clock::local_time(boost::local_time::time_zone_ptr())) + { + } + DateTime(const std::string& path) + : DateTime() + { + from_file(path); + } + + // return datetime string + std::string str() + { + static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); + std::string result; + try + { + std::stringstream ss; + ss.imbue(locale_); + ss << m_dt; + result = ss.str(); + } + catch (std::exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + } + return result; + } + + // update datetime from file mod date + std::string from_file(const std::string& path) + { + try + { + boost::filesystem::path p(path); + boost::posix_time::ptime pt = boost::posix_time::from_time_t( + boost::filesystem::last_write_time(p)); + m_dt = boost::local_time::local_date_time(pt, boost::local_time::time_zone_ptr()); + } + catch (std::exception& e) + { + std::cout << "Exception: " << e.what() << std::endl; + } + return str(); + } + + // parse datetime string + void parse(const std::string& dt) + { + static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") ); + std::stringstream ss(dt); + ss.imbue(locale_); + ss >> m_dt; + } + + // boolean equal operator + friend bool operator==(const DateTime& left, const DateTime& right) + { + return (left.m_dt == right.m_dt); + } + + private: + boost::local_time::local_date_time m_dt; + }; +} |