nfx-datetime 0.6.0
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
197
198#pragma once
199
200#include <chrono>
201#include <compare>
202#include <cstdint>
203#include <format>
204#include <optional>
205#include <string>
206#include <string_view>
207
208#include "TimeSpan.h"
209
210namespace nfx::time
211{
225 class DateTime final
226 {
227 public:
228 //----------------------------------------------
229 // Enumerations
230 //----------------------------------------------
231
271
272 //----------------------------------------------
273 // Construction
274 //----------------------------------------------
275
277 constexpr DateTime() noexcept;
278
283 explicit constexpr inline DateTime( std::int64_t ticks ) noexcept;
284
289 inline explicit DateTime( std::chrono::system_clock::time_point timePoint ) noexcept;
290
297 DateTime( std::int32_t year, std::int32_t month, std::int32_t day ) noexcept;
298
309 std::int32_t year,
310 std::int32_t month,
311 std::int32_t day,
312 std::int32_t hour,
313 std::int32_t minute,
314 std::int32_t second ) noexcept;
315
327 std::int32_t year,
328 std::int32_t month,
329 std::int32_t day,
330 std::int32_t hour,
331 std::int32_t minute,
332 std::int32_t second,
333 std::int32_t millisecond ) noexcept;
334
339 inline explicit DateTime( std::string_view iso8601String );
340
345 inline explicit DateTime( const char* iso8601String );
346
351 inline explicit DateTime( std::initializer_list<const char*> list );
352
354 DateTime( const DateTime& ) = default;
355
357 DateTime( DateTime&& ) noexcept = default;
358
359 //----------------------------------------------
360 // Destruction
361 //----------------------------------------------
362
364 ~DateTime() = default;
365
366 //----------------------------------------------
367 // Assignment
368 //----------------------------------------------
369
374 DateTime& operator=( const DateTime& ) = default;
375
380 DateTime& operator=( DateTime&& ) noexcept = default;
381
382 //----------------------------------------------
383 // Comparison operators
384 //----------------------------------------------
385
391 inline constexpr std::strong_ordering operator<=>( const DateTime& other ) const noexcept;
392
398 inline constexpr bool operator==( const DateTime& other ) const noexcept;
399
400 //----------------------------------------------
401 // Arithmetic operators
402 //----------------------------------------------
403
409 inline constexpr DateTime operator+( const TimeSpan& duration ) const noexcept;
410
416 inline constexpr DateTime operator-( const TimeSpan& duration ) const noexcept;
417
423 inline constexpr TimeSpan operator-( const DateTime& other ) const noexcept;
424
430 inline constexpr DateTime& operator+=( const TimeSpan& duration ) noexcept;
431
437 inline constexpr DateTime& operator-=( const TimeSpan& duration ) noexcept;
438
439 //----------------------------------------------
440 // Property accessors
441 //----------------------------------------------
442
448 [[nodiscard]] std::int32_t year() const noexcept;
449
455 [[nodiscard]] std::int32_t month() const noexcept;
456
462 [[nodiscard]] std::int32_t day() const noexcept;
463
469 [[nodiscard]] std::int32_t hour() const noexcept;
470
476 [[nodiscard]] std::int32_t minute() const noexcept;
477
483 [[nodiscard]] std::int32_t second() const noexcept;
484
490 [[nodiscard]] std::int32_t millisecond() const noexcept;
491
497 [[nodiscard]] std::int32_t microsecond() const noexcept;
498
505 [[nodiscard]] std::int32_t nanosecond() const noexcept;
506
512 [[nodiscard]] constexpr std::int64_t ticks() const noexcept;
513
519 [[nodiscard]] std::int32_t dayOfWeek() const noexcept;
520
526 [[nodiscard]] std::int32_t dayOfYear() const noexcept;
527
528 //----------------------------------------------
529 // Conversion methods
530 //----------------------------------------------
531
537 [[nodiscard]] inline constexpr std::int64_t toEpochSeconds() const noexcept;
538
544 [[nodiscard]] inline constexpr std::int64_t toEpochMilliseconds() const noexcept;
545
551 [[nodiscard]] DateTime date() const noexcept;
552
558 [[nodiscard]] TimeSpan timeOfDay() const noexcept;
559
560 //----------------------------------------------
561 // Validation methods
562 //----------------------------------------------
563
569 [[nodiscard]] bool isValid() const noexcept;
570
577 [[nodiscard]] inline static constexpr bool isLeapYear( std::int32_t year ) noexcept;
578
586 [[nodiscard]] inline static constexpr std::int32_t daysInMonth(
587 std::int32_t year, std::int32_t month ) noexcept;
588
589 //----------------------------------------------
590 // Static factory methods
591 //----------------------------------------------
592
598 [[nodiscard]] static DateTime now() noexcept;
599
605 [[nodiscard]] static DateTime utcNow() noexcept;
606
612 [[nodiscard]] static DateTime today() noexcept;
613
619 [[nodiscard]] inline static constexpr DateTime min() noexcept;
620
626 [[nodiscard]] inline static constexpr DateTime max() noexcept;
627
633 [[nodiscard]] inline static constexpr DateTime epoch() noexcept;
634
642 [[nodiscard]] static bool fromString( std::string_view iso8601String, DateTime& result ) noexcept;
643
650 [[nodiscard]] static std::optional<DateTime> fromString( std::string_view iso8601String ) noexcept;
651
658 [[nodiscard]] inline static constexpr DateTime fromEpochSeconds( std::int64_t seconds ) noexcept;
659
666 [[nodiscard]] inline static constexpr DateTime fromEpochMilliseconds( std::int64_t milliseconds ) noexcept;
667
668 //----------------------------------------------
669 // String formatting
670 //----------------------------------------------
671
678 [[nodiscard]] std::string toString( Format format = Format::Iso8601 ) const;
679
680 //----------------------------------------------
681 // std::chrono interoperability
682 //----------------------------------------------
683
693 [[nodiscard]] std::chrono::system_clock::time_point toChrono() const noexcept;
694
703 [[nodiscard]] static DateTime fromChrono( const std::chrono::system_clock::time_point& timePoint ) noexcept;
704
705 private:
707 std::int64_t m_ticks;
708 };
709} // namespace nfx::time
710
711#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).
std::string toString(Format format=Format::Iso8601) const
Convert to string using specified format.
bool isValid() const noexcept
Check if this DateTime is valid.
std::int32_t second() const noexcept
Get second component (0-59).
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::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.
@ Iso8601Millis
ISO 8601 with millisecond precision (3 decimal digits): "2024-01-01T12:00:00.123Z".
@ Iso8601Time
ISO 8601 time only: "12:00:00".
@ Iso8601Date
ISO 8601 date only: "2024-01-01".
@ Iso8601Micros
ISO 8601 with microsecond precision (6 decimal digits): "2024-01-01T12:00:00.123456Z".
@ UnixMilliseconds
Unix epoch milliseconds (integer): "1704110400000".
@ Iso8601Basic
ISO 8601 compact form without separators: "20240101T120000Z".
@ UnixSeconds
Unix epoch seconds (integer): "1704110400".
@ Iso8601
ISO 8601 with seconds precision: "2024-01-01T12:00:00Z".
@ Iso8601Precise
ISO 8601 with full tick precision (7 decimal digits): "2024-01-01T12:00:00.1234567Z".
@ Iso8601PreciseTrimmed
ISO 8601 with trimmed trailing zeros in fractions: "2024-01-01T12:00:00.1234Z".
@ Iso8601Extended
ISO 8601 with numeric offset (always +00:00 for DateTime): "2024-01-01T12:00:00+00:00".
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