2014-04-18 22:27:05 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <boost/date_time/local_time/local_time.hpp>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
2014-04-26 17:19:59 +00:00
|
|
|
namespace crow
|
2014-04-18 22:27:05 +00:00
|
|
|
{
|
|
|
|
// 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()
|
|
|
|
{
|
2014-05-02 05:17:49 +00:00
|
|
|
static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") );
|
2014-04-18 22:27:05 +00:00
|
|
|
std::string result;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2014-05-02 05:17:49 +00:00
|
|
|
ss.imbue(locale_);
|
2014-04-18 22:27:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2014-05-02 05:17:49 +00:00
|
|
|
static const std::locale locale_(std::locale::classic(), new boost::local_time::local_time_facet("%a, %d %b %Y %H:%M:%S GMT") );
|
2014-04-18 22:27:05 +00:00
|
|
|
std::stringstream ss(dt);
|
2014-05-02 05:17:49 +00:00
|
|
|
ss.imbue(locale_);
|
2014-04-18 22:27:05 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|