OpenXLSX 1.9.1
Loading...
Searching...
No Matches
XLDateTime.hpp
Go to the documentation of this file.
1#ifndef OPENXLSX_XLDATETIME_HPP
2#define OPENXLSX_XLDATETIME_HPP
3
4#ifdef _MSC_VER
5# pragma warning(push)
6# pragma warning(disable : 4251)
7# pragma warning(disable : 4275)
8#endif
9
10// ===== External Includes ===== //
11#include <chrono>
12#include <ctime>
13#include <string>
14
15// ===== OpenXLSX Includes ===== //
16#include "OpenXLSX-Exports.hpp"
17#include "XLException.hpp"
18
19namespace OpenXLSX
20{
29 class OPENXLSX_EXPORT XLDateTime
30 {
31 public:
36
41 explicit XLDateTime(double serial);
42
47 explicit XLDateTime(const std::tm& timepoint);
48
52 explicit XLDateTime(time_t unixtime);
53
57 explicit XLDateTime(const std::chrono::system_clock::time_point& timepoint);
58
59 // Rule of Zero: All copy/move operations are handled by the compiler
60 // as the class only contains a single primitive double (m_serial).
61 XLDateTime(const XLDateTime& other) = default;
62 XLDateTime(XLDateTime&& other) noexcept = default;
63 ~XLDateTime() = default;
64 XLDateTime& operator=(const XLDateTime& other) = default;
65 XLDateTime& operator=(XLDateTime&& other) noexcept = default;
66
70 [[nodiscard]] static XLDateTime now();
71
78 [[nodiscard]] static XLDateTime fromString(const std::string& dateString, const std::string& format = "%Y-%m-%d %H:%M:%S");
79
84 [[nodiscard]] std::string toString(const std::string& format = "%Y-%m-%d %H:%M:%S") const;
85
89 XLDateTime& operator=(double serial);
90
94 XLDateTime& operator=(const std::tm& timepoint);
95
100 template<typename T, typename = std::enable_if_t<std::is_floating_point_v<T>>>
101 operator T() const // NOLINT
102 { return static_cast<T>(serial()); }
103
107 operator std::tm() const; // NOLINT
108
112 [[nodiscard]] std::chrono::system_clock::time_point chrono() const;
113
117 [[nodiscard]] double serial() const;
118
124 [[nodiscard]] std::tm tm() const;
125
126 private:
127 double m_serial{1.0};
128 };
129} // namespace OpenXLSX
130
131#ifdef _MSC_VER
132# pragma warning(pop)
133#endif
134
135#endif // OPENXLSX_XLDATETIME_HPP
Manages date and time values according to the Excel 1900 date system.
Definition XLDateTime.hpp:30
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
XLDateTime & operator=(const XLDateTime &other)=default
Definition IZipArchive.hpp:18
std::string toString(const XLCellValue &v)
Definition XLFormulaUtils.cpp:29