OpenXLSX 1.12.1
Modern C++ Excel Library (Fork of OpenXLSX by Kenneth Troldal Balslev)
Loading...
Searching...
No Matches
XLDateTime.hpp
Go to the documentation of this file.
1/*
2 * OpenXLSX-NX - Modern High-Performance C++ Excel Library
3 *
4 * Original work Copyright (c) 2018-2025, Kenneth Troldal Balslev and OpenXLSX contributors
5 * Modified work Copyright (c) 2026, Curry Tang and OpenXLSX-NX contributors
6 *
7 * Distributed under the BSD 3-Clause License.
8 * See LICENSE.md in the project root or https://opensource.org/licenses/BSD-3-Clause
9 */
10
11#ifndef OPENXLSX_XLDATETIME_HPP
12#define OPENXLSX_XLDATETIME_HPP
13
14#ifdef _MSC_VER
15# pragma warning(push)
16# pragma warning(disable : 4251)
17# pragma warning(disable : 4275)
18#endif
19
20// ===== External Includes ===== //
21#include <chrono>
22#include <ctime>
23#include <string>
24
25// ===== OpenXLSX Includes ===== //
26#include "OpenXLSX-Exports.hpp"
27#include "XLException.hpp"
28
29namespace OpenXLSX
30{
39 class OPENXLSX_EXPORT XLDateTime
40 {
41 public:
46
51 explicit XLDateTime(double serial);
52
57 explicit XLDateTime(const std::tm& timepoint);
58
62 explicit XLDateTime(time_t unixtime);
63
67 explicit XLDateTime(const std::chrono::system_clock::time_point& timepoint);
68
69 // Rule of Zero: All copy/move operations are handled by the compiler
70 // as the class only contains a single primitive double (m_serial).
71 XLDateTime(const XLDateTime& other) = default;
72 XLDateTime(XLDateTime&& other) noexcept = default;
73 ~XLDateTime() = default;
74 XLDateTime& operator=(const XLDateTime& other) = default;
75 XLDateTime& operator=(XLDateTime&& other) noexcept = default;
76
80 [[nodiscard]] static XLDateTime now();
81
88 [[nodiscard]] static XLDateTime fromString(const std::string& dateString, const std::string& format = "%Y-%m-%d %H:%M:%S");
89
94 [[nodiscard]] std::string toString(const std::string& format = "%Y-%m-%d %H:%M:%S") const;
95
100
104 XLDateTime& operator=(const std::tm& timepoint);
105
110 template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
111 operator T() const // NOLINT
112 { return static_cast<T>(serial()); }
113
117 operator std::tm() const; // NOLINT
118
122 [[nodiscard]] std::chrono::system_clock::time_point chrono() const;
123
127 [[nodiscard]] double serial() const;
128
134 [[nodiscard]] std::tm tm() const;
135
136 private:
137 double m_serial{1.0};
138 };
139} // namespace OpenXLSX
140
141#ifdef _MSC_VER
142# pragma warning(pop)
143#endif
144
145#endif // OPENXLSX_XLDATETIME_HPP
static XLDateTime now()
Definition XLDateTime.cpp:103
XLDateTime(const XLDateTime &other)=default
XLDateTime(XLDateTime &&other) noexcept=default
XLDateTime()
Constructs an object representing the Excel epoch (1900-01-01).
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.
Definition XLDateTime.cpp:114
static XLDateTime fromString(const std::string &dateString, const std::string &format="%Y-%m-%d %H:%M:%S")
Parses a string into an Excel date.
Definition XLDateTime.cpp:105
XLDateTime & operator=(const XLDateTime &other)=default
double serial() const
Definition XLDateTime.cpp:143
Definition IXLCellProvider.hpp:24