nfx-datetime 0.1.1
Cross-platform C++ DateTime library with 100-nanosecond precision and ISO 8601 support
Loading...
Searching...
No Matches
TimeSpan.h
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 nfx
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
99
100#pragma once
101
102#include <chrono>
103#include <compare>
104#include <cstdint>
105#include <format>
106#include <optional>
107#include <string>
108#include <string_view>
109
110namespace nfx::time
111{
112 //=====================================================================
113 // TimeSpan class
114 //=====================================================================
115
135 class TimeSpan final
136 {
137 public:
138 //----------------------------------------------
139 // Construction
140 //----------------------------------------------
141
146 explicit inline constexpr TimeSpan( std::int64_t ticks = 0 ) noexcept;
147
152 explicit inline constexpr TimeSpan( std::chrono::system_clock::duration duration ) noexcept;
153
158 explicit TimeSpan( std::string_view iso8601String );
159
161 TimeSpan( const TimeSpan& ) = default;
162
164 TimeSpan( TimeSpan&& ) noexcept = default;
165
166 //----------------------------------------------
167 // Destruction
168 //----------------------------------------------
169
171 ~TimeSpan() = default;
172
173 //----------------------------------------------
174 // Assignment
175 //----------------------------------------------
176
181 TimeSpan& operator=( const TimeSpan& ) = default;
182
187 TimeSpan& operator=( TimeSpan&& ) noexcept = default;
188
189 //----------------------------------------------
190 // Comparison operators
191 //----------------------------------------------
192
198 inline constexpr std::strong_ordering operator<=>( const TimeSpan& other ) const noexcept;
199
205 inline constexpr bool operator==( const TimeSpan& other ) const noexcept;
206
207 //----------------------------------------------
208 // Arithmetic operators
209 //----------------------------------------------
210
216 inline constexpr TimeSpan operator+( const TimeSpan& other ) const noexcept;
217
223 inline constexpr TimeSpan operator-( const TimeSpan& other ) const noexcept;
224
229 inline constexpr TimeSpan operator-() const noexcept;
230
236 inline constexpr TimeSpan& operator+=( const TimeSpan& other ) noexcept;
237
243 inline constexpr TimeSpan& operator-=( const TimeSpan& other ) noexcept;
244
251 inline TimeSpan operator*( double multiplier ) const noexcept;
252
259 inline TimeSpan operator/( double divisor ) const noexcept;
260
266 inline double operator/( const TimeSpan& other ) const noexcept;
267
275 friend inline TimeSpan operator*( double multiplier, const TimeSpan& ts ) noexcept;
276
277 //----------------------------------------------
278 // Property accessors
279 //----------------------------------------------
280
286 [[nodiscard]] inline double days() const noexcept;
287
293 [[nodiscard]] inline double hours() const noexcept;
294
300 [[nodiscard]] inline double minutes() const noexcept;
301
307 [[nodiscard]] inline double seconds() const noexcept;
308
314 [[nodiscard]] inline double milliseconds() const noexcept;
315
321 [[nodiscard]] inline double microseconds() const noexcept;
322
328 [[nodiscard]] inline double nanoseconds() const noexcept;
329
335 [[nodiscard]] inline constexpr std::int64_t ticks() const noexcept;
336
337 //----------------------------------------------
338 // String formatting
339 //----------------------------------------------
340
346 [[nodiscard]] std::string toString() const;
347
348 //----------------------------------------------
349 // String parsing
350 //----------------------------------------------
351
367 [[nodiscard]] static bool fromString( std::string_view iso8601DurationString, TimeSpan& result ) noexcept;
368
375 [[nodiscard]] static std::optional<TimeSpan> fromString( std::string_view iso8601DurationString ) noexcept;
376
377 //----------------------------------------------
378 // std::chrono interoperability
379 //----------------------------------------------
380
389 [[nodiscard]] std::chrono::system_clock::duration toChrono() const noexcept;
390
400 [[nodiscard]] static TimeSpan fromChrono( const std::chrono::system_clock::duration& duration ) noexcept;
401
402 //----------------------------------------------
403 // Static factory methods
404 //----------------------------------------------
405
412 [[nodiscard]] inline static constexpr TimeSpan fromDays( double days ) noexcept;
413
420 [[nodiscard]] inline static constexpr TimeSpan fromHours( double hours ) noexcept;
421
428 [[nodiscard]] inline static constexpr TimeSpan fromMinutes( double minutes ) noexcept;
429
436 [[nodiscard]] inline static constexpr TimeSpan fromSeconds( double seconds ) noexcept;
437
444 [[nodiscard]] inline static constexpr TimeSpan fromMilliseconds( double milliseconds ) noexcept;
445
452 [[nodiscard]] inline static constexpr TimeSpan fromMicroseconds( double microseconds ) noexcept;
453
460 [[nodiscard]] inline static constexpr TimeSpan fromTicks( double ticks ) noexcept;
461
462 private:
464 std::int64_t m_ticks;
465 };
466} // namespace nfx::time
467
468#include "nfx/detail/datetime/TimeSpan.inl"
double minutes() const noexcept
Get total minutes.
static constexpr TimeSpan fromSeconds(double seconds) noexcept
Create a TimeSpan from a number of seconds.
static constexpr TimeSpan fromHours(double hours) noexcept
Create a TimeSpan from a number of hours.
constexpr std::int64_t ticks() const noexcept
Get tick count.
static bool fromString(std::string_view iso8601DurationString, TimeSpan &result) noexcept
Parse a TimeSpan from a string safely without throwing exceptions.
double seconds() const noexcept
Get total seconds.
constexpr TimeSpan(std::int64_t ticks=0) noexcept
Construct from ticks (100-nanosecond units).
double hours() const noexcept
Get total hours.
double days() const noexcept
Get total days.
std::chrono::system_clock::duration toChrono() const noexcept
Convert to std::chrono::system_clock::duration.
double microseconds() const noexcept
Get total microseconds.
static TimeSpan fromChrono(const std::chrono::system_clock::duration &duration) noexcept
Create TimeSpan from std::chrono::system_clock::duration.
static constexpr TimeSpan fromDays(double days) noexcept
Create a TimeSpan from a number of days.
double milliseconds() const noexcept
Get total milliseconds.
std::string toString() const
Convert to ISO 8601 duration string.
static constexpr TimeSpan fromMilliseconds(double milliseconds) noexcept
Create a TimeSpan from a number of milliseconds.
double nanoseconds() const noexcept
Get total nanoseconds.
static constexpr TimeSpan fromMinutes(double minutes) noexcept
Create a TimeSpan from a number of minutes.
static constexpr TimeSpan fromMicroseconds(double microseconds) noexcept
Create a TimeSpan from a number of microseconds.
static constexpr TimeSpan fromTicks(double ticks) noexcept
Create a TimeSpan from a number of ticks (100-nanosecond units).