Use boost::posix_time::ptime internally for times and dates.

This commit is contained in:
Aloshi 2013-09-28 12:51:16 -05:00
parent 7bd34ec62a
commit 7db0100edd
4 changed files with 32 additions and 9 deletions

View file

@ -74,9 +74,8 @@ void GameData::incTimesPlayed()
void GameData::lastPlayedNow()
{
std::stringstream ss;
ss << std::time(nullptr);
metadata()->set("lastplayed", ss.str());
boost::posix_time::ptime time = boost::posix_time::second_clock::universal_time();
metadata()->setTime("lastplayed", time);
}
MetaDataList* GameData::metadata()

View file

@ -76,6 +76,11 @@ void MetaDataList::set(const std::string& key, const std::string& value)
mMap[key] = value;
}
void MetaDataList::setTime(const std::string& key, const boost::posix_time::ptime& time)
{
mMap[key] = boost::posix_time::to_iso_string(time);
}
const std::string& MetaDataList::get(const std::string& key) const
{
return mMap.at(key);
@ -91,9 +96,9 @@ float MetaDataList::getFloat(const std::string& key) const
return (float)atof(get(key).c_str());
}
std::time_t MetaDataList::getTime(const std::string& key) const
boost::posix_time::ptime MetaDataList::getTime(const std::string& key) const
{
return (std::time_t) atoi(get(key).c_str());
return string_to_ptime(get(key), "%Y%m%dT%H%M%S%F%q");
}
GuiComponent* MetaDataList::makeDisplay(Window* window, MetaDataType as)
@ -133,3 +138,14 @@ GuiComponent* MetaDataList::makeEditor(Window* window, MetaDataType as)
}
}
}
//util function
boost::posix_time::ptime string_to_ptime(const std::string& str, const std::string& fmt)
{
std::istringstream ss(str);
ss.imbue(std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(fmt))); //std::locale handles deleting the facet
boost::posix_time::ptime time;
ss >> time;
return time;
}

View file

@ -4,7 +4,7 @@
#include <string>
#include <map>
#include "GuiComponent.h"
#include <ctime>
#include <boost/date_time.hpp>
enum MetaDataType
{
@ -25,9 +25,11 @@ struct MetaDataDecl
std::string key;
MetaDataType type;
std::string defaultValue;
bool isStatistic;
bool isStatistic; //if true, ignore scraper values for this metadata
};
boost::posix_time::ptime string_to_ptime(const std::string& str, const std::string& fmt);
class MetaDataList
{
public:
@ -39,10 +41,12 @@ public:
MetaDataList(const std::vector<MetaDataDecl>& mdd);
void set(const std::string& key, const std::string& value);
void setTime(const std::string& key, const boost::posix_time::ptime& time);
const std::string& get(const std::string& key) const;
int getInt(const std::string& key) const;
float getFloat(const std::string& key) const;
std::time_t getTime(const std::string& key) const;
boost::posix_time::ptime getTime(const std::string& key) const;
static GuiComponent* makeDisplay(Window* window, MetaDataType as);
static GuiComponent* makeEditor(Window* window, MetaDataType as);

View file

@ -3,6 +3,7 @@
#include "../components/AsyncReqComponent.h"
#include "../Log.h"
#include "../pugiXML/pugixml.hpp"
#include "../MetaData.h"
std::shared_ptr<HttpReq> GamesDBScraper::makeHttpReq(ScraperSearchParams params)
{
@ -47,7 +48,10 @@ std::vector<MetaDataList> GamesDBScraper::parseReq(ScraperSearchParams params, s
mdl.push_back(MetaDataList(params.system->getGameMDD()));
mdl.back().set("name", game.child("GameTitle").text().get());
mdl.back().set("desc", game.child("Overview").text().get());
mdl.back().set("releasedate", game.child("ReleaseDate").text().get());
boost::posix_time::ptime rd = string_to_ptime(game.child("ReleaseDate").text().get(), "%m/%d/%Y");
mdl.back().setTime("releasedate", rd);
pugi::xml_node images = game.child("Images");
if(images)