MythTV  master
mythdate.cpp
Go to the documentation of this file.
1 #include <QtGlobal>
2 #include <QCoreApplication>
3 
4 #include "mythcorecontext.h"
5 #include "mythdate.h"
6 
7 namespace MythDate
8 {
9 
10 QDateTime current(bool stripped)
11 {
12  QDateTime rettime = QDateTime::currentDateTimeUtc();
13  if (stripped)
14  rettime = rettime.addMSecs(-rettime.time().msec());
15  return rettime;
16 }
17 
18 QString current_iso_string(bool stripped)
19 {
20  return MythDate::current(stripped).toString(Qt::ISODate);
21 }
22 
23 QDateTime as_utc(const QDateTime &old_dt)
24 {
25  QDateTime dt(old_dt);
26  dt.setTimeSpec(Qt::UTC);
27  return dt;
28 }
29 
30 QDateTime fromString(const QString &dtstr)
31 {
32  QDateTime dt;
33  if (dtstr.isEmpty())
34  return as_utc(dt);
35 
36  if (!dtstr.contains("-") && dtstr.length() == 14)
37  {
38  // must be in yyyyMMddhhmmss format
39  dt = QDateTime::fromString(dtstr, "yyyyMMddhhmmss");
40  }
41  else
42  {
44  }
45 
46  return as_utc(dt);
47 }
48 
49 MBASE_PUBLIC QDateTime fromString(const QString &str, const QString &format)
50 {
51  QDateTime dt = QDateTime::fromString(str, format);
52  dt.setTimeSpec(Qt::UTC);
53  return dt;
54 }
55 
56 #if QT_VERSION < QT_VERSION_CHECK(5,8,0)
57 
68 MBASE_PUBLIC QDateTime fromTime_t(uint seconds)
69 {
70  QDateTime dt = QDateTime::fromTime_t(seconds);
71  return dt.toUTC();
72 }
73 
74 #else
75 
89 {
90  QDateTime dt = QDateTime::fromSecsSinceEpoch(seconds);
91  return dt.toUTC();
92 }
93 #endif
94 
101 QString toString(const QDateTime &raw_dt, uint format)
102 {
103  QString result;
104 
105  if (!raw_dt.isValid())
106  return result;
107 
108  // if no format is set default to UTC for ISO/file/DB dates.
109  if (!((format & kOverrideUTC) || (format & kOverrideLocal)))
110  {
111  format |= ((ISODate|kFilename|kDatabase) & format) ?
113  }
114 
115  QDateTime datetime =
116  (format & kOverrideUTC) ? raw_dt.toUTC() : raw_dt.toLocalTime();
117 
118  if (format & kDatabase)
119  return datetime.toString("yyyy-MM-dd hh:mm:ss");
120 
121  if (format & MythDate::ISODate)
122  return datetime.toString(Qt::ISODate);
123 
124  if (format & MythDate::kRFC822) // RFC 822 - RFC 7231 Sect 7.1.1.1 - HTTP Date
125  return datetime.toUTC().toString("ddd, dd MMM yyyy hh:mm:ss").append(" GMT");
126 
127  if (format & kFilename)
128  return datetime.toString("yyyyMMddhhmmss");
129 
130  if (format & kScreenShotFilename)
131  return datetime.toString("yyyy-MM-ddThh-mm-ss.zzz");
132 
133  if (format & kDateEither)
134  result += toString(datetime.date(), format);
135 
136  if (format & kTime)
137  {
138  if (!result.isEmpty())
139  result.append(", ");
140 
141  QString timeformat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
142  result += datetime.time().toString(timeformat);
143  }
144 
145  return result;
146 }
147 
153 QString toString(const QDate &date, uint format)
154 {
155  QString result;
156 
157  if (!date.isValid())
158  return result;
159 
160  if (format & kDateEither)
161  {
162  QString stringformat;
163  if (format & kDateShort)
164  stringformat = gCoreContext->GetSetting("ShortDateFormat", "ddd d");
165  else
166  stringformat = gCoreContext->GetSetting("DateFormat", "ddd d MMMM");
167 
168  if (format & kAddYear)
169  {
170  if (!stringformat.contains("yy")) // Matches both 2 or 4 digit year
171  stringformat.append(" yyyy");
172  }
173 
174  if (format & kAutoYear)
175  {
176  if (!stringformat.contains("yy") // Matches both 2 or 4 digit year
177  && date.year() != QDateTime::currentDateTime().date().year())
178  stringformat.append(" yyyy");
179  }
180 
181  if (format & ~kDateShort)
182  {
183  QDate now = current().toLocalTime().date();
184  if ((format & kSimplify) && (now == date))
185  result = QCoreApplication::translate("(Common)", "Today");
186  else if (((format & kSimplify) != 0U) && (now.addDays(-1) == date))
187  result = QCoreApplication::translate("(Common)", "Yesterday");
188  else if (((format & kSimplify) != 0U) && (now.addDays(1) == date))
189  result = QCoreApplication::translate("(Common)", "Tomorrow");
190  }
191 
192  if (result.isEmpty())
193  result = gCoreContext->GetQLocale().toString(date, stringformat);
194  }
195 
196  return result;
197 }
198 
203 int toSeconds( const QTime &time )
204 {
205  if (!time.isValid())
206  return 0;
207 
208  int nSecs = time.hour() * 3600;
209  nSecs += (time.minute() * 60 );
210  nSecs += time.second();
211 
212  return nSecs;
213 }
214 
215 }; // namespace MythDate
MBASE_PUBLIC QDateTime fromString(const QString &str, const QString &format)
Converts dy in format to QDateTime.
Definition: mythdate.cpp:49
Default local time.
Definition: mythdate.h:17
Default UTC, "yyyyMMddhhmmss".
Definition: mythdate.h:15
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
Definition: mythdate.cpp:18
unsigned int uint
Definition: compat.h:140
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
int toSeconds(const QTime &time)
Returns the total number of seconds since midnight of the supplied QTime.
Definition: mythdate.cpp:203
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:23
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MBASE_PUBLIC QDateTime fromSecsSinceEpoch(uint seconds)
This function takes the number of seconds since the start of the epoch and returns a QDateTime with t...
Definition: mythdate.cpp:88
Add year only if different from current year.
Definition: mythdate.h:25
Default local time.
Definition: mythdate.h:19
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
QString GetSetting(const QString &key, const QString &defaultval="")
Present date/time in UTC.
Definition: mythdate.h:28
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:23
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:101
"yyyy-MM-ddThh-mm-ss.zzz"
Definition: mythdate.h:26
HTTP Date format.
Definition: mythdate.h:27
string timeformat
Definition: mythburn.py:142
QLocale GetQLocale(void)
Present date/time in localtime.
Definition: mythdate.h:29
Default UTC, database format.
Definition: mythdate.h:24
Add year to string if not included.
Definition: mythdate.h:22
Default local time.
Definition: mythdate.h:18
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
Default UTC.
Definition: mythdate.h:14