-
Notifications
You must be signed in to change notification settings - Fork 1
/
Date.cpp
154 lines (123 loc) · 3.86 KB
/
Date.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "Date.h"
#include <QSettings>
#include <QLocale>
#include <clockd/libtime.h>
#include "ChangeManager.h"
QString Date::m_fullFormat;
QString Date::m_partialFormat;
int Date::m_firstDayOfWeek;
void Date::init()
{
m_firstDayOfWeek = QSettings().value("Date/FirstDayOfWeek", 1).toInt();
loadFormatStrings();
}
QString Date::formatString(Format format)
{
switch (format) {
case Full:
return QSettings().value("Date/FullFormat").toString();
case Partial:
return QSettings().value("Date/PartialFormat").toString();
}
}
QString Date::sanitizeFormatString(QString string, Format format)
{
if (string.isEmpty()) {
switch (format) {
case Full:
return "dddd d MMMM yyyy";
case Partial:
string = QLocale().dateFormat(QLocale::ShortFormat);
return string.indexOf('M') < string.indexOf('d') ? "MM/dd" : "dd/MM";
}
}
return string;
}
void Date::setFormatStrings(const QString &fullFormat, const QString &partialFormat)
{
if (fullFormat != formatString(Full) || partialFormat != formatString(Partial)) {
QSettings().setValue("Date/FullFormat", fullFormat);
QSettings().setValue("Date/PartialFormat", partialFormat);
loadFormatStrings();
ChangeManager::bump();
}
}
int Date::firstDayOfWeek()
{
return m_firstDayOfWeek;
}
void Date::setFirstDayOfWeek(int firstDayOfWeek)
{
if (firstDayOfWeek != m_firstDayOfWeek) {
QSettings().setValue("Date/FirstDayOfWeek", m_firstDayOfWeek = firstDayOfWeek);
ChangeManager::bump();
}
}
// Map relative (user) to absolute (Qt) day of week
int Date::absDayOfWeek(int relDayOfWeek)
{
return 1 + ((relDayOfWeek-1) + (m_firstDayOfWeek-1) ) % 7;
}
// Map absolute (Qt) to relative (user) day of week
int Date::relDayOfWeek(int absDayOfWeek)
{
return 1 + ((absDayOfWeek-1) - (m_firstDayOfWeek-1) + 7) % 7;
}
// Calculate relative (user) week number and its year
int Date::relWeekNumber(const QDate &date, int *year)
{
const QDate givenMid = date.addDays(1 - relDayOfWeek(date.dayOfWeek()) + 3);
const QDate firstDay = QDate(givenMid.year(), 1, 1);
const QDate firstMid = firstDay.addDays(((4-1) - (relDayOfWeek(firstDay.dayOfWeek())-1) + 7) % 7);
if (year) *year = givenMid.year();
return firstMid.daysTo(givenMid) / 7 + 1;
}
QDateTime Date::toRemote(time_t t, const QString &zone)
{
// Treat empty zone as the current zone
if (zone.isEmpty())
return QDateTime::fromTime_t(t);
tm tm;
time_get_remote(t, zone.toLatin1(), &tm);
return QDateTime(QDate(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday),
QTime(tm.tm_hour, tm.tm_min, tm.tm_sec));
}
time_t Date::toUtc(const QDateTime &t, const QString &zone)
{
// Treat empty zone as the current zone
if (zone.isEmpty())
return t.toTime_t();
tm tm;
QDate date = t.date();
tm.tm_year = date.year() - 1900;
tm.tm_mon = date.month() - 1;
tm.tm_mday = date.day();
QTime time = t.time();
tm.tm_hour = time.hour();
tm.tm_min = time.minute();
tm.tm_sec = time.second();
tm.tm_isdst = -1;
return time_mktime(&tm, zone.toLatin1());
}
QString Date::toString(const QDate &date, Format format)
{
return QLocale().toString(date, internalFormatString(format));
}
QString Date::toString(const QDateTime &date, Format format, bool time)
{
return QLocale().toString(date, internalFormatString(format) + (time ? ", hh:mm" : QString()));
}
QString Date::internalFormatString(Format format)
{
switch (format) {
case Full:
return m_fullFormat;
case Partial:
return m_partialFormat;
}
}
void Date::loadFormatStrings()
{
m_fullFormat = sanitizeFormatString(formatString(Full), Full);
m_partialFormat = sanitizeFormatString(formatString(Partial), Partial);
}