-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomeClass.hpp
71 lines (56 loc) · 1.94 KB
/
SomeClass.hpp
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
#pragma once
#include <chrono>
#include <string>
#include <sstream>
#include <iomanip>
//using T = std::chrono::time_point<std::chrono::system_clock>;
template <class T>
class Property {
public:
explicit Property(T& r) : reference{r} {}
Property& operator=(const std::string& s) {
std::tm tm = {};
std::stringstream ss(s);
ss >> std::get_time(&tm, "%b %d %Y %H:%M:%S");
reference = std::chrono::system_clock::from_time_t(std::mktime(&tm));
return *this;
}
Property& operator=(
const std::chrono::time_point<std::chrono::system_clock>& tp) {
reference = tp;
return *this;
}
Property& operator=(const unsigned& secondsSinceEpoch) {
std::chrono::seconds duration(secondsSinceEpoch);
std::chrono::time_point<std::chrono::system_clock> dt(duration);
reference = dt;
return *this;
}
explicit operator std::string() {
const std::time_t t = std::chrono::system_clock::to_time_t(reference);
char s[1000];
struct tm* tm = std::localtime(&t);
strftime(s, 1000, "%b %d %Y %H:%M:%S", tm);
return s;
}
/*explicit*/ operator std::chrono::time_point<std::chrono::system_clock>() {
const auto secondsSinceEpoch =
std::chrono::time_point_cast<std::chrono::seconds>(reference);
return secondsSinceEpoch;
}
explicit operator unsigned() {
const auto epoch = reference.time_since_epoch();
const auto value = std::chrono::duration_cast<std::chrono::seconds>(epoch);
return value.count(); // warning C4244: 'return': conversion from '_Rep' to 'unsigned int', possible loss of data
}
private:
T& reference;
};
class SomeClass {
private:
std::chrono::time_point<std::chrono::system_clock> impl;
// static decltype(impl) fromString(const std::string&);
public:
//Property property{impl};
Property<decltype(impl)> property{impl};
};