nfx-datetime 0.1.1
Cross-platform C++ DateTime library with 100-nanosecond precision and ISO 8601 support
Loading...
Searching...
No Matches
datetime/DateTime.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
195
196#pragma once
197
198#include <chrono>
199#include <compare>
200#include <cstdint>
201#include <format>
202#include <optional>
203#include <string>
204#include <string_view>
205
206#include "TimeSpan.h"
207
208namespace nfx::time
209{
223 class DateTime final
224 {
225 public:
226 //----------------------------------------------
227 // Enumerations
228 //----------------------------------------------
229
257
258 //----------------------------------------------
259 // Construction
260 //----------------------------------------------
261
263 constexpr DateTime() noexcept;
264
269 explicit constexpr inline DateTime( std::int64_t ticks ) noexcept;
270
275 explicit inline DateTime( std::chrono::system_clock::time_point timePoint ) noexcept;
276
283 DateTime( std::int32_t year, std::int32_t month, std::int32_t day ) noexcept;
284
294 DateTime( std::int32_t year, std::int32_t month, std::int32_t day,
295 std::int32_t hour, std::int32_t minute, std::int32_t second ) noexcept;
296
307 DateTime( std::int32_t year, std::int32_t month, std::int32_t day,
308 std::int32_t hour, std::int32_t minute, std::int32_t second,
309 std::int32_t millisecond ) noexcept;
310
315 explicit inline DateTime( std::string_view iso8601String );
316
318 DateTime( const DateTime& ) = default;
319
321 DateTime( DateTime&& ) noexcept = default;
322
323 //----------------------------------------------
324 // Destruction
325 //----------------------------------------------
326
328 ~DateTime() = default;
329
330 //----------------------------------------------
331 // Assignment
332 //----------------------------------------------
333
338 DateTime& operator=( const DateTime& ) = default;
339
344 DateTime& operator=( DateTime&& ) noexcept = default;
345
346 //----------------------------------------------
347 // Comparison operators
348 //----------------------------------------------
349
355 inline constexpr std::strong_ordering operator<=>( const DateTime& other ) const noexcept;
356
362 inline constexpr bool operator==( const DateTime& other ) const noexcept;
363
364 //----------------------------------------------
365 // Arithmetic operators
366 //----------------------------------------------
367
373 inline constexpr DateTime operator+( const TimeSpan& duration ) const noexcept;
374
380 inline constexpr DateTime operator-( const TimeSpan& duration ) const noexcept;
381
387 inline constexpr TimeSpan operator-( const DateTime& other ) const noexcept;
388
394 inline constexpr DateTime& operator+=( const TimeSpan& duration ) noexcept;
395
401 inline constexpr DateTime& operator-=( const TimeSpan& duration ) noexcept;
402
403 //----------------------------------------------
404 // Property accessors
405 //----------------------------------------------
406
412 [[nodiscard]] std::int32_t year() const noexcept;
413
419 [[nodiscard]] std::int32_t month() const noexcept;
420
426 [[nodiscard]] std::int32_t day() const noexcept;
427
433 [[nodiscard]] std::int32_t hour() const noexcept;
434
440 [[nodiscard]] std::int32_t minute() const noexcept;
441
447 [[nodiscard]] std::int32_t second() const noexcept;
448
454 [[nodiscard]] std::int32_t millisecond() const noexcept;
455
461 [[nodiscard]] std::int32_t microsecond() const noexcept;
462
469 [[nodiscard]] std::int32_t nanosecond() const noexcept;
470
476 [[nodiscard]] constexpr std::int64_t ticks() const noexcept;
477
483 [[nodiscard]] std::int32_t dayOfWeek() const noexcept;
484
490 [[nodiscard]] std::int32_t dayOfYear() const noexcept;
491
492 //----------------------------------------------
493 // Conversion methods
494 //----------------------------------------------
495
501 [[nodiscard]] inline constexpr std::int64_t toEpochSeconds() const noexcept;
502
508 [[nodiscard]] inline constexpr std::int64_t toEpochMilliseconds() const noexcept;
509
515 [[nodiscard]] DateTime date() const noexcept;
516
522 [[nodiscard]] TimeSpan timeOfDay() const noexcept;
523
524 //----------------------------------------------
525 // Validation methods
526 //----------------------------------------------
527
533 [[nodiscard]] bool isValid() const noexcept;
534
541 [[nodiscard]] inline static constexpr bool isLeapYear( std::int32_t year ) noexcept;
542
550 [[nodiscard]] inline static constexpr std::int32_t daysInMonth( std::int32_t year, std::int32_t month ) noexcept;
551
552 //----------------------------------------------
553 // Static factory methods
554 //----------------------------------------------
555
561 [[nodiscard]] static DateTime now() noexcept;
562
568 [[nodiscard]] static DateTime utcNow() noexcept;
569
575 [[nodiscard]] static DateTime today() noexcept;
576
582 [[nodiscard]] inline static constexpr DateTime min() noexcept;
583
589 [[nodiscard]] inline static constexpr DateTime max() noexcept;
590
596 [[nodiscard]] inline static constexpr DateTime epoch() noexcept;
597
605 [[nodiscard]] static bool fromString( std::string_view iso8601String, DateTime& result ) noexcept;
606
613 [[nodiscard]] static std::optional<DateTime> fromString( std::string_view iso8601String ) noexcept;
614
621 [[nodiscard]] inline static constexpr DateTime fromEpochSeconds( std::int64_t seconds ) noexcept;
622
629 [[nodiscard]] inline static constexpr DateTime fromEpochMilliseconds( std::int64_t milliseconds ) noexcept;
630
631 //----------------------------------------------
632 // String formatting
633 //----------------------------------------------
634
640 [[nodiscard]] std::string toString() const;
641
648 [[nodiscard]] std::string toString( Format format ) const;
649
655 [[nodiscard]] std::string toIso8601Extended() const;
656
657 //----------------------------------------------
658 // std::chrono interoperability
659 //----------------------------------------------
660
670 [[nodiscard]] std::chrono::system_clock::time_point toChrono() const noexcept;
671
680 [[nodiscard]] static DateTime fromChrono( const std::chrono::system_clock::time_point& timePoint ) noexcept;
681
682 private:
684 std::int64_t m_ticks;
685 };
686} // namespace nfx::time
687
688#include "nfx/detail/datetime/DateTime.inl"
High-precision time interval representation with 100-nanosecond tick precision.
constexpr std::int64_t toEpochMilliseconds() const noexcept
Convert to Epoch timestamp (milliseconds since epoch).
constexpr std::int64_t ticks() const noexcept
Get tick count (100-nanosecond units since year 1).
std::int32_t microsecond() const noexcept
Get microsecond component (0-999).
TimeSpan timeOfDay() const noexcept
Get time of day as duration since midnight.
static constexpr DateTime max() noexcept
Get maximum DateTime value.
DateTime date() const noexcept
Get date component (time set to 00:00:00).
bool isValid() const noexcept
Check if this DateTime is valid.
std::int32_t second() const noexcept
Get second component (0-59).
std::string toIso8601Extended() const
Convert to ISO 8601 extended format with full precision.
std::int32_t month() const noexcept
Get month component (1-12).
std::int32_t minute() const noexcept
Get minute component (0-59).
std::int32_t day() const noexcept
Get day component (1-31).
static constexpr DateTime epoch() noexcept
Get Unix epoch DateTime (January 1, 1970 00:00:00 UTC).
std::int32_t millisecond() const noexcept
Get millisecond component (0-999).
static bool fromString(std::string_view iso8601String, DateTime &result) noexcept
Parse ISO 8601 string safely without throwing exceptions.
static constexpr std::int32_t daysInMonth(std::int32_t year, std::int32_t month) noexcept
Get days in month for given year and month.
std::int32_t year() const noexcept
Get year component (1-9999).
static constexpr bool isLeapYear(std::int32_t year) noexcept
Check if given year is a leap year.
static DateTime utcNow() noexcept
Get current UTC time.
static constexpr DateTime fromEpochSeconds(std::int64_t seconds) noexcept
Create from Epoch timestamp (seconds since epoch).
static DateTime fromChrono(const std::chrono::system_clock::time_point &timePoint) noexcept
Create DateTime from std::chrono::system_clock::time_point.
std::string toString() const
Convert to ISO 8601 string (basic format).
std::chrono::system_clock::time_point toChrono() const noexcept
Convert to std::chrono::system_clock::time_point.
static constexpr DateTime fromEpochMilliseconds(std::int64_t milliseconds) noexcept
Create from Epoch timestamp (milliseconds since epoch).
static DateTime today() noexcept
Get current local date (time set to 00:00:00).
std::int32_t dayOfYear() const noexcept
Get day of year (1-366).
static DateTime now() noexcept
Get current local time.
constexpr DateTime() noexcept
Default constructor (minimum DateTime value).
std::int32_t hour() const noexcept
Get hour component (0-23).
std::int32_t nanosecond() const noexcept
Get nanosecond component (0-900, in 100ns increments).
std::int32_t dayOfWeek() const noexcept
Get day of week (0=Sunday, 6=Saturday).
Format
DateTime string format options.
@ TimeOnly
Time only: "12:00:00".
@ UnixMilliseconds
Epoch timestamp with milliseconds: "1704110400123".
@ Iso8601Basic
ISO 8601 basic format: "2024-01-01T12:00:00Z".
@ UnixSeconds
Epoch timestamp format: "1704110400" (seconds since epoch).
@ Iso8601WithOffset
Date and time with timezone: "2024-01-01T12:00:00+02:00".
@ Iso8601Extended
ISO 8601 extended format with fractional seconds: "2024-01-01T12:00:00.1234567Z".
@ DateOnly
Date only format: "2024-01-01".
constexpr std::int64_t toEpochSeconds() const noexcept
Convert to Epoch timestamp (seconds since epoch).
static constexpr DateTime min() noexcept
Get minimum DateTime value.
Represents a time interval in 100-nanosecond ticks.
Definition TimeSpan.h:136