nfx-datetime 0.1.1
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
133
134#pragma once
135
136#include <compare>
137#include <cstdint>
138#include <string>
139#include <format>
140#include <optional>
141#include <string>
142#include <string_view>
143
144#include "DateTime.h"
145#include "TimeSpan.h"
146
147namespace nfx::time
148{
149 //=====================================================================
150 // DateTimeOffset class
151 //=====================================================================
152
167 class DateTimeOffset final
168 {
169 public:
170 //----------------------------------------------
171 // Construction
172 //----------------------------------------------
173
175 inline constexpr DateTimeOffset() noexcept;
176
182 inline constexpr DateTimeOffset( const DateTime& dateTime, const TimeSpan& offset ) noexcept;
183
188 explicit DateTimeOffset( const DateTime& dateTime ) noexcept;
189
195 inline constexpr DateTimeOffset( std::int64_t ticks, const TimeSpan& offset ) noexcept;
196
204 inline DateTimeOffset( std::int32_t year, std::int32_t month, std::int32_t day, const TimeSpan& offset ) noexcept;
205
216 inline DateTimeOffset( std::int32_t year, std::int32_t month, std::int32_t day, std::int32_t hour, std::int32_t minute, std::int32_t second,
217 const TimeSpan& offset ) noexcept;
218
230 inline DateTimeOffset( std::int32_t year, std::int32_t month, std::int32_t day, std::int32_t hour, std::int32_t minute, std::int32_t second,
231 std::int32_t millisecond, const TimeSpan& offset ) noexcept;
232
245 inline DateTimeOffset( std::int32_t year, std::int32_t month, std::int32_t day, std::int32_t hour, std::int32_t minute, std::int32_t second,
246 std::int32_t millisecond, std::int32_t microsecond, const TimeSpan& offset ) noexcept;
247
252 inline explicit DateTimeOffset( std::string_view iso8601String );
253
255 DateTimeOffset( const DateTimeOffset& ) = default;
256
258 DateTimeOffset( DateTimeOffset&& ) noexcept = default;
259
260 //----------------------------------------------
261 // Destruction
262 //----------------------------------------------
263
265 ~DateTimeOffset() = default;
266
267 //----------------------------------------------
268 // Assignment
269 //----------------------------------------------
270
275 DateTimeOffset& operator=( const DateTimeOffset& ) = default;
276
281 DateTimeOffset& operator=( DateTimeOffset&& ) noexcept = default;
282
283 //----------------------------------------------
284 // Comparison operators
285 //----------------------------------------------
286
292 inline constexpr std::strong_ordering operator<=>( const DateTimeOffset& other ) const noexcept;
293
299 inline constexpr bool operator==( const DateTimeOffset& other ) const noexcept;
300
301 //----------------------------------------------
302 // Arithmetic operators
303 //----------------------------------------------
304
310 inline constexpr DateTimeOffset operator+( const TimeSpan& duration ) const noexcept;
311
317 inline constexpr DateTimeOffset operator-( const TimeSpan& duration ) const noexcept;
318
324 inline constexpr TimeSpan operator-( const DateTimeOffset& other ) const noexcept;
325
331 inline constexpr DateTimeOffset& operator+=( const TimeSpan& duration ) noexcept;
332
338 inline constexpr DateTimeOffset& operator-=( const TimeSpan& duration ) noexcept;
339
340 //----------------------------------------------
341 // Property accessors
342 //----------------------------------------------
343
349 [[nodiscard]] inline constexpr const DateTime& dateTime() const noexcept;
350
356 [[nodiscard]] inline constexpr const TimeSpan& offset() const noexcept;
357
363 [[nodiscard]] DateTime utcDateTime() const noexcept;
364
370 [[nodiscard]] DateTime localDateTime() const noexcept;
371
377 [[nodiscard]] inline constexpr std::int64_t ticks() const noexcept;
378
384 [[nodiscard]] inline constexpr std::int64_t utcTicks() const noexcept;
385
391 [[nodiscard]] inline std::int32_t year() const noexcept;
392
398 [[nodiscard]] inline std::int32_t month() const noexcept;
399
405 [[nodiscard]] inline std::int32_t day() const noexcept;
406
412 [[nodiscard]] inline std::int32_t hour() const noexcept;
413
419 [[nodiscard]] inline std::int32_t minute() const noexcept;
420
426 [[nodiscard]] inline std::int32_t second() const noexcept;
427
433 [[nodiscard]] inline std::int32_t millisecond() const noexcept;
434
440 [[nodiscard]] inline std::int32_t microsecond() const noexcept;
441
447 [[nodiscard]] inline std::int32_t nanosecond() const noexcept;
448
454 [[nodiscard]] inline std::int32_t dayOfWeek() const noexcept;
455
461 [[nodiscard]] inline std::int32_t dayOfYear() const noexcept;
462
468 [[nodiscard]] inline std::int32_t totalOffsetMinutes() const noexcept;
469
470 //----------------------------------------------
471 // Conversion methods
472 //----------------------------------------------
473
479 [[nodiscard]] inline std::int64_t toEpochSeconds() const noexcept;
480
486 [[nodiscard]] inline std::int64_t toEpochMilliseconds() const noexcept;
487
493 [[nodiscard]] DateTimeOffset date() const noexcept;
494
500 [[nodiscard]] inline TimeSpan timeOfDay() const noexcept;
501
508 [[nodiscard]] DateTimeOffset toOffset( const TimeSpan& newOffset ) const noexcept;
509
515 [[nodiscard]] DateTimeOffset toUniversalTime() const noexcept;
516
522 [[nodiscard]] DateTimeOffset toLocalTime() const noexcept;
523
531 [[nodiscard]] std::int64_t toFILETIME() const noexcept;
532
533 //----------------------------------------------
534 // Arithmetic methods
535 //----------------------------------------------
536
543 [[nodiscard]] inline DateTimeOffset add( const TimeSpan& value ) const noexcept;
544
551 [[nodiscard]] DateTimeOffset addDays( double days ) const noexcept;
552
559 [[nodiscard]] DateTimeOffset addHours( double hours ) const noexcept;
560
567 [[nodiscard]] DateTimeOffset addMilliseconds( double milliseconds ) const noexcept;
568
575 [[nodiscard]] DateTimeOffset addMinutes( double minutes ) const noexcept;
576
583 [[nodiscard]] DateTimeOffset addMonths( std::int32_t months ) const noexcept;
584
591 [[nodiscard]] DateTimeOffset addSeconds( double seconds ) const noexcept;
592
599 [[nodiscard]] inline DateTimeOffset addTicks( std::int64_t ticks ) const noexcept;
600
607 [[nodiscard]] DateTimeOffset addYears( std::int32_t years ) const noexcept;
608
615 [[nodiscard]] inline TimeSpan subtract( const DateTimeOffset& value ) const noexcept;
616
623 [[nodiscard]] inline DateTimeOffset subtract( const TimeSpan& value ) const noexcept;
624
625 //----------------------------------------------
626 // String formatting
627 //----------------------------------------------
628
634 [[nodiscard]] std::string toString() const;
635
642 [[nodiscard]] std::string toString( DateTime::Format format ) const;
643
649 [[nodiscard]] std::string toIso8601Extended() const;
650
651 //----------------------------------------------
652 // Comparison methods
653 //----------------------------------------------
654
661 [[nodiscard]] inline bool equals( const DateTimeOffset& other ) const noexcept;
662
669 [[nodiscard]] inline bool equalsExact( const DateTimeOffset& other ) const noexcept;
670
671 //----------------------------------------------
672 // Validation methods
673 //----------------------------------------------
674
680 [[nodiscard]] bool isValid() const noexcept;
681
682 //----------------------------------------------
683 // Static factory methods
684 //----------------------------------------------
685
691 [[nodiscard]] static DateTimeOffset now() noexcept;
692
698 [[nodiscard]] static DateTimeOffset utcNow() noexcept;
699
705 [[nodiscard]] static DateTimeOffset today() noexcept;
706
712 [[nodiscard]] static DateTimeOffset min() noexcept;
713
719 [[nodiscard]] static DateTimeOffset max() noexcept;
720
726 [[nodiscard]] static DateTimeOffset epoch() noexcept;
727
735 [[nodiscard]] static bool fromString( std::string_view iso8601String, DateTimeOffset& result ) noexcept;
736
743 [[nodiscard]] static std::optional<DateTimeOffset> fromString( std::string_view iso8601String ) noexcept;
744
751 [[nodiscard]] static DateTimeOffset fromEpochSeconds( std::int64_t seconds ) noexcept;
752
759 [[nodiscard]] static DateTimeOffset fromEpochMilliseconds( std::int64_t milliseconds ) noexcept;
760
769 [[nodiscard]] static DateTimeOffset fromFILETIME( std::int64_t FILETIME ) noexcept;
770
771 private:
773 DateTime m_dateTime;
774
776 TimeSpan m_offset;
777 };
778} // namespace nfx::time
779
780#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).
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::string toString() const
Convert to ISO 8601 string with offset.
std::string toIso8601Extended() const
Convert to ISO 8601 extended format with full precision and offset.
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.