8 std::string TimeToString(
struct tm time) {
9 std::array<char, 22> formatted{};
10 strftime(formatted.data(), 22,
"%Y-%m-%dT%H:%M:%SZ", &time);
11 return std::string(formatted.data());
18 struct tm time_struct {};
20 gmtime_r(&raw_time, &time_struct);
26 if (rfc3339.length() != 20 || rfc3339[19] !=
'Z') {
34 bool TimeStamp::IsValid()
const {
return time_.length() != 0; }
36 bool TimeStamp::IsExpiredAt(
const TimeStamp &now)
const {
46 bool TimeStamp::operator<(
const TimeStamp &other)
const {
return IsValid() && other.IsValid() && time_ < other.time_; }
48 bool TimeStamp::operator>(
const TimeStamp &other)
const {
return (other < *
this); }
50 std::ostream &operator<<(std::ostream &os,
const TimeStamp &t) {
56 Json::Value Package::toJson()
const {
59 json[
"version"] = version;
63 Package Package::fromJson(
const std::string &json_str) {
64 std::istringstream jsonss(json_str);
66 Json::parseFromStream(Json::CharReaderBuilder(), jsonss, &json,
nullptr);
68 package.name = json[
"name"].asString();
69 package.version = json[
"version"].asString();
73 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
74 {ResultCode::Numeric::kOk,
"OK"},
81 {ResultCode::Numeric::kNeedCompletion,
"NEED_COMPLETION"},
82 {ResultCode::Numeric::kCustomError,
"CUSTOM_ERROR"},
83 {ResultCode::Numeric::kUnknown,
"UNKNOWN"},
86 std::string data::ResultCode::toRepr()
const {
87 std::string s = toString();
89 if (s.find(
'\"') != std::string::npos) {
90 throw std::runtime_error(
"Result code cannot contain double quotes");
93 return "\"" + s +
"\"" +
":" + std::to_string(static_cast<int>(num_code));
96 ResultCode data::ResultCode::fromRepr(
const std::string &repr) {
97 size_t quote_n = repr.find(
'"');
101 if (quote_n < repr.size() - 1) {
102 size_t end_quote_n = repr.find(
'"', quote_n + 1);
103 col_n = repr.find(
':', end_quote_n + 1);
104 s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
107 col_n = repr.find(
':');
108 s = repr.substr(0, col_n);
111 if (col_n >= repr.size() - 1) {
115 int num = std::stoi(repr.substr(col_n + 1));
117 return ResultCode(static_cast<Numeric>(num), s);
120 Json::Value InstallationResult::toJson()
const {
122 json[
"success"] = success;
123 json[
"code"] = result_code.toString();
124 json[
"description"] = description;
128 std::ostream &operator<<(std::ostream &os,
const ResultCode &result_code) {
129 os << result_code.toRepr();