|
| 1 | +// Copyright 2024 Man Group Operations Limited |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <chrono> |
| 18 | +#if defined(__cpp_lib_format) |
| 19 | +# include <format> |
| 20 | +#endif |
| 21 | + |
| 22 | +namespace sparrow |
| 23 | +{ |
| 24 | + namespace chrono |
| 25 | + { |
| 26 | + using days = std::chrono::duration<int32_t, std::ratio<86400>>; |
| 27 | + using months = std::chrono::duration<int32_t, std::ratio<2629746>>; |
| 28 | + } |
| 29 | + |
| 30 | +// We pack the structures to ensure that they are the same size on all platforms |
| 31 | +#pragma pack(push, 1) |
| 32 | + |
| 33 | + struct days_time_interval |
| 34 | + { |
| 35 | + chrono::days days; |
| 36 | + std::chrono::duration<int32_t, std::milli> time; |
| 37 | + }; |
| 38 | + |
| 39 | +#pragma pack(pop) |
| 40 | + |
| 41 | + inline bool operator==(const days_time_interval& lhs, const days_time_interval& rhs) |
| 42 | + { |
| 43 | + return lhs.days == rhs.days && lhs.time == rhs.time; |
| 44 | + } |
| 45 | + |
| 46 | +#pragma pack(push, 1) |
| 47 | + |
| 48 | + struct month_day_nanoseconds_interval |
| 49 | + { |
| 50 | + chrono::months months; |
| 51 | + chrono::days days; |
| 52 | + std::chrono::duration<int64_t, std::nano> nanoseconds; |
| 53 | + }; |
| 54 | + |
| 55 | +#pragma pack(pop) |
| 56 | + |
| 57 | + inline bool operator==(const month_day_nanoseconds_interval& lhs, const month_day_nanoseconds_interval& rhs) |
| 58 | + { |
| 59 | + return lhs.months == rhs.months && lhs.days == rhs.days && lhs.nanoseconds == rhs.nanoseconds; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +namespace std |
| 64 | +{ |
| 65 | +#if defined(__cpp_lib_format) |
| 66 | + template <> |
| 67 | + struct formatter<sparrow::days_time_interval> |
| 68 | + { |
| 69 | + constexpr auto parse(std::format_parse_context& ctx) |
| 70 | + { |
| 71 | + return ctx.begin(); // Simple implementation |
| 72 | + } |
| 73 | + |
| 74 | + auto format(const sparrow::days_time_interval& interval, std::format_context& ctx) const |
| 75 | + { |
| 76 | + std::ostringstream oss; |
| 77 | + oss << interval.days.count() << " days/" << interval.time.count() << " ms"; |
| 78 | + const std::string interval_str = oss.str(); |
| 79 | + return std::format_to(ctx.out(), "{}", interval_str); |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + template <> |
| 84 | + struct formatter<sparrow::month_day_nanoseconds_interval> |
| 85 | + { |
| 86 | + constexpr auto parse(std::format_parse_context& ctx) |
| 87 | + { |
| 88 | + return ctx.begin(); // Simple implementation |
| 89 | + } |
| 90 | + |
| 91 | + auto format(const sparrow::month_day_nanoseconds_interval& interval, std::format_context& ctx) const |
| 92 | + { |
| 93 | + std::ostringstream oss; |
| 94 | + oss << interval.months.count() << " months/" << interval.days.count() << " days/" |
| 95 | + << interval.nanoseconds.count() << " ns"; |
| 96 | + const std::string interval_str = oss.str(); |
| 97 | + return std::format_to(ctx.out(), "{}", interval_str); |
| 98 | + } |
| 99 | + }; |
| 100 | +#endif |
| 101 | +} |
0 commit comments