Initial commit (software version 0.2.0)
[movie-schedule] / src / utils / timeutils.cpp
1 // Copyright 2010 Jochen Becher
2 //
3 // This file is part of MovieSchedule.
4 //
5 // MovieSchedule is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // MovieSchedule is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with MovieSchedule.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "timeutils.h"
19
20 TimeUtils::TimeUtils()
21 {
22 }
23
24 QTime TimeUtils::FromTimeString(const QString &time)
25 {
26     int pos = 0;
27     int n = time.length();
28     int hours = 0;
29     int minutes = 0;
30     int m = 0;
31     while (pos < n && time[pos].isDigit()) {
32         hours = 10 * hours + time[pos].digitValue();
33         ++pos;
34         ++m;
35     }
36     if (m == 0 || pos >= n || time[pos] != ':') {
37         return QTime();
38     }
39     ++pos;
40     m = 0;
41     while (pos < n && time[pos].isDigit()) {
42         minutes = 10 * minutes + time[pos].digitValue();
43         ++pos;
44         ++m;
45     }
46     if (m == 0) {
47         return QTime();
48     }
49     if (pos < n) {
50         QString s = time.mid(pos);
51         if (s.compare("pm", Qt::CaseInsensitive) == 0) {
52             if (hours < 12) {
53                 hours += 12;
54             }
55         } else if (s.compare("am", Qt::CaseInsensitive) == 0) {
56             // nothing to do
57         } else {
58             return QTime();
59         }
60     } else {
61         // assume pm as default
62         if (hours < 12) {
63             hours += 12;
64         }
65     }
66     return QTime(hours, minutes);
67 }
68
69 QString TimeUtils::ToTimeString(const QTime &time)
70 {
71     QString s = time.toString(Qt::SystemLocaleShortDate);
72     return s;
73 }
74
75 QString TimeUtils::ToDateString(const QDate &date)
76 {
77     QString date_name;
78     if (date == QDate::currentDate()) {
79         date_name = QObject::tr("Today", "DayPainter");
80     } else if (date == QDate::currentDate().addDays(1)) {
81         date_name = QObject::tr("Tomorrow", "DayPainter");
82     } else {
83         date_name = date.toString("dddd");
84     }
85     return date_name;
86 }