Serializer: string to integer conversion for MyMonero compatibility

mymonero timestamp conversion
This commit is contained in:
Jaquee 2017-07-22 10:10:21 +02:00
parent 1cf940f2a1
commit 76961ddc3e

View file

@ -28,6 +28,8 @@
#pragma once
#include <regex>
#include "misc_language.h"
#include "portable_storage_base.h"
#include "warnings.h"
@ -131,6 +133,31 @@ POP_WARNINGS
}
};
// For MyMonero/OpenMonero backend compatibility
// MyMonero backend sends amount, fees and timestamp values as strings.
// Until MM backend is updated, this is needed for compatibility between OpenMonero and MyMonero.
template<>
struct convert_to_integral<std::string, uint64_t, false>
{
static void convert(const std::string& from, uint64_t& to)
{
MTRACE("Converting std::string to uint64_t. Source: " << from);
// String only contains digits
if(std::all_of(from.begin(), from.end(), ::isdigit))
to = boost::lexical_cast<uint64_t>(from);
// MyMonero ISO 8061 timestamp (2017-05-06T16:27:06Z)
else if (std::regex_match (from, std::regex("\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\dZ")))
{
// Convert to unix timestamp
std::tm tm = {};
std::istringstream ss(from);
if (ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S"))
to = std::mktime(&tm);
} else
ASSERT_AND_THROW_WRONG_CONVERSION();
}
};
template<class from_type, class to_type>
struct is_convertable: std::integral_constant<bool,
std::is_integral<to_type>::value &&