From d2cfd3eeeb8b6af3b7ccda01e6c1ac581a2df398 Mon Sep 17 00:00:00 2001
From: Joel Grunbaum <joelgrun@gmail.com>
Date: Thu, 13 Jan 2022 06:54:09 +0000
Subject: [PATCH] Added time parsing

---
 json.cpp |   57 ++++++++++++++++++++++++++++++++++-----------------------
 1 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/json.cpp b/json.cpp
index dfcbbf4..a1b0a7a 100644
--- a/json.cpp
+++ b/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(),
@@ -279,9 +290,9 @@
 {
 	if (mapOrderSide.empty()) initialise();
 	return "{\"type\": \"ADD\", \"product\": \"" + this->product +
-		   "\", \"price\": " + std::to_string(this->price) + ", \"side\": \"" +
-		   mapOrderSide[this->side] +
-		   "\", \"volume\": " + std::to_string(this->volume) + "}";
+	       "\", \"price\": " + std::to_string(this->price) + ", \"side\": \"" +
+	       mapOrderSide[this->side] +
+	       "\", \"volume\": " + std::to_string(this->volume) + "}";
 }
 
 AddedMessage::AddedMessage(MessageTypes type, std::string product,
@@ -296,13 +307,13 @@
 std::string AddedMessage::as_string()
 {
 	return "{\"type\": \"ADDED\", \"product\": \"" + this->product +
-		   "\", \"product\": \"" + this->id + "\" \"side\": \"" +
-		   mapOrderSide[this->side] +
-		   "\", \"price\": " + std::to_string(this->price) +
-		   "\"filled\": " + std::to_string(this->filled) +
-		   ", \"resting\": " + std::to_string(this->resting) +
-		   ", \"sequence\": " + std::to_string(this->sequence) +
-		   ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
+	       "\", \"product\": \"" + this->id + "\" \"side\": \"" +
+	       mapOrderSide[this->side] +
+	       "\", \"price\": " + std::to_string(this->price) +
+	       "\"filled\": " + std::to_string(this->filled) +
+	       ", \"resting\": " + std::to_string(this->resting) +
+	       ", \"sequence\": " + std::to_string(this->sequence) +
+	       ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
 }
 
 DeleteMessage::DeleteMessage(MessageTypes type, std::string product,
@@ -315,7 +326,7 @@
 {
 	if (mapOrderSide.empty()) initialise();
 	return "{\"type\": \"DELETE\", \"product\": \"" + this->product +
-		   "\", \"id\": \"" + this->id + "\"}";
+	       "\", \"id\": \"" + this->id + "\"}";
 }
 
 DeletedMessage::DeletedMessage(MessageTypes type, std::string product,
@@ -328,10 +339,10 @@
 std::string DeletedMessage::as_string()
 {
 	return "{\"type\": \"DELETED\", \"product\": \"" + this->product +
-		   "\", \"product\": \"" + this->id + "\" \"side\": \"" +
-		   mapOrderSide[this->side] +
-		   ", \"sequence\": " + std::to_string(this->sequence) +
-		   ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
+	       "\", \"product\": \"" + this->id + "\" \"side\": \"" +
+	       mapOrderSide[this->side] +
+	       ", \"sequence\": " + std::to_string(this->sequence) +
+	       ", \"timestamp\":" + std::to_string(this->timestamp) + "}";
 }
 
 RejectMessage::RejectMessage(MessageTypes type, std::string product,
@@ -344,9 +355,9 @@
 std::string RejectMessage::as_string()
 {
 	return "{\"type\": \"REJECT\", \"product\": \"" + this->product =
-			   "\", \"error\": \"" + this->error +
-			   "\", \"sequence\": " + std::to_string(this->sequence) +
-			   ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
+	           "\", \"error\": \"" + this->error +
+	           "\", \"sequence\": " + std::to_string(this->sequence) +
+	           ", \"timestamp\": " + std::to_string(this->timestamp) + "}";
 }
 
 TradeMessage::TradeMessage(MessageTypes type, std::string product, double price,

--
Gitblit v1.9.3