Aktualizr
C++ SOTA Client
types.cc
1 #include "utilities/types.h"
2 
3 #include <stdexcept>
4 #include <utility>
5 
6 TimeStamp TimeStamp::Now() {
7  time_t raw_time;
8  struct tm time_struct {};
9  time(&raw_time);
10  gmtime_r(&raw_time, &time_struct);
11  char formatted[22];
12  strftime(formatted, 22, "%Y-%m-%dT%H:%M:%SZ", &time_struct);
13  return TimeStamp(formatted);
14 }
15 
16 TimeStamp::TimeStamp(std::string rfc3339) {
17  if (rfc3339.length() != 20 || rfc3339[19] != 'Z') {
19  }
20  time_ = rfc3339;
21 }
22 
23 bool TimeStamp::IsValid() const { return time_.length() != 0; }
24 
25 bool TimeStamp::IsExpiredAt(const TimeStamp &now) const {
26  if (!IsValid()) {
27  return true;
28  }
29  if (!now.IsValid()) {
30  return true;
31  }
32  return *this < now;
33 }
34 
35 bool TimeStamp::operator<(const TimeStamp &other) const { return IsValid() && other.IsValid() && time_ < other.time_; }
36 
37 bool TimeStamp::operator>(const TimeStamp &other) const { return (other < *this); }
38 
39 std::ostream &operator<<(std::ostream &os, const TimeStamp &t) {
40  os << t.time_;
41  return os;
42 }
43 
44 namespace data {
45 Json::Value Package::toJson() {
46  Json::Value json;
47  json["name"] = name;
48  json["version"] = version;
49  return json;
50 }
51 
52 Package Package::fromJson(const std::string &json_str) {
53  Json::Reader reader;
54  Json::Value json;
55  reader.parse(json_str, json);
56  Package package;
57  package.name = json["name"].asString();
58  package.version = json["version"].asString();
59  return package;
60 }
61 
62 const std::map<data::ResultCode::Numeric, const char *> data::ResultCode::string_repr{
63  {ResultCode::Numeric::kOk, "OK"},
64  {ResultCode::Numeric::kAlreadyProcessed, "ALREADY_PROCESSED"},
65  {ResultCode::Numeric::kValidationFailed, "VALIDATION_FAILED"},
66  {ResultCode::Numeric::kInstallFailed, "INSTALL_FAILED"},
67  {ResultCode::Numeric::kInternalError, "INTERNAL_ERROR"},
68  {ResultCode::Numeric::kGeneralError, "GENERAL_ERROR"},
69  {ResultCode::Numeric::kNeedCompletion, "NEED_COMPLETION"},
70  {ResultCode::Numeric::kCustomError, "CUSTOM_ERROR"},
71  {ResultCode::Numeric::kUnknown, "UNKNOWN"},
72 };
73 
74 std::string data::ResultCode::toRepr() const {
75  std::string s = toString();
76 
77  return s + ":" + std::to_string(static_cast<int>(num_code));
78 }
79 
80 ResultCode data::ResultCode::fromRepr(const std::string &repr) {
81  size_t n = repr.find(':');
82  std::string s = repr.substr(0, n);
83 
84  if (n >= repr.size() - 1) {
85  return ResultCode(Numeric::kUnknown, s);
86  }
87 
88  int num = std::stoi(repr.substr(n + 1));
89 
90  return ResultCode(static_cast<Numeric>(num), s);
91 }
92 
93 Json::Value InstallationResult::toJson() const {
94  Json::Value json;
95  json["success"] = success;
96  json["code"] = result_code.toString();
97  json["description"] = description;
98  return json;
99 }
100 
101 std::ostream &operator<<(std::ostream &os, const ResultCode &result_code) {
102  os << result_code.toRepr();
103  return os;
104 }
105 
106 } // namespace data
107 
108 // vim: set tabstop=2 shiftwidth=2 expandtab:
TimeStamp()
An invalid TimeStamp.
Definition: types.h:86
General data structures.
Definition: types.cc:44