nfx-datetime 0.6.0
Cross-platform C++ DateTime library with 100-nanosecond precision and ISO 8601 support
Loading...
Searching...
No Matches
DateTimeOffset.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
136
137#pragma once
138
139#include <compare>
140#include <cstdint>
141#include <string>
142#include <format>
143#include <optional>
144#include <string>
145#include <string_view>
146
147#include "DateTime.h"
148#include "TimeSpan.h"
149
150namespace nfx::time
151{
152 //=====================================================================
153 // DateTimeOffset class
154 //=====================================================================
155
170 class DateTimeOffset final
171 {
172 public:
173 //----------------------------------------------
174 // Construction
175 //----------------------------------------------
176
178 inline constexpr DateTimeOffset() noexcept;
179
185 inline constexpr DateTimeOffset( const DateTime& dateTime, const TimeSpan& offset ) noexcept;
186
191 explicit DateTimeOffset( const DateTime& dateTime ) noexcept;
192
198 inline constexpr DateTimeOffset( std::int64_t ticks, const TimeSpan& offset ) noexcept;
199
208 std::int32_t year, std::int32_t month, std::int32_t day, const TimeSpan& offset ) noexcept;
209
221 std::int32_t year,
222 std::int32_t month,
223 std::int32_t day,
224 std::int32_t hour,
225 std::int32_t minute,
226 std::int32_t second,
227 const TimeSpan& offset ) noexcept;
228
241 std::int32_t year,
242 std::int32_t month,
243 std::int32_t day,
244 std::int32_t hour,
245 std::int32_t minute,
246 std::int32_t second,
247 std::int32_t millisecond,
248 const TimeSpan& offset ) noexcept;
249
263 std::int32_t year,
264 std::int32_t month,
265 std::int32_t day,
266 std::int32_t hour,
267 std::int32_t minute,
268 std::int32_t second,
269 std::int32_t millisecond,
270 std::int32_t microsecond,
271 const TimeSpan& offset ) noexcept;
272
277 inline explicit DateTimeOffset( std::string_view iso8601String );
278
283 inline explicit DateTimeOffset( const char* iso8601String );
284
289 inline explicit DateTimeOffset( std::initializer_list<const char*> list );
290
292 DateTimeOffset( const DateTimeOffset& ) = default;
293
295 DateTimeOffset( DateTimeOffset&& ) noexcept = default;
296
297 //----------------------------------------------
298 // Destruction
299 //----------------------------------------------
300
302 ~DateTimeOffset() = default;
303
304 //----------------------------------------------
305 // Assignment
306 //----------------------------------------------
307
312 DateTimeOffset& operator=( const DateTimeOffset& ) = default;
313
318 DateTimeOffset& operator=( DateTimeOffset&& ) noexcept = default;
319
320 //----------------------------------------------
321 // Comparison operators
322 //----------------------------------------------
323
329 inline constexpr std::strong_ordering operator<=>( const DateTimeOffset& other ) const noexcept;
330
336 inline constexpr bool operator==( const DateTimeOffset& other ) const noexcept;
337
338 //----------------------------------------------
339 // Arithmetic operators
340 //----------------------------------------------
341
347 inline constexpr DateTimeOffset operator+( const TimeSpan& duration ) const noexcept;
348
354 inline constexpr DateTimeOffset operator-( const TimeSpan& duration ) const noexcept;
355
361 inline constexpr TimeSpan operator-( const DateTimeOffset& other ) const noexcept;
362
368 inline constexpr DateTimeOffset& operator+=( const TimeSpan& duration ) noexcept;
369
375 inline constexpr DateTimeOffset& operator-=( const TimeSpan& duration ) noexcept;
376
377 //----------------------------------------------
378 // Property accessors
379 //----------------------------------------------
380
386 [[nodiscard]] inline constexpr const DateTime& dateTime() const noexcept;
387
393 [[nodiscard]] inline constexpr const TimeSpan& offset() const noexcept;
394
400 [[nodiscard]] DateTime utcDateTime() const noexcept;
401
407 [[nodiscard]] DateTime localDateTime() const noexcept;
408
414 [[nodiscard]] inline constexpr std::int64_t ticks() const noexcept;
415
421 [[nodiscard]] inline constexpr std::int64_t utcTicks() const noexcept;
422
428 [[nodiscard]] inline std::int32_t year() const noexcept;
429
435 [[nodiscard]] inline std::int32_t month() const noexcept;
436
442 [[nodiscard]] inline std::int32_t day() const noexcept;
443
449 [[nodiscard]] inline std::int32_t hour() const noexcept;
450
456 [[nodiscard]] inline std::int32_t minute() const noexcept;
457
463 [[nodiscard]] inline std::int32_t second() const noexcept;
464
470 [[nodiscard]] inline std::int32_t millisecond() const noexcept;
471
477 [[nodiscard]] inline std::int32_t microsecond() const noexcept;
478
484 [[nodiscard]] inline std::int32_t nanosecond() const noexcept;
485
491 [[nodiscard]] inline std::int32_t dayOfWeek() const noexcept;
492
498 [[nodiscard]] inline std::int32_t dayOfYear() const noexcept;
499
505 [[nodiscard]] inline std::int32_t totalOffsetMinutes() const noexcept;
506
507 //----------------------------------------------
508 // Conversion methods
509 //----------------------------------------------
510
516 [[nodiscard]] inline std::int64_t toEpochSeconds() const noexcept;
517
523 [[nodiscard]] inline std::int64_t toEpochMilliseconds() const noexcept;
524
530 [[nodiscard]] DateTimeOffset date() const noexcept;
531
537 [[nodiscard]] inline TimeSpan timeOfDay() const noexcept;
538
545 [[nodiscard]] DateTimeOffset toOffset( const TimeSpan& newOffset ) const noexcept;
546
552 [[nodiscard]] DateTimeOffset toUniversalTime() const noexcept;
553
559 [[nodiscard]] DateTimeOffset toLocalTime() const noexcept;
560
568 [[nodiscard]] std::int64_t toFILETIME() const noexcept;
569
570 //----------------------------------------------
571 // Arithmetic methods
572 //----------------------------------------------
573
580 [[nodiscard]] inline DateTimeOffset add( const TimeSpan& value ) const noexcept;
581
588 [[nodiscard]] DateTimeOffset addDays( double days ) const noexcept;
589
596 [[nodiscard]] DateTimeOffset addHours( double hours ) const noexcept;
597
604 [[nodiscard]] DateTimeOffset addMilliseconds( double milliseconds ) const noexcept;
605
612 [[nodiscard]] DateTimeOffset addMinutes( double minutes ) const noexcept;
613
620 [[nodiscard]] DateTimeOffset addMonths( std::int32_t months ) const noexcept;
621
628 [[nodiscard]] DateTimeOffset addSeconds( double seconds ) const noexcept;
629
636 [[nodiscard]] inline DateTimeOffset addTicks( std::int64_t ticks ) const noexcept;
637
644 [[nodiscard]] DateTimeOffset addYears( std::int32_t years ) const noexcept;
645
652 [[nodiscard]] inline TimeSpan subtract( const DateTimeOffset& value ) const noexcept;
653
660 [[nodiscard]] inline DateTimeOffset subtract( const TimeSpan& value ) const noexcept;
661
662 //----------------------------------------------
663 // String formatting
664 //----------------------------------------------
665
672 [[nodiscard]] std::string toString( DateTime::Format format = DateTime::Format::Iso8601 ) const;
673
674 //----------------------------------------------
675 // Comparison methods
676 //----------------------------------------------
677
684 [[nodiscard]] inline bool equals( const DateTimeOffset& other ) const noexcept;
685
692 [[nodiscard]] inline bool equalsExact( const DateTimeOffset& other ) const noexcept;
693
694 //----------------------------------------------
695 // Validation methods
696 //----------------------------------------------
697
703 [[nodiscard]] bool isValid() const noexcept;
704
705 //----------------------------------------------
706 // Static factory methods
707 //----------------------------------------------
708
714 [[nodiscard]] static DateTimeOffset now() noexcept;
715
721 [[nodiscard]] static DateTimeOffset utcNow() noexcept;
722
728 [[nodiscard]] static DateTimeOffset today() noexcept;
729
736 [[nodiscard]] static DateTimeOffset min() noexcept;
737
744 [[nodiscard]] static DateTimeOffset max() noexcept;
745
751 [[nodiscard]] static DateTimeOffset epoch() noexcept;
752
760 [[nodiscard]] static bool fromString( std::string_view iso8601String, DateTimeOffset& result ) noexcept;
761
768 [[nodiscard]] static std::optional<DateTimeOffset> fromString( std::string_view iso8601String ) noexcept;
769
776 [[nodiscard]] static DateTimeOffset fromEpochSeconds( std::int64_t seconds ) noexcept;
777
784 [[nodiscard]] static DateTimeOffset fromEpochMilliseconds( std::int64_t milliseconds ) noexcept;
785
794 [[nodiscard]] static DateTimeOffset fromFILETIME( std::int64_t FILETIME ) noexcept;
795
796 private:
798 DateTime m_dateTime;
799
801 TimeSpan m_offset;
802 };
803} // namespace nfx::time
804
805#include "nfx/detail/datetime/DateTimeOffset.inl"
High-precision time interval representation with 100-nanosecond tick precision.
Cross-platform DateTime type with 100-nanosecond precision.
DateTime localDateTime() const noexcept
Get local DateTime equivalent.
DateTimeOffset addDays(double days) const noexcept
Add days.
std::int32_t hour() const noexcept
Get hour component (0-23).
std::int32_t day() const noexcept
Get day component (1-31).
DateTimeOffset add(const TimeSpan &value) const noexcept
Add time span.
static DateTimeOffset max() noexcept
Get maximum DateTimeOffset value.
static DateTimeOffset utcNow() noexcept
Get current UTC time (offset = 00:00:00).
std::string toString(DateTime::Format format=DateTime::Format::Iso8601) const
Convert to string using specified format.
constexpr std::int64_t utcTicks() const noexcept
Get UTC tick count.
DateTimeOffset toUniversalTime() const noexcept
Convert to UTC (offset = 00:00:00).
std::int32_t dayOfYear() const noexcept
Get day of year (1-366).
std::int32_t dayOfWeek() const noexcept
Get day of week (0=Sunday, 6=Saturday).
bool equals(const DateTimeOffset &other) const noexcept
Check if this DateTimeOffset equals another (same UTC time).
std::int32_t nanosecond() const noexcept
Get nanosecond component (0-900, in hundreds).
DateTimeOffset toOffset(const TimeSpan &newOffset) const noexcept
Convert to specified offset.
bool equalsExact(const DateTimeOffset &other) const noexcept
Check if this DateTimeOffset equals another exactly (same local time and offset).
TimeSpan subtract(const DateTimeOffset &value) const noexcept
Subtract DateTimeOffset and return TimeSpan.
DateTime utcDateTime() const noexcept
Get UTC DateTime equivalent.
bool isValid() const noexcept
Check if this DateTimeOffset is valid.
DateTimeOffset addSeconds(double seconds) const noexcept
Add seconds.
TimeSpan timeOfDay() const noexcept
Get time of day as duration since midnight.
static bool fromString(std::string_view iso8601String, DateTimeOffset &result) noexcept
Parse ISO 8601 string with timezone offset safely without throwing exceptions.
static DateTimeOffset today() noexcept
Get current local date (time set to 00:00:00).
DateTimeOffset date() const noexcept
Get date component (time set to 00:00:00).
DateTimeOffset addMinutes(double minutes) const noexcept
Add minutes.
std::int64_t toEpochSeconds() const noexcept
Convert to Epoch timestamp (seconds since epoch).
constexpr DateTimeOffset() noexcept
Default constructor (minimum DateTimeOffset value with zero offset).
static DateTimeOffset min() noexcept
Get minimum DateTimeOffset value.
constexpr std::int64_t ticks() const noexcept
Get tick count (100-nanosecond units of local time).
static DateTimeOffset fromEpochMilliseconds(std::int64_t milliseconds) noexcept
Create from Epoch timestamp milliseconds with UTC offset.
static DateTimeOffset fromFILETIME(std::int64_t FILETIME) noexcept
Create DateTimeOffset from Windows FILETIME format.
std::int32_t totalOffsetMinutes() const noexcept
Get offset in total minutes.
std::int32_t microsecond() const noexcept
Get microsecond component (0-999).
static DateTimeOffset fromEpochSeconds(std::int64_t seconds) noexcept
Create from Epoch timestamp seconds with UTC offset.
static DateTimeOffset epoch() noexcept
Get Unix epoch DateTimeOffset (January 1, 1970 00:00:00 UTC).
std::int32_t year() const noexcept
Get year component (1-9999).
static DateTimeOffset now() noexcept
Get current local time with system timezone offset.
std::int32_t millisecond() const noexcept
Get millisecond component (0-999).
constexpr const TimeSpan & offset() const noexcept
Get the offset from UTC.
DateTimeOffset addHours(double hours) const noexcept
Add hours.
DateTimeOffset addMilliseconds(double milliseconds) const noexcept
Add milliseconds.
constexpr const DateTime & dateTime() const noexcept
Get the DateTime component (local time).
DateTimeOffset addTicks(std::int64_t ticks) const noexcept
Add ticks.
DateTimeOffset addYears(std::int32_t years) const noexcept
Add years.
DateTimeOffset toLocalTime() const noexcept
Convert to local time (system timezone).
std::int32_t month() const noexcept
Get month component (1-12).
std::int64_t toEpochMilliseconds() const noexcept
Convert to Epoch timestamp (milliseconds since epoch).
std::int64_t toFILETIME() const noexcept
Convert to Windows FILETIME format.
std::int32_t second() const noexcept
Get second component (0-59).
DateTimeOffset addMonths(std::int32_t months) const noexcept
Add months.
std::int32_t minute() const noexcept
Get minute component (0-59).
Represents a time interval in 100-nanosecond ticks.
Definition TimeSpan.h:136
Cross-platform DateTime class with ISO 8601 support.