sptk2 logo
SPTK Home Page
DateTime.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <chrono>
30#include <ctime>
31#include <iostream>
32#include <sptk5/String.h>
33#include <sptk5/sptk.h>
34
35namespace sptk {
36
42class DateTimeFormat;
43
44class DateTime;
45
49class SP_EXPORT TimeZone
50{
51public:
55 static String name();
56
61 static std::chrono::minutes offset();
62
68
73 static void set(const sptk::String& timeZoneName);
74};
75
76
85class SP_EXPORT DateTime
86{
87 friend class DateTimeFormat;
88
89public:
93 using clock = std::chrono::system_clock;
94
98 using time_point = std::chrono::system_clock::time_point;
99
103 using duration = std::chrono::system_clock::duration;
104
108 enum class PrintAccuracy
109 {
110 MINUTES = 1,
111 SECONDS = 2,
112 MILLISECONDS = 3
113 };
114
118 static constexpr int PF_RFC_DATE = 1;
119 static constexpr int PF_TIMEZONE = 2;
120 static constexpr int PF_12HOURS = 4;
121 static constexpr int PF_GMT = 16;
122
123 enum class Format
124 {
125 DATE_FORMAT,
126 DATE_PARTS_ORDER,
127 FULL_TIME_FORMAT,
128 SHORT_TIME_FORMAT,
129 MONTH_NAME,
130 WEEKDAY_NAME
131 };
132
138 static String format(Format dtFormat, size_t arg = 0);
139
143 static char dateSeparator();
144
148 static char timeSeparator();
149
153 static bool time24Mode();
154
158 static void time24Mode(bool t24mode);
159
170 DateTime(short y, short m, short d, short h = 0, short mm = 0, short s = 0, short ms = 0);
171
176 explicit DateTime(const char* dateStr = nullptr);
177
181 DateTime(const DateTime& dt) = default;
182
186 DateTime(DateTime&& dt) = default;
187
192 explicit DateTime(const time_point& dt);
193
198 explicit DateTime(const duration& dt);
199
203 const time_point& timePoint() const
204 {
205 return m_dateTime;
206 }
207
211 DateTime& operator=(const DateTime& date) = default;
212
218 void formatDate(std::ostream& str, int printFlags = 0) const;
219
226 void formatTime(std::ostream& str, int printFlags = 0, PrintAccuracy printAccuracy = PrintAccuracy::SECONDS) const;
227
232 {
233 return m_dateTime.time_since_epoch();
234 }
235
239 static DateTime Now();
240
245 static DateTime convertCTime(const time_t tt);
246
250 int16_t daysInMonth() const;
251
255 int16_t dayOfWeek() const;
256
261
266
270 DateTime date() const;
271
276 String dateString(int printFlags = 0) const;
277
283 String timeString(int printFlags = 0, PrintAccuracy printAccuracy = PrintAccuracy::SECONDS) const;
284
290 String isoDateTimeString(PrintAccuracy printAccuracy = PrintAccuracy::SECONDS, bool gmt = false) const;
291
296 static std::chrono::minutes timeZoneOffset();
297
303
309
313 explicit operator String() const
314 {
315 return dateString() + " " + timeString();
316 }
317
321 explicit operator time_t() const
322 {
323 return clock::to_time_t(m_dateTime);
324 }
325
329 void decodeDate(short* year, short* month, short* day, short* wday, short* yday, bool gmt = false) const;
330
334 void decodeTime(short* h, short* m, short* s, short* ms, bool gmt = false) const;
335
339 bool zero() const
340 {
341 return m_dateTime.time_since_epoch().count() == 0;
342 }
343
344private:
345 time_point m_dateTime;
346
347 static String _dateFormat;
348 static String _fullTimeFormat;
349 static String _shortTimeFormat;
350 static String _datePartsOrder;
351 static char _dateSeparator;
352 static char _timeSeparator;
353 static Strings _weekDayNames;
354 static Strings _monthNames;
355
356 static bool _time24Mode;
357 static String _timeZoneName;
358 static std::chrono::minutes _timeZoneOffset;
359 static int _isDaylightSavingsTime;
360};
361
362
366SP_EXPORT bool operator<(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
367
371SP_EXPORT bool operator<=(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
372
376SP_EXPORT bool operator>(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
377
381SP_EXPORT bool operator>=(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
382
386SP_EXPORT bool operator==(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
387
391SP_EXPORT bool operator!=(const sptk::DateTime& dt1, const sptk::DateTime& dt2);
392
397
402
407
411SP_EXPORT double duration2seconds(const sptk::DateTime::duration& duration);
412
416} // namespace sptk
Definition: DateTime.h:86
static DateTime convertCTime(const time_t tt)
static char timeSeparator()
static String format(Format dtFormat, size_t arg=0)
static bool isDaylightSavingsTime()
std::chrono::system_clock::time_point time_point
Definition: DateTime.h:98
DateTime date() const
int16_t dayOfWeek() const
void decodeDate(short *year, short *month, short *day, short *wday, short *yday, bool gmt=false) const
void decodeTime(short *h, short *m, short *s, short *ms, bool gmt=false) const
static DateTime Now()
int16_t daysInMonth() const
DateTime(DateTime &&dt)=default
static std::chrono::minutes timeZoneOffset()
String isoDateTimeString(PrintAccuracy printAccuracy=PrintAccuracy::SECONDS, bool gmt=false) const
const time_point & timePoint() const
Definition: DateTime.h:203
DateTime & operator=(const DateTime &date)=default
String dayOfWeekName() const
DateTime(const time_point &dt)
static char dateSeparator()
std::chrono::system_clock::duration duration
Definition: DateTime.h:103
duration sinceEpoch() const
Definition: DateTime.h:231
DateTime(const DateTime &dt)=default
static bool time24Mode()
PrintAccuracy
Definition: DateTime.h:109
bool zero() const
Definition: DateTime.h:339
DateTime(const duration &dt)
std::chrono::system_clock clock
Definition: DateTime.h:93
DateTime(short y, short m, short d, short h=0, short mm=0, short s=0, short ms=0)
String dateString(int printFlags=0) const
DateTime(const char *dateStr=nullptr)
String timeString(int printFlags=0, PrintAccuracy printAccuracy=PrintAccuracy::SECONDS) const
void formatDate(std::ostream &str, int printFlags=0) const
String monthName() const
static void time24Mode(bool t24mode)
void formatTime(std::ostream &str, int printFlags=0, PrintAccuracy printAccuracy=PrintAccuracy::SECONDS) const
static String timeZoneName()
Definition: String.h:49
Definition: Strings.h:52
Definition: DateTime.h:50
static void set(const sptk::String &timeZoneName)
static int isDaylightSavingsTime()
static String name()
static std::chrono::minutes offset()
SP_EXPORT sptk::DateTime::duration operator-(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT bool operator>(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT sptk::DateTime operator+(const sptk::DateTime &dt1, const sptk::DateTime::duration &duration)
SP_EXPORT bool operator<=(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT bool operator<(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT bool operator==(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT bool operator!=(const sptk::DateTime &dt1, const sptk::DateTime &dt2)
SP_EXPORT double duration2seconds(const sptk::DateTime::duration &duration)
SP_EXPORT bool operator>=(const sptk::DateTime &dt1, const sptk::DateTime &dt2)

Fri Oct 14 2022 09:58:31: SPTK 5.4.1