MythTV master
mythdate.h
Go to the documentation of this file.
1#ifndef MYTH_DATE_H
2#define MYTH_DATE_H
3
4#include <cstdint>
5
6#include <QDateTime>
7#include <QString>
8
9#include "mythbaseexp.h"
10#include "mythchrono.h"
11
12namespace MythDate
13{
14
16{
18 kFilename = 0x000100,
19 kDateFull = 0x000200,
20 kDateShort = 0x000400,
22 kTime = 0x000800,
25 kAddYear = 0x001000,
26 kSimplify = 0x002000,
27 kDatabase = 0x004000,
28 kAutoYear = 0x008000,
30 kRFC822 = 0x020000,
31 kOverrideUTC = 0x100000,
32 kOverrideLocal = 0x200000,
33};
34
37MBASE_PUBLIC QDateTime current(bool stripped = false);
40MBASE_PUBLIC QString current_iso_string(bool stripped = false);
42MBASE_PUBLIC QDateTime as_utc(const QDateTime &dt);
43
45MBASE_PUBLIC QDateTime fromString(const QString &dtstr);
47MBASE_PUBLIC QDateTime fromString(const QString &dt, const QString &format);
48MBASE_PUBLIC QDateTime fromSecsSinceEpoch(int64_t seconds);
51 const QDateTime &datetime, uint format = MythDate::kDateTimeFull);
54 QDate date, uint format = MythDate::kDateFull);
55
56// Returns the total number of seconds since midnight
57MBASE_PUBLIC std::chrono::seconds toSeconds( QTime time );
58
59MBASE_PUBLIC std::chrono::milliseconds currentMSecsSinceEpochAsDuration(void);
60MBASE_PUBLIC std::chrono::seconds secsInPast (const QDateTime& past);
61MBASE_PUBLIC std::chrono::seconds secsInFuture (const QDateTime& future);
62
63MBASE_PUBLIC QString formatTime(std::chrono::milliseconds msecs,
64 QString fmt = "HH:mm:ss");
65
66} // namespace MythDate
67
76inline int ternary_compare(const QDateTime& a, const QDateTime& b)
77{
78 if (a < b) return -1;
79 if (a > b) return +1;
80 return 0; // a == b
81}
82
83#endif // MYTH_DATE_H
unsigned int uint
Definition: compat.h:68
#define MBASE_PUBLIC
Definition: mythbaseexp.h:8
int ternary_compare(const QDateTime &a, const QDateTime &b)
balanced ternary (three way) comparison This is equivalent to C++20's operator <=>.
Definition: mythdate.h:76
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
Definition: mythdate.cpp:23
QString formatTime(std::chrono::milliseconds msecs, QString fmt)
Format a milliseconds time value.
Definition: mythdate.cpp:242
std::chrono::milliseconds currentMSecsSinceEpochAsDuration(void)
Definition: mythdate.cpp:207
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:28
std::chrono::seconds secsInPast(const QDateTime &past)
Definition: mythdate.cpp:212
MBASE_PUBLIC QDateTime fromSecsSinceEpoch(int64_t seconds)
This function takes the number of seconds since the start of the epoch and returns a QDateTime with t...
Definition: mythdate.cpp:81
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:93
@ kOverrideUTC
Present date/time in UTC.
Definition: mythdate.h:31
@ kScreenShotFilename
"yyyy-MM-ddThh-mm-ss.zzz"
Definition: mythdate.h:29
@ kDateTimeFull
Default local time.
Definition: mythdate.h:23
@ kFilename
Default UTC, "yyyyMMddhhmmss".
Definition: mythdate.h:18
@ kRFC822
HTTP Date format.
Definition: mythdate.h:30
@ kSimplify
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:26
@ kDateFull
Default local time.
Definition: mythdate.h:19
@ kDateTimeShort
Default local time.
Definition: mythdate.h:24
@ ISODate
Default UTC.
Definition: mythdate.h:17
@ kTime
Default local time.
Definition: mythdate.h:22
@ kAddYear
Add year to string if not included.
Definition: mythdate.h:25
@ kOverrideLocal
Present date/time in localtime.
Definition: mythdate.h:32
@ kDateEither
Default local time.
Definition: mythdate.h:21
@ kDateShort
Default local time.
Definition: mythdate.h:20
@ kDatabase
Default UTC, database format.
Definition: mythdate.h:27
@ kAutoYear
Add year only if different from current year.
Definition: mythdate.h:28
std::chrono::seconds secsInFuture(const QDateTime &future)
Definition: mythdate.cpp:217
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:39
std::chrono::seconds toSeconds(QTime time)
Returns the total number of seconds since midnight of the supplied QTime.
Definition: mythdate.cpp:195
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:15