Ticket #11538: date_test.2.cpp

File date_test.2.cpp, 3.0 KB (added by richard@…, 13 years ago)
Line 
1#include <QString>
2#include <QStringList>
3#include <QDateTime>
4#include <QDebug>
5
6static void fromXMLTVDate(QString &timestr, QDateTime &dt)
7{
8 // The XMLTV spec requires dates to either be in UTC/GMT or to specify a
9 // valid timezone. We are sticking to the spec and require all grabbers
10 // to comply.
11
12 if (timestr.isEmpty())
13 {
14 qWarning() << "Found empty Date/Time in XMLTV data, ignoring";
15 return;
16 }
17
18 QStringList split = timestr.split(" ");
19 QString ts = split[0];
20 QDateTime tmpDT;
21 tmpDT.setTimeSpec(Qt::LocalTime);
22
23 // UTC/GMT, just strip
24 if (ts.endsWith('Z'))
25 ts.truncate(ts.length()-1);
26
27 if (ts.length() == 14)
28 {
29 tmpDT = QDateTime::fromString(ts, "yyyyMMddHHmmss");
30 }
31 else if (ts.length() == 12)
32 {
33 tmpDT = QDateTime::fromString(ts, "yyyyMMddHHmm");
34 }
35 else if (ts.length() == 8)
36 {
37 tmpDT = QDateTime::fromString(ts, "yyyyMMdd");
38 }
39 else if (ts.length() == 6)
40 {
41 tmpDT = QDateTime::fromString(ts, "yyyyMM");
42 }
43 else if (ts.length() == 4)
44 {
45 tmpDT = QDateTime::fromString(ts, "yyyy");
46 }
47
48 if (!tmpDT.isValid())
49 {
50 qWarning() <<
51 QString("Ignoring unknown timestamp format: %1")
52 .arg(ts);
53 return;
54 }
55
56 if (split.size() > 1)
57 {
58 QString tmp = split[1].trimmed();
59
60 // These shouldn't be required and they aren't ISO 8601 but the
61 // xmltv spec mentions these and just these so handle them just in
62 // case
63 if (tmp == "GMT" || tmp == "UTC")
64 tmp = "+0000";
65 else if (tmp == "BST")
66 tmp = "+0100";
67
68 // While this seems like a hack, it's better than what was done before
69 QString isoDateString = QString("%1 %2").arg(tmpDT.toString(Qt::ISODate))
70 .arg(tmp);
71 qDebug() << QString("Parsing: %1").arg(isoDateString);
72 QDateTime dt2 = QDateTime::fromString(isoDateString, Qt::ISODate);
73 qDebug() << QString("Parsed Result: %1, TimeSpec: %2").arg(dt2.toString()).arg(dt2.timeSpec());
74 dt = dt2.toUTC();
75 qDebug() << QString("toUTC() Result: %1, TimeSpec: %2").arg(dt.toString()).arg(dt.timeSpec());
76 }
77
78 if (!dt.isValid())
79 {
80 static bool warned_once_on_implicit_utc = false;
81 if (!warned_once_on_implicit_utc)
82 {
83 qWarning() << "No explicit time zone found, "
84 "guessing implicit UTC! Please consider enhancing "
85 "the guide source to provice explicit UTC or local "
86 "time instead.";
87 warned_once_on_implicit_utc = true;
88 }
89 dt = tmpDT;
90 }
91
92 dt.setTimeSpec(Qt::UTC);
93
94// timestr = MythDate::toString(dt, MythDate::kFilename);
95}
96
97int main(int argc, char *argv[])
98{
99 QDateTime dt;
100 QString dstr;
101
102 if (argc != 2)
103 {
104 fprintf(stderr,"Usage: %s <Date String>\n",argv[0]);
105 exit(1);
106 }
107
108 dstr = QString(argv[1]);
109 fromXMLTVDate(dstr,dt);
110 qDebug() << QString("Localtime QDateTime: %1").arg(dt.toLocalTime().toString());
111}