Ticket #11538: date_test.cpp

File date_test.cpp, 2.9 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 dt = QDateTime::fromString(isoDateString, Qt::ISODate).toUTC();
73 qDebug() << QString("Result: %1, TimeSpec: %2").arg(dt.toString()).arg(dt.timeSpec());
74 }
75
76 if (!dt.isValid())
77 {
78 static bool warned_once_on_implicit_utc = false;
79 if (!warned_once_on_implicit_utc)
80 {
81 qWarning() << "No explicit time zone found, "
82 "guessing implicit UTC! Please consider enhancing "
83 "the guide source to provice explicit UTC or local "
84 "time instead.";
85 warned_once_on_implicit_utc = true;
86 }
87 dt = tmpDT;
88 }
89
90 dt.setTimeSpec(Qt::UTC);
91
92// timestr = MythDate::toString(dt, MythDate::kFilename);
93}
94
95int main(int argc, char *argv[])
96{
97 QDateTime dt;
98 QString dstr;
99
100 if (argc != 2)
101 {
102 fprintf(stderr,"Usage: %s <Date String>\n",argv[0]);
103 exit(1);
104 }
105
106 dstr = QString(argv[1]);
107 fromXMLTVDate(dstr,dt);
108 qDebug() << QString("Localtime QDateTime: %1").arg(dt.toLocalTime().toString());
109}