8 struct tm time_struct {};
10 gmtime_r(&raw_time, &time_struct);
12 strftime(formatted, 22,
"%Y-%m-%dT%H:%M:%SZ", &time_struct);
17 if (rfc3339.length() != 20 || rfc3339[19] !=
'Z') {
23 bool TimeStamp::IsValid()
const {
return time_.length() != 0; }
25 bool TimeStamp::IsExpiredAt(
const TimeStamp &now)
const {
35 bool TimeStamp::operator<(
const TimeStamp &other)
const {
return IsValid() && other.IsValid() && time_ < other.time_; }
37 bool TimeStamp::operator>(
const TimeStamp &other)
const {
return (other < *
this); }
39 std::ostream &operator<<(std::ostream &os,
const TimeStamp &t) {
45 Json::Value Package::toJson() {
48 json[
"version"] = version;
52 Package Package::fromJson(
const std::string &json_str) {
53 std::istringstream jsonss(json_str);
55 Json::parseFromStream(Json::CharReaderBuilder(), jsonss, &json,
nullptr);
57 package.name = json[
"name"].asString();
58 package.version = json[
"version"].asString();
62 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
63 {ResultCode::Numeric::kOk,
"OK"},
70 {ResultCode::Numeric::kNeedCompletion,
"NEED_COMPLETION"},
71 {ResultCode::Numeric::kCustomError,
"CUSTOM_ERROR"},
72 {ResultCode::Numeric::kUnknown,
"UNKNOWN"},
75 std::string data::ResultCode::toRepr()
const {
76 std::string s = toString();
78 if (s.find(
'\"') != std::string::npos) {
79 throw std::runtime_error(
"Result code cannot contain double quotes");
82 return "\"" + s +
"\"" +
":" + std::to_string(static_cast<int>(num_code));
85 ResultCode data::ResultCode::fromRepr(
const std::string &repr) {
86 size_t quote_n = repr.find(
'"');
90 if (quote_n < repr.size() - 1) {
91 size_t end_quote_n = repr.find(
'"', quote_n + 1);
92 col_n = repr.find(
':', end_quote_n + 1);
93 s = repr.substr(quote_n + 1, end_quote_n - quote_n - 1);
96 col_n = repr.find(
':');
97 s = repr.substr(0, col_n);
100 if (col_n >= repr.size() - 1) {
104 int num = std::stoi(repr.substr(col_n + 1));
106 return ResultCode(static_cast<Numeric>(num), s);
109 Json::Value InstallationResult::toJson()
const {
111 json[
"success"] = success;
112 json[
"code"] = result_code.toString();
113 json[
"description"] = description;
117 std::ostream &operator<<(std::ostream &os,
const ResultCode &result_code) {
118 os << result_code.toRepr();