Skip to content

Commit 8ca10e9

Browse files
committed
Add frpc DateTime type
1 parent 515e640 commit 8ca10e9

File tree

16 files changed

+739
-113
lines changed

16 files changed

+739
-113
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ xmake install -o . --file=./test_xmake.lua
4242
| i16 | int16_t | option | std::optional |
4343
| u16 | uint16_t | string | std::string |
4444
| i32 | int32_t | map | std::unordered_map |
45-
| u32 | uint32_t | | |
46-
| i64 | int64_t | | |
47-
| u64 | uint64_t | | |
45+
| u32 | uint32_t | DateTime| frpc::DateTime |
46+
| i64 | int64_t | Date | frpc::Date |
47+
| u64 | uint64_t | Time | frpc::Time |
4848

4949

5050
## Protocol Definition

config/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ BankInfo:
2525
TestMap: { type: map<bool;u32>, comment: "test map" }
2626
TestVector: { type: vector<string>, comment: "test map" }
2727
Info: { type: Info, comment: "Info" }
28+
DateTime: { type: DateTime, comment: "date time" }
2829

2930
HelloWorldApi:
3031
type: interface
@@ -41,6 +42,7 @@ HelloWorldApi:
4142
bank_name: {type: string}
4243
blance: {type: u64}
4344
date: {type: option<string>}
45+
date_time: {type: DateTime}
4446
outputs:
4547
reply: {type: string}
4648
info: {type: Info}

out/bi_web/include/data/bank_info.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ struct BankInfo {
1212
std::unordered_map<bool, uint32_t> test_map; // test map
1313
std::vector<std::string> test_vector; // test map
1414
Info info; // Info
15+
frpc::DateTime date_time; // date time
1516

16-
MSGPACK_DEFINE(name, type, test_one, test_two, test_map_one, test_map, test_vector, info)
17+
MSGPACK_DEFINE(name, type, test_one, test_two, test_map_one, test_map, test_vector, info, date_time)
1718
};
1819

19-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(BankInfo, name, type, test_one, test_two, test_map_one, test_map, test_vector, info)
20+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(BankInfo, name, type, test_one, test_two, test_map_one, test_map, test_vector, info, date_time)
2021

2122
inline std::string toString(const BankInfo& value) {
2223
std::ostringstream ss;
@@ -28,6 +29,7 @@ inline std::string toString(const BankInfo& value) {
2829
<< "," << toString(value.test_map)
2930
<< "," << toString(value.test_vector)
3031
<< "," << toString(value.info)
32+
<< "," << toString(value.date_time)
3133
<< "}";
3234
return ss.str();
3335
}
@@ -40,7 +42,8 @@ inline bool operator==(const BankInfo& lhs, const BankInfo& rhs) {
4042
(lhs.test_map_one == rhs.test_map_one) &&
4143
(lhs.test_map == rhs.test_map) &&
4244
(lhs.test_vector == rhs.test_vector) &&
43-
(lhs.info == rhs.info);
45+
(lhs.info == rhs.info) &&
46+
(lhs.date_time == rhs.date_time);
4447
}
4548

4649
inline bool operator!=(const BankInfo& lhs, const BankInfo& rhs) {

out/bi_web/include/fantasy.hpp

Lines changed: 39 additions & 37 deletions
Large diffs are not rendered by default.

out/bi_web/include/impl/date_time.h

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#ifndef _FRPC_DATE_TIME_H_
2+
#define _FRPC_DATE_TIME_H_
3+
4+
#include <chrono>
5+
#include <cstdint>
6+
7+
namespace frpc {
8+
9+
struct Date {
10+
Date() = default;
11+
Date(uint16_t year, uint8_t month, uint8_t day)
12+
: year(year)
13+
, month(month)
14+
, day(day) {
15+
}
16+
17+
uint16_t year{};
18+
uint8_t month{};
19+
uint8_t day{};
20+
21+
MSGPACK_DEFINE(year, month, day)
22+
};
23+
24+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Date, year, month, day)
25+
26+
inline std::string toString(const Date& value) {
27+
std::ostringstream ss;
28+
ss << "Date"
29+
<< " year=" << value.year << " month=" << static_cast<uint32_t>(value.month)
30+
<< " day=" << static_cast<uint32_t>(value.day);
31+
return ss.str();
32+
}
33+
34+
inline bool operator==(const Date& lhs, const Date& rhs) {
35+
return (lhs.year == rhs.year) && (lhs.month == rhs.month) && (lhs.day == rhs.day);
36+
}
37+
38+
inline bool operator!=(const Date& lhs, const Date& rhs) {
39+
return !(lhs == rhs);
40+
}
41+
42+
struct Time {
43+
Time() = default;
44+
Time(uint8_t hour, uint8_t minute, uint8_t second, uint32_t microsecond)
45+
: hour(hour)
46+
, minute(minute)
47+
, second(second)
48+
, microsecond(microsecond) {
49+
}
50+
51+
uint8_t hour{};
52+
uint8_t minute{};
53+
uint8_t second{};
54+
uint32_t microsecond{};
55+
56+
MSGPACK_DEFINE(hour, minute, second, microsecond)
57+
};
58+
59+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Time, hour, minute, second, microsecond)
60+
61+
inline std::string toString(const Time& value) {
62+
std::ostringstream ss;
63+
ss << "Time"
64+
<< " hour=" << static_cast<uint32_t>(value.hour) << " minute=" << static_cast<uint32_t>(value.minute)
65+
<< " second=" << static_cast<uint32_t>(value.second) << " microsecond=" << value.microsecond;
66+
return ss.str();
67+
}
68+
69+
inline bool operator==(const Time& lhs, const Time& rhs) {
70+
return (lhs.hour == rhs.hour) &&
71+
(lhs.minute == rhs.minute) &&
72+
(lhs.second == rhs.second) &&
73+
(lhs.microsecond == rhs.microsecond);
74+
}
75+
76+
inline bool operator!=(const Time& lhs, const Time& rhs) {
77+
return !(lhs == rhs);
78+
}
79+
80+
struct DateTime {
81+
DateTime() = default;
82+
DateTime(const Date& date, const Time& time)
83+
: date(date)
84+
, time(time) {
85+
}
86+
87+
Date date{};
88+
Time time{};
89+
90+
MSGPACK_DEFINE(date, time)
91+
};
92+
93+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(DateTime, date, time)
94+
95+
inline std::string toString(const DateTime& value) {
96+
std::ostringstream ss;
97+
ss << "DateTime"
98+
<< " date=" << toString(value.date) << " time=" << toString(value.time);
99+
return ss.str();
100+
}
101+
102+
inline bool operator==(const DateTime& lhs, const DateTime& rhs) {
103+
return (lhs.date == rhs.date) && (lhs.time == rhs.time);
104+
}
105+
106+
inline bool operator!=(const DateTime& lhs, const DateTime& rhs) {
107+
return !(lhs == rhs);
108+
}
109+
110+
inline DateTime getFrpcDateTime(std::chrono::system_clock::time_point now_time = std::chrono::system_clock::now()) {
111+
auto mic = std::chrono::duration_cast<std::chrono::microseconds>(now_time.time_since_epoch());
112+
auto microseconds = mic.count() % 1000000;
113+
auto time_tt = std::chrono::system_clock::to_time_t(now_time);
114+
struct tm work {};
115+
localtime_r(&time_tt, &work);
116+
Date d(1900 + work.tm_year, work.tm_mon + 1, work.tm_mday);
117+
Time t(work.tm_hour, work.tm_min, work.tm_sec, microseconds);
118+
return DateTime{d, t};
119+
}
120+
121+
inline std::chrono::system_clock::time_point frpcDateTimeToTimePoint(const DateTime& date_time) {
122+
struct tm timeinfo {};
123+
timeinfo.tm_year = date_time.date.year - 1900;
124+
timeinfo.tm_mon = date_time.date.month - 1;
125+
timeinfo.tm_mday = date_time.date.day;
126+
timeinfo.tm_hour = date_time.time.hour;
127+
timeinfo.tm_min = date_time.time.minute;
128+
timeinfo.tm_sec = date_time.time.second;
129+
auto time_c = std::mktime(&timeinfo);
130+
return std::chrono::system_clock::from_time_t(time_c) + std::chrono::microseconds{date_time.time.microsecond};
131+
}
132+
133+
template <bool millisec = false>
134+
inline std::string fromFrpcDateTime(const DateTime& date_time, const std::string& format = "%Y-%m-%d %H:%M:%S") {
135+
struct tm timeinfo {};
136+
timeinfo.tm_year = date_time.date.year - 1900;
137+
timeinfo.tm_mon = date_time.date.month - 1;
138+
timeinfo.tm_mday = date_time.date.day;
139+
timeinfo.tm_hour = date_time.time.hour;
140+
timeinfo.tm_min = date_time.time.minute;
141+
timeinfo.tm_sec = date_time.time.second;
142+
std::stringstream ss;
143+
ss << std::put_time(&timeinfo, format.c_str());
144+
if constexpr (millisec) {
145+
auto millisecond = date_time.time.microsecond / 1000;
146+
ss << '.' << std::setw(3) << std::setfill('0') << millisecond;
147+
}
148+
return ss.str();
149+
}
150+
151+
inline DateTime toFrpcDateTime(const std::string& date, const std::string& format = "%Y-%m-%d %H:%M:%S.") {
152+
struct tm tm {};
153+
std::stringstream ss(date);
154+
ss >> std::get_time(&tm, format.c_str());
155+
int milliseconds = 0;
156+
ss >> milliseconds;
157+
Date d(1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday);
158+
Time t(tm.tm_hour, tm.tm_min, tm.tm_sec, milliseconds * 1000);
159+
return DateTime{d, t};
160+
}
161+
162+
} // namespace frpc
163+
164+
namespace std {
165+
166+
template <>
167+
struct hash<frpc::Date> {
168+
size_t operator()(const frpc::Date& value) const {
169+
return std::hash<uint16_t>{}(value.year) ^ std::hash<uint8_t>{}(value.month) ^ std::hash<uint8_t>{}(value.day);
170+
}
171+
};
172+
173+
template <>
174+
struct hash<frpc::Time> {
175+
size_t operator()(const frpc::Time& value) const {
176+
return std::hash<uint8_t>{}(value.hour) ^
177+
std::hash<uint8_t>{}(value.minute) ^
178+
std::hash<uint8_t>{}(value.second) ^
179+
std::hash<uint32_t>{}(value.microsecond);
180+
}
181+
};
182+
183+
template <>
184+
struct hash<frpc::DateTime> {
185+
size_t operator()(const frpc::DateTime& value) const {
186+
return std::hash<frpc::Date>{}(value.date) ^ std::hash<frpc::Time>{}(value.time);
187+
}
188+
};
189+
190+
} // namespace std
191+
192+
#endif // _FRPC_DATE_TIME_H_

out/bi_web/src/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ struct HelloWorldApi final {
2626
auto bank_name = request["bank_name"].template get<std::string>();
2727
auto blance = request["blance"].template get<uint64_t>();
2828
auto date = request["date"].template get<std::optional<std::string>>();
29+
auto date_time = request["date_time"].template get<frpc::DateTime>();
2930
static std::chrono::milliseconds timeout(9000);
3031
m_client->hello_world(
31-
std::move(bank_info), std::move(bank_name), blance, std::move(date),
32+
std::move(bank_info), std::move(bank_name), blance, std::move(date), date_time,
3233
[callback](std::string reply, Info info, uint64_t count, std::optional<std::string> date) mutable {
3334
nlohmann::json json;
3435
json["reply"] = std::move(reply);
@@ -75,6 +76,7 @@ int main(int argc, char** argv) {
7576
tmp["input"]["bank_name"] = std::string{};
7677
tmp["input"]["blance"] = uint64_t{};
7778
tmp["input"]["date"] = std::optional<std::string>{};
79+
tmp["input"]["date_time"] = frpc::DateTime{};
7880
tmp["output"]["reply"] = std::string{};
7981
tmp["output"]["info"] = Info{};
8082
tmp["output"]["count"] = uint64_t{};

out/include/data/bank_info.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ struct BankInfo {
1212
std::unordered_map<bool, uint32_t> test_map; // test map
1313
std::vector<std::string> test_vector; // test map
1414
Info info; // Info
15+
frpc::DateTime date_time; // date time
1516

16-
MSGPACK_DEFINE(name, type, test_one, test_two, test_map_one, test_map, test_vector, info)
17+
MSGPACK_DEFINE(name, type, test_one, test_two, test_map_one, test_map, test_vector, info, date_time)
1718
};
1819

19-
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(BankInfo, name, type, test_one, test_two, test_map_one, test_map, test_vector, info)
20+
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(BankInfo, name, type, test_one, test_two, test_map_one, test_map, test_vector, info, date_time)
2021

2122
inline std::string toString(const BankInfo& value) {
2223
std::ostringstream ss;
@@ -28,6 +29,7 @@ inline std::string toString(const BankInfo& value) {
2829
<< "," << toString(value.test_map)
2930
<< "," << toString(value.test_vector)
3031
<< "," << toString(value.info)
32+
<< "," << toString(value.date_time)
3133
<< "}";
3234
return ss.str();
3335
}
@@ -40,7 +42,8 @@ inline bool operator==(const BankInfo& lhs, const BankInfo& rhs) {
4042
(lhs.test_map_one == rhs.test_map_one) &&
4143
(lhs.test_map == rhs.test_map) &&
4244
(lhs.test_vector == rhs.test_vector) &&
43-
(lhs.info == rhs.info);
45+
(lhs.info == rhs.info) &&
46+
(lhs.date_time == rhs.date_time);
4447
}
4548

4649
inline bool operator!=(const BankInfo& lhs, const BankInfo& rhs) {

0 commit comments

Comments
 (0)