|
| 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 | +#include <cstdint> |
| 19 | + |
| 20 | +#if defined(__cpp_lib_format) |
| 21 | +# include <format> |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace sparrow::chrono |
| 25 | +{ |
| 26 | + /** |
| 27 | + * A duration representing time elapsed since midnight. |
| 28 | + */ |
| 29 | + struct time_seconds : public std::chrono::duration<int32_t> |
| 30 | + { |
| 31 | + time_seconds() = default; |
| 32 | + |
| 33 | + explicit time_seconds(int32_t seconds) |
| 34 | + : std::chrono::duration<int32_t>(seconds) |
| 35 | + { |
| 36 | + } |
| 37 | + }; |
| 38 | + |
| 39 | + /** |
| 40 | + * A duration representing time elapsed since midnight, in milliseconds. |
| 41 | + */ |
| 42 | + struct time_milliseconds : public std::chrono::duration<int32_t, std::milli> |
| 43 | + { |
| 44 | + time_milliseconds() = default; |
| 45 | + |
| 46 | + explicit time_milliseconds(int32_t milliseconds) |
| 47 | + : std::chrono::duration<int32_t, std::milli>(milliseconds) |
| 48 | + { |
| 49 | + } |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * A duration representing time elapsed since midnight, in microseconds. |
| 54 | + */ |
| 55 | + struct time_microseconds : public std::chrono::duration<int64_t, std::micro> |
| 56 | + { |
| 57 | + time_microseconds() = default; |
| 58 | + |
| 59 | + explicit time_microseconds(int64_t microseconds) |
| 60 | + : std::chrono::duration<int64_t, std::micro>(microseconds) |
| 61 | + { |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + /** |
| 66 | + * A duration representing time elapsed since midnight, in nanoseconds. |
| 67 | + */ |
| 68 | + struct time_nanoseconds : public std::chrono::duration<int64_t, std::nano> |
| 69 | + { |
| 70 | + time_nanoseconds() = default; |
| 71 | + |
| 72 | + explicit time_nanoseconds(int64_t nanoseconds) |
| 73 | + : std::chrono::duration<int64_t, std::nano>(nanoseconds) |
| 74 | + { |
| 75 | + } |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +#if defined(__cpp_lib_format) |
| 80 | + |
| 81 | +template <typename T> |
| 82 | + requires std::same_as<T, sparrow::chrono::time_seconds> |
| 83 | + || std::same_as<T, sparrow::chrono::time_milliseconds> |
| 84 | + || std::same_as<T, sparrow::chrono::time_microseconds> |
| 85 | + || std::same_as<T, sparrow::chrono::time_nanoseconds> |
| 86 | +struct std::formatter<T> |
| 87 | +{ |
| 88 | + constexpr auto parse(std::format_parse_context& ctx) |
| 89 | + { |
| 90 | + return ctx.begin(); // Simple implementation |
| 91 | + } |
| 92 | + |
| 93 | + auto format(const T& time, std::format_context& ctx) const |
| 94 | + { |
| 95 | + return std::format_to(ctx.out(), "{}", time.count()); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +#endif |
0 commit comments