OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
OpenXLSX::XLDateTime Class Reference

Manages date and time values according to the Excel 1900 date system. More...

#include <XLDateTime.hpp>

Public Member Functions

 XLDateTime ()
 Constructs an object representing the Excel epoch (1900-01-01).
 XLDateTime (double serial)
 XLDateTime (const std::tm &timepoint)
 XLDateTime (time_t unixtime)
 XLDateTime (const std::chrono::system_clock::time_point &timepoint)
 XLDateTime (const XLDateTime &other)=default
 XLDateTime (XLDateTime &&other) noexcept=default
 ~XLDateTime ()=default
XLDateTime & operator= (const XLDateTime &other)=default
XLDateTime & operator= (XLDateTime &&other) noexcept=default
std::string toString (const std::string &format="%Y-%m-%d %H:%M:%S") const
 Formats the date into a human-readable string.
XLDateTime & operator= (double serial)
 Updates the serial number and validates its range.
XLDateTime & operator= (const std::tm &timepoint)
 Updates the date from a tm struct.
template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
 operator T () const
 Implicitly converts the date to a floating-point serial number.
 operator std::tm () const
 Explicitly converts the Excel date to a standard C tm struct.
std::chrono::system_clock::time_point chrono () const
 Converts the Excel date to a C++ system_clock::time_point.
double serial () const
std::tm tm () const
 Decomposes the Excel serial number into its date and time components.

Static Public Member Functions

static XLDateTime now ()
static XLDateTime fromString (const std::string &dateString, const std::string &format="%Y-%m-%d %H:%M:%S")
 Parses a string into an Excel date.

Detailed Description

Manages date and time values according to the Excel 1900 date system.

Excel represents dates as floating-point numbers where 1.0 is 1900-01-01. This class handles the historical "1900 Leap Year Bug" (Excel incorrectly treats 1900 as a leap year) to ensure absolute compatibility with Microsoft Excel files. All string inputs and outputs are in the ISO 8601-like format (YYYY-MM-DD HH:MM:SS) unless a custom format is specified.

Constructor & Destructor Documentation

◆ XLDateTime() [1/7]

OpenXLSX::XLDateTime::XLDateTime ( )
default

Constructs an object representing the Excel epoch (1900-01-01).

◆ XLDateTime() [2/7]

OpenXLSX::XLDateTime::XLDateTime ( double serial)
explicit
Parameters
serialExcel's floating-point date representation. Must be >= 1.0.
Exceptions
XLDateTimeErrorif serial is less than 1.0 (Excel doesn't support earlier dates).

◆ XLDateTime() [3/7]

OpenXLSX::XLDateTime::XLDateTime ( const std::tm & timepoint)
explicit
Parameters
timepointA standard C tm struct. Only date and time fields are used.
Exceptions
XLDateTimeErrorif the date is invalid or before 1900.

Converts a standard tm struct to an Excel serial number. The conversion process starts from the 1900 epoch and accounts for every full year and month before adding the current day and fractional time.

◆ XLDateTime() [4/7]

OpenXLSX::XLDateTime::XLDateTime ( time_t unixtime)
explicit
Parameters
unixtimeSeconds since the Unix epoch (1970-01-01 00:00:00 UTC).

Unix-to-Excel conversion uses a fixed offset of 25569 days (the difference between the two epochs).

◆ XLDateTime() [5/7]

OpenXLSX::XLDateTime::XLDateTime ( const std::chrono::system_clock::time_point & timepoint)
explicit
Parameters
timepointA high-precision system clock time point.

◆ XLDateTime() [6/7]

OpenXLSX::XLDateTime::XLDateTime ( const XLDateTime & other)
default

◆ XLDateTime() [7/7]

OpenXLSX::XLDateTime::XLDateTime ( XLDateTime && other)
defaultnoexcept

◆ ~XLDateTime()

OpenXLSX::XLDateTime::~XLDateTime ( )
default

Member Function Documentation

◆ chrono()

std::chrono::system_clock::time_point OpenXLSX::XLDateTime::chrono ( ) const
nodiscard

Converts the Excel date to a C++ system_clock::time_point.

◆ fromString()

XLDateTime OpenXLSX::XLDateTime::fromString ( const std::string & dateString,
const std::string & format = "%Y-%m-%d %H:%M:%S" )
staticnodiscard

Parses a string into an Excel date.

Parameters
dateStringThe source string (e.g., "2024-03-16 10:00:00").
formatStandard strptime-compatible format string.
Exceptions
XLDateTimeErrorif parsing fails.

◆ now()

XLDateTime OpenXLSX::XLDateTime::now ( )
staticnodiscard
Returns
An XLDateTime object initialized to the current system time.

◆ operator std::tm()

OpenXLSX::XLDateTime::operator std::tm ( ) const

Explicitly converts the Excel date to a standard C tm struct.

◆ operator T()

template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
OpenXLSX::XLDateTime::operator T ( ) const
inline

Implicitly converts the date to a floating-point serial number.

This allows seamless integration with worksheet cell values.

◆ operator=() [1/4]

XLDateTime & OpenXLSX::XLDateTime::operator= ( const std::tm & timepoint)

Updates the date from a tm struct.

◆ operator=() [2/4]

XLDateTime & OpenXLSX::XLDateTime::operator= ( const XLDateTime & other)
default

◆ operator=() [3/4]

XLDateTime & OpenXLSX::XLDateTime::operator= ( double serial)

Updates the serial number and validates its range.

◆ operator=() [4/4]

XLDateTime & OpenXLSX::XLDateTime::operator= ( XLDateTime && other)
defaultnoexcept

◆ serial()

double OpenXLSX::XLDateTime::serial ( ) const
nodiscard
Returns
The raw Excel serial number (days since 1899-12-30).

◆ tm()

std::tm OpenXLSX::XLDateTime::tm ( ) const
nodiscard

Decomposes the Excel serial number into its date and time components.

This method handles the 1900 leap year bug and rounding overflows to provide an accurate representation of the time.

Decomposition of the Excel serial number. The process handles the 1900 bug by treating 1900 as a leap year, ensuring that all subsequent dates are correctly aligned with Excel's calendar. Time components are extracted through fractional remainders, with an overflow check to handle precision rounding issues that could push a timestamp into the next second or day.

◆ toString()

std::string OpenXLSX::XLDateTime::toString ( const std::string & format = "%Y-%m-%d %H:%M:%S") const
nodiscard

Formats the date into a human-readable string.

Parameters
formatStandard strftime-compatible format string.

The documentation for this class was generated from the following files:
  • /home/runner/work/OpenXLSX-NX/OpenXLSX-NX/OpenXLSX/headers/XLDateTime.hpp
  • /home/runner/work/OpenXLSX-NX/OpenXLSX-NX/OpenXLSX/sources/XLDateTime.cpp