Joel Grunbaum
2022-01-13 d2cfd3eeeb8b6af3b7ccda01e6c1ac581a2df398
Added time parsing
2 files modified
23 ■■■■ changed files
json.cpp 21 ●●●● patch | view | raw | blame | history
test.cpp 2 ●●● patch | view | raw | blame | history
json.cpp
@@ -66,6 +66,8 @@
BrokerConfirm* brokerCon(rapidjson::Value& d);
ErrorMessage* error(rapidjson::Value& d);
std::chrono::seconds parseTime(std::string& s);
std::queue<Message*> parse(std::string& str)
{
    std::queue<Message*> out;
@@ -126,12 +128,21 @@
    return out;
}
std::chrono::seconds parseTime(std::string& s)
{
    return std::chrono::hours{std::stoi(s.substr(0, 4)) * 24 * 30 * 12} +
           std::chrono::hours{std::stoi(s.substr(5, 2)) * 24 * 30} +
           std::chrono::hours{std::stoi(s.substr(8, 2)) * 24} +
           std::chrono::hours{std::stoi(s.substr(11, 2))} +
           std::chrono::minutes{std::stoi(s.substr(14, 2))} +
           std::chrono::hours{std::stoi(s.substr(16, 5))};
}
AnnounceMessage* announce(rapidjson::Value& d)
{
    // std::stringstream expiryStream(d["expiry"].GetString());
    std::string es = d["expiry"].GetString();
    std::chrono::nanoseconds exp_time(0);
    // expiryStream >>
    // date::parse("%Y-%m-%f %H:%M%z", exp_time); // Parsing is broken
    exp_time = parseTime(es);
    return new AnnounceMessage(
        mapTypes[d["type"].GetString()], d["product"].GetString(),
        d["stationId"].GetInt(), d["stationName"].GetString(),
@@ -142,9 +153,9 @@
SettleMessage* settle(rapidjson::Value& d)
{
    // std::stringstream expiryStream(d["expiry"].GetString());
    std::chrono::nanoseconds exp_time(0);
    // expiryStream >> date::parse("%Y-%m-%d %H:%M%z", exp_time);
    std::string es = d["expiry"].GetString();
    exp_time = parseTime(es);
    return new SettleMessage(
        mapTypes[d["type"].GetString()], d["product"].GetString(),
        d["stationName"].GetString(), exp_time, d["price"].GetDouble(),
test.cpp
@@ -23,7 +23,7 @@
    protocol::catchUp(bs);
    std::cout << bs.size() << std::endl;
    for (auto& i : bs) {
        std::cout << i.first << ", " << i.second.bomPrice << std::endl;
        std::cout << i.first << ", " << i.second.expiry.count() << ", " << i.second.bomPrice << std::endl;
        i.second.printBook();
    }
}