numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
time_stamp.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <cstdint>
23#include <ctime>
24
25#include <fmt/base.h>
26#include <fmt/chrono.h>
27#include <fmt/format.h>
28
29#include "num_collect/impl/num_collect_export.h"
30
31namespace num_collect::logging {
32
38class NUM_COLLECT_EXPORT time_stamp {
39public:
47 time_stamp(std::time_t seconds, std::uint32_t nanoseconds) noexcept;
48
54 [[nodiscard]] auto seconds() const noexcept -> std::time_t;
55
61 [[nodiscard]] auto nanoseconds() const noexcept -> std::uint32_t;
62
68 [[nodiscard]] static auto now() noexcept -> time_stamp;
69
70private:
72 std::time_t seconds_;
73
75 std::uint32_t nanoseconds_;
76};
77
78} // namespace num_collect::logging
79
80namespace fmt {
81
85template <>
86struct formatter<num_collect::logging::time_stamp> {
87public:
94 constexpr auto parse(format_parse_context& context) // NOLINT
95 -> decltype(context.begin()) {
96 return context.end();
97 }
98
106 auto format( // NOLINT
107 num_collect::logging::time_stamp val, format_context& context) const {
108 const auto time_tm = fmt::gmtime(val.seconds());
109 return fmt::format_to(context.out(), FMT_STRING("{0:%FT%T}.{1:09d}"),
110 time_tm, val.nanoseconds());
111 }
112};
113
114} // namespace fmt
Class of time stamps.
Definition time_stamp.h:38
auto seconds() const noexcept -> std::time_t
Get the number of seconds from the epoch.
time_stamp(std::time_t seconds, std::uint32_t nanoseconds) noexcept
Constructor.
auto nanoseconds() const noexcept -> std::uint32_t
Get the number of nanoseconds from the time specified by seconds.
Namespace of fmt library.
Definition log_level.h:114
Namespace of num_collect source codes.
STL namespace.
auto format(num_collect::logging::time_stamp val, format_context &context) const
Format a value.
Definition time_stamp.h:106
constexpr auto parse(format_parse_context &context) -> decltype(context.begin())
Parse format specifications.
Definition time_stamp.h:94