AnCH Framework 0.2
Another C++ Hack: C++23 utility libraries
Loading...
Searching...
No Matches
time.hpp
1/*
2 ANCH Framework: ANother C++ Hack is a C++ framework based on C++23 standard
3 Copyright (C) 2026 Vincent Lachenal
4
5 This file is part of ANCH Framework.
6
7 ANCH Framework is free software: you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 ANCH Framework is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with ANCH Framework. If not, see <https://www.gnu.org/licenses/>.
19*/
20#pragma once
21
22#include "anch-date-export.hpp"
23
24#include <chrono>
25#include <compare>
26#include <cstdint>
27#include <ostream>
28#include <string>
29#include <string_view>
30
31#include "date/date.hpp"
32
33namespace anch::date {
34
48 class ANCH_DATE_EXPORT Time {
49
50 // Attributes +
51 private:
53 std::chrono::nanoseconds _time;
54 // Attributes -
55
56 // Constructors +
57 public:
61 constexpr Time() noexcept;
62
73 Time(unsigned hour, unsigned minute, unsigned second = 0, uint32_t nanosecond = 0);
74
82 explicit Time(std::chrono::nanoseconds sinceMidnight);
83 // Constructors -
84
85 // Methods +
86 public:
95
105 static Time parse(std::string_view text);
106
112 std::string format() const;
113
119 constexpr unsigned hour() const noexcept;
120
126 constexpr unsigned minute() const noexcept;
127
133 constexpr unsigned second() const noexcept;
134
140 constexpr uint32_t nanosecond() const noexcept;
141
149 Time plus(std::chrono::nanoseconds duration) const noexcept;
150 // Methods -
151
152 // Operators +
153 public:
161 constexpr std::strong_ordering operator<=>(const Time& other) const noexcept;
162
170 constexpr bool operator==(const Time& other) const noexcept;
171
177 constexpr explicit operator std::chrono::nanoseconds() const noexcept;
178 // Operators -
179
180 };
181
192 ANCH_DATE_EXPORT std::ostream& operator<<(std::ostream& out, const anch::date::Time& time);
193
194} // anch::date
195
196namespace anch::date {
197
198 constexpr
199 Time::Time() noexcept: _time(0) {
200 // Nothing to do
201 }
202
203 constexpr unsigned
204 Time::hour() const noexcept {
205 return static_cast<unsigned>(std::chrono::duration_cast<std::chrono::hours>(_time).count());
206 }
207
208 constexpr unsigned
209 Time::minute() const noexcept {
210 return static_cast<unsigned>(std::chrono::duration_cast<std::chrono::minutes>(_time).count() % 60);
211 }
212
213 constexpr unsigned
214 Time::second() const noexcept {
215 return static_cast<unsigned>(std::chrono::duration_cast<std::chrono::seconds>(_time).count() % 60);
216 }
217
218 constexpr uint32_t
219 Time::nanosecond() const noexcept {
220 return static_cast<uint32_t>(_time.count() % 1'000'000'000);
221 }
222
223 constexpr std::strong_ordering
224 Time::operator<=>(const Time& other) const noexcept {
225 return _time <=> other._time;
226 }
227
228 constexpr bool
229 Time::operator==(const Time& other) const noexcept {
230 return _time == other._time;
231 }
232
233 constexpr
234 Time::operator std::chrono::nanoseconds() const noexcept {
235 return _time;
236 }
237
238} // anch::date
Time of day (RFC 3339 partial-time , SQL TIME , XSD xs:time without time zone).
Definition time.hpp:48
constexpr bool operator==(const Time &other) const noexcept
Definition time.hpp:229
static Time now(anch::date::Zone zone=anch::date::Zone::LOCAL)
constexpr std::strong_ordering operator<=>(const Time &other) const noexcept
Definition time.hpp:224
constexpr unsigned second() const noexcept
Definition time.hpp:214
constexpr Time() noexcept
Definition time.hpp:199
constexpr unsigned minute() const noexcept
Definition time.hpp:209
constexpr uint32_t nanosecond() const noexcept
Definition time.hpp:219
Time(unsigned hour, unsigned minute, unsigned second=0, uint32_t nanosecond=0)
static Time parse(std::string_view text)
constexpr operator std::chrono::nanoseconds() const noexcept
Definition time.hpp:234
std::string format() const
constexpr unsigned hour() const noexcept
Definition time.hpp:204
Time plus(std::chrono::nanoseconds duration) const noexcept
Time(std::chrono::nanoseconds sinceMidnight)
Dates namespace.
Zone
Time zone of conversions between days and points in time.
Definition date.hpp:42
@ LOCAL
Definition date.hpp:46
AnCH framework base namespace.
Definition app.hpp:30