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 
69 {
70  QDateTime dt = QDateTime::fromSecsSinceEpoch(seconds);
71  return dt.toUTC();
72 }
73 
80 QString toString(const QDateTime &raw_dt, uint format)
81 {
82  QString result;
83 
84  if (!raw_dt.isValid())
85  return result;
86 
87  // if no format is set default to UTC for ISO/file/DB dates.
88  if (!((format & kOverrideUTC) || (format & kOverrideLocal)))
89  {
90  format |= ((ISODate|kFilename|kDatabase) & format) ?
92  }
93 
94  QDateTime datetime =
95  (format & kOverrideUTC) ? raw_dt.toUTC() : raw_dt.toLocalTime();
96 
97  if (format & kDatabase)
98  return datetime.toString("yyyy-MM-dd hh:mm:ss");
99 
100  if (format & MythDate::ISODate)
101  return datetime.toString(Qt::ISODate);
102 
103  if (format & MythDate::kRFC822) // RFC 822 - RFC 7231 Sect 7.1.1.1 - HTTP Date
104  return datetime.toUTC().toString("ddd, dd MMM yyyy hh:mm:ss").append(" GMT");
105 
106  if (format & kFilename)
107  return datetime.toString("yyyyMMddhhmmss");
108 
109  if (format & kScreenShotFilename)
110  return datetime.toString("yyyy-MM-ddThh-mm-ss.zzz");
111 
112  if (format & kDateEither)
113  result += toString(datetime.date(), format);
114 
115  if (format & kTime)
116  {
117  if (!result.isEmpty())
118  result.append(", ");
119 
120  QString timeformat = gCoreContext->GetSetting("TimeFormat", "h:mm AP");
121  result += datetime.time().toString(timeformat);
122  }
123 
124  return result;
125 }
126 
132 QString toString(const QDate &date, uint format)
133 {
134  QString result;
135 
136  if (!date.isValid())
137  return result;
138 
139  if (format & kDateEither)
140  {
141  QString stringformat;
142  if (format & kDateShort)
143  stringformat = gCoreContext->GetSetting("ShortDateFormat", "ddd d");
144  else
145  stringformat = gCoreContext->GetSetting("DateFormat", "ddd d MMMM");
146 
147  if (format & kAddYear)
148  {
149  if (!stringformat.contains("yy")) // Matches both 2 or 4 digit year
150  stringformat.append(" yyyy");
151  }
152 
153  if (format & kAutoYear)
154  {
155  if (!stringformat.contains("yy") // Matches both 2 or 4 digit year
156  && date.year() != QDateTime::currentDateTime().date().year())
157  stringformat.append(" yyyy");
158  }
159 
160  if (format & ~kDateShort)
161  {
162  QDate now = current().toLocalTime().date();
163  if ((format & kSimplify) && (now == date))
164  result = QCoreApplication::translate("(Common)", "Today");
165  else if (((format & kSimplify) != 0U) && (now.addDays(-1) == date))
166  result = QCoreApplication::translate("(Common)", "Yesterday");
167  else if (((format & kSimplify) != 0U) && (now.addDays(1) == date))
168  result = QCoreApplication::translate("(Common)", "Tomorrow");
169  }
170 
171  if (result.isEmpty())
172  result = gCoreContext->GetQLocale().toString(date, stringformat);
173  }
174 
175  return result;
176 }
177 
182 int toSeconds( const QTime &time )
183 {
184  if (!time.isValid())
185  return 0;
186 
187  int nSecs = time.hour() * 3600;
188  nSecs += (time.minute() * 60 );
189  nSecs += time.second();
190 
191  return nSecs;
192 }
193 
194 }; // namespace MythDate
MythDate::toString
QString toString(const QDateTime &raw_dt, uint format)
Returns formatted string representing the time.
Definition: mythdate.cpp:80
MythDate::as_utc
QDateTime as_utc(const QDateTime &old_dt)
Returns copy of QDateTime with TimeSpec set to UTC.
Definition: mythdate.cpp:23
MythDate::toSeconds
int toSeconds(const QTime &time)
Returns the total number of seconds since midnight of the supplied QTime.
Definition: mythdate.cpp:182
MythDate::kScreenShotFilename
@ kScreenShotFilename
"yyyy-MM-ddThh-mm-ss.zzz"
Definition: mythdate.h:26
MythDate::kOverrideUTC
@ kOverrideUTC
Present date/time in UTC.
Definition: mythdate.h:28
MythDate::current
QDateTime current(bool stripped)
Returns current Date and Time in UTC.
Definition: mythdate.cpp:10
MythDate::fromString
MBASE_PUBLIC QDateTime fromString(const QString &str, const QString &format)
Converts dy in format to QDateTime.
Definition: mythdate.cpp:49
MythDate::fromSecsSinceEpoch
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:68
mythdate.h
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
MythCoreContext::GetQLocale
QLocale GetQLocale(void)
Definition: mythcorecontext.cpp:1827
MythDate::kFilename
@ kFilename
Default UTC, "yyyyMMddhhmmss".
Definition: mythdate.h:15
MythDate::kDateEither
@ kDateEither
Default local time.
Definition: mythdate.h:18
MythDate::kOverrideLocal
@ kOverrideLocal
Present date/time in localtime.
Definition: mythdate.h:29
mythburn.timeformat
string timeformat
Definition: mythburn.py:171
MythDate::kAutoYear
@ kAutoYear
Add year only if different from current year.
Definition: mythdate.h:25
MythDate::kDateShort
@ kDateShort
Default local time.
Definition: mythdate.h:17
uint
unsigned int uint
Definition: compat.h:140
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:57
MythDate::fromString
QDateTime fromString(const QString &dtstr)
Converts kFilename && kISODate formats to QDateTime.
Definition: mythdate.cpp:30
MythDate::kSimplify
@ kSimplify
Do Today/Yesterday/Tomorrow transform.
Definition: mythdate.h:23
MythDate::kAddYear
@ kAddYear
Add year to string if not included.
Definition: mythdate.h:22
mythcorecontext.h
MythDate::kRFC822
@ kRFC822
HTTP Date format.
Definition: mythdate.h:27
MythDate
Definition: mythdate.cpp:7
MythDate::ISODate
@ ISODate
Default UTC.
Definition: mythdate.h:14
MythDate::current_iso_string
QString current_iso_string(bool stripped)
Returns current Date and Time in UTC as a string.
Definition: mythdate.cpp:18
MythDate::kDatabase
@ kDatabase
Default UTC, database format.
Definition: mythdate.h:24
MythDate::kTime
@ kTime
Default local time.
Definition: mythdate.h:19
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:901