File: Timestamp.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (95 lines) | stat: -rw-r--r-- 2,496 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef RFL_TIMESTAMP_HPP_
#define RFL_TIMESTAMP_HPP_

#include <ctime>
#include <iomanip>
#include <sstream>
#include <stdexcept>
#include <string>

#include "Result.hpp"
#include "internal/StringLiteral.hpp"

namespace rfl {

/// For serializing and deserializing time stamps.
template <internal::StringLiteral _format>
class Timestamp {
  constexpr static const internal::StringLiteral format_ = _format;

 public:
  using Format = rfl::Literal<_format>;

  using ReflectionType = std::string;

  Timestamp() : tm_(std::tm{}) {}

  Timestamp(const char* _str) : tm_(std::tm{}) {
    const auto r = strptime(_str, _format.str().c_str(), &tm_);
    if (r == NULL) {
      throw std::runtime_error("String '" + std::string(_str) +
                               "' did not match format '" + Format().str() +
                               "'.");
    }
  }

  Timestamp(const std::string& _str) : Timestamp(_str.c_str()) {}

  Timestamp(const std::tm& _tm) : tm_(_tm) {}

  ~Timestamp() = default;

  /// Returns a result containing the timestamp when successful or an Error
  /// otherwise.
  static Result<Timestamp> from_string(const char* _str) noexcept {
    try {
      return Timestamp(_str);
    } catch (std::exception& e) {
      return error(e.what());
    }
  }

  /// Returns a result containing the timestamp when successful or an Error
  /// otherwise.
  static Result<Timestamp> from_string(const std::string& _str) {
    return from_string(_str.c_str());
  }

  /// Necessary for the serialization to work.
  ReflectionType reflection() const {
    char outstr[200];
    strftime(outstr, 200, format_.str().c_str(), &tm_);
    return std::string(outstr);
  }

  /// Expresses the underlying timestamp as a string.
  std::string str() const { return reflection(); }

  /// Trivial accessor to the underlying time stamp.
  std::tm& tm() { return tm_; }

  /// Trivial (const) accessor to the underlying time stamp.
  const std::tm& tm() const { return tm_; }

 private:
#if defined(_MSC_VER) || defined(__MINGW32__)
  // This workaround is necessary, because strptime is not available on Windows.
  char* strptime(const char* _s, const char* _f, std::tm* _tm) {
    std::istringstream input(_s);
    input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
    input >> std::get_time(_tm, _f);
    if (input.fail()) {
      return NULL;
    }
    return (char*)(_s + input.tellg());
  }
#endif

 private:
  /// The underlying time stamp.
  std::tm tm_;
};

}  // namespace rfl

#endif