on xenial help sign in
Fixed up relative time formatting & tests
commitbdfb1b0a8edb09b572dd55ad83f0fc87a6ea8af1 authorNick Dedekind <nick.dedekind@canonical.com> dateWed 3 Sep 2014, 13:11 (12 years ago) parente0990ee
5 files changed+204−52
plugins/Utils/relativetimeformatter.cpp+89−42 plugins/Utils/timeformatter.cpp+7−1 plugins/Utils/timeformatter.h+2 tests/plugins/Utils/CMakeLists.txt+2−2 tests/plugins/Utils/timeformattertest.cpp+104−7
plugins/Utils/relativetimeformatter.cpp+89−42
@@ -15,21 +15,25 @@
1515 *
1616 */
1717
18+// Local
1819#include "relativetimeformatter.h"
1920
21+// Qt
2022#include <QDateTime>
2123
24+// Other
2225#include <glib.h>
26+#include <glib/gi18n.h>
2327#include <locale.h>
2428#include <langinfo.h>
25−#include <libintl.h>
29+#include <string.h>
2630
2731RelativeTimeFormatter::RelativeTimeFormatter(QObject *parent)
2832 : GDateTimeFormatter(parent)
2933{
3034}
3135
32−/* Check the system locale setting to see if the format is 24-hour
36+ /* Check the system locale setting to see if the format is 24-hour
3337 time or 12-hour time */
3438gboolean
3539is_locale_12h(void)
@@ -45,15 +49,15 @@ is_locale_12h(void)
4549 return TRUE;
4650}
4751
48−
4952typedef enum
5053{
51− DATE_PROXIMITY_TODAY,
5254 DATE_PROXIMITY_YESTERDAY,
53− DATE_PROXIMITY_WEEK,
55+ DATE_PROXIMITY_TODAY,
56+ DATE_PROXIMITY_TOMORROW,
57+ DATE_PROXIMITY_LAST_WEEK,
58+ DATE_PROXIMITY_NEXT_WEEK,
5459 DATE_PROXIMITY_FAR
55−}
56−date_proximity_t;
60+} date_proximity_t;
5761
5862static date_proximity_t
5963getDateProximity(GDateTime* now, GDateTime* time)
@@ -69,44 +73,68 @@ getDateProximity(GDateTime* now, GDateTime* time)
6973 return DATE_PROXIMITY_TODAY;
7074 }
7175
72− // does it happen tomorrow?
76+ // did it happen yesterday?
7377 GDateTime* tomorrow = g_date_time_add_days(now, -1);
7478 gint tom_year, tom_month, tom_day;
7579 g_date_time_get_ymd(tomorrow, &tom_year, &tom_month, &tom_day);
76− if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day))
77− return DATE_PROXIMITY_YESTERDAY;
7880 g_date_time_unref(tomorrow);
81+ if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day)) {
82+ return DATE_PROXIMITY_YESTERDAY;
83+ }
84+
85+ // does it happen tomorrow?
86+ if (prox == DATE_PROXIMITY_FAR)
87+ {
88+ GDateTime* tomorrow = g_date_time_add_days(now, 1);
89+
90+ gint tom_year, tom_month, tom_day;
91+ g_date_time_get_ymd(tomorrow, &tom_year, &tom_month, &tom_day);
92+ g_date_time_unref(tomorrow);
93+ if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day)) {
94+ return DATE_PROXIMITY_TOMORROW;
95+ }
96+ }
7997
8098 // does it happen this week?
8199 if (prox == DATE_PROXIMITY_FAR) {
82− GDateTime* week = g_date_time_add_days(now, -6);
83− GDateTime* week_bound = g_date_time_new_local(g_date_time_get_year(week),
84− g_date_time_get_month(week),
85− g_date_time_get_day_of_month(week),
86− 23, 59, 59.9);
87−
88− if (g_date_time_compare(time, week_bound) >= 0)
89− prox = DATE_PROXIMITY_WEEK;
100+ if (g_date_time_compare(time, now) < 0) {
101+ GDateTime* last_week = g_date_time_add_days(now, -6);
102+ GDateTime* last_week_bound = g_date_time_new_local(g_date_time_get_year(last_week),
103+ g_date_time_get_month(last_week),
104+ g_date_time_get_day_of_month(last_week),
105+ 0, 0, 0);
106+ if (g_date_time_compare(time, last_week_bound) >= 0)
107+ prox = DATE_PROXIMITY_LAST_WEEK;
108+
109+ g_date_time_unref(last_week);
110+ g_date_time_unref(last_week_bound);
111+ } else {
112+ GDateTime* next_week = g_date_time_add_days(now, 6);
113+ GDateTime* next_week_bound = g_date_time_new_local(g_date_time_get_year(next_week),
114+ g_date_time_get_month(next_week),
115+ g_date_time_get_day_of_month(next_week),
116+ 23, 59, 59.9);
117+ if (g_date_time_compare(time, next_week_bound) <= 0)
118+ prox = DATE_PROXIMITY_NEXT_WEEK;
119+
120+ g_date_time_unref(next_week);
121+ g_date_time_unref(next_week_bound);
122+ }
90123
91− g_date_time_unref(week_bound);
92− g_date_time_unref(week);
93124 }
94125
95− return DATE_PROXIMITY_FAR;
126+ return prox;
96127}
97128
98129/**
130+ * _ a time yesterday should be shown as (e.g. “Yesterday 3:55 PM”)
99131 * _ a time today should be shown as just the time (e.g. “3:55 PM”)
100− * _ a full-day event today should be shown as “Today”
132+ * _ a time tomorrow should be shown as(e.g. “Tomorrow 3:55 PM”)
101133 * _ a time any other day this week should be shown as the short version of the
102134 * day and time (e.g. “Wed 3:55 PM”)
103− * _ a full-day event tomorrow should be shown as “Tomorrow”
104− * _ a full-day event another day this week should be shown as the
105135 * weekday (e.g. “Friday”)
106136 * _ a time after this week should be shown as the short version of the day,
107137 * date, and time (e.g. “Wed 21 Apr 3:55 PM”)
108− * _ a full-day event after this week should be shown as the short version of
109− * the day and date (e.g. “Wed 21 Apr”).
110138 * _ in addition, when presenting the times of upcoming events, the time should
111139 * be followed by the timezone if it is different from the one the computer
112140 * is currently set to. For example, “Wed 3:55 PM UTC−5”.
@@ -121,27 +149,36 @@ char* generate_full_format_string_at_time (GDateTime* now,
121149
122150 if (is_locale_12h()) {
123151 switch (prox) {
152+ case DATE_PROXIMITY_YESTERDAY:
153+ /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
154+ This format string is used for showing, on a 12-hour clock, times that happen yesterday.
155+ (\u2003 is a unicode em space which is slightly wider than a normal space.)
156+ en_US example: "Yesterday\u2003%l:%M %p" --> "Tomorrow 1:00 PM" */
157+ g_string_assign (ret, dgettext("indicator-datetime", "Yesterday\u2003%l:%M %p"));
158+ break;
159+
124160 case DATE_PROXIMITY_TODAY:
125161 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
126162 This format string is used for showing, on a 12-hour clock, times that happened today.
127163 en_US example: "%l:%M %p" --> "1:00 PM" */
128− g_string_assign (ret, gettext("%l:%M %p"));
164+ g_string_assign (ret, dgettext("indicator-datetime", "%l:%M %p"));
129165 break;
130166
131− case DATE_PROXIMITY_YESTERDAY:
167+ case DATE_PROXIMITY_TOMORROW:
132168 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
133− This format string is used for showing, on a 12-hour clock, times that happen yesterday.
169+ This format string is used for showing, on a 12-hour clock, events/appointments that happen tomorrow.
134170 (\u2003 is a unicode em space which is slightly wider than a normal space.)
135− en_US example: "Yesterday\u2003%l:%M %p" --> "Tomorrow 1:00 PM" */
136− g_string_assign (ret, gettext("Yesterday\u2003%l:%M %p"));
171+ en_US example: "Tomorrow\u2003%l:%M %p" --> "Tomorrow 1:00 PM" */
172+ g_string_assign (ret, dgettext("indicator-datetime", "Tomorrow\u2003%l:%M %p"));
137173 break;
138174
139− case DATE_PROXIMITY_WEEK:
175+ case DATE_PROXIMITY_LAST_WEEK:
176+ case DATE_PROXIMITY_NEXT_WEEK:
140177 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
141178 This format string is used for showing, on a 12-hour clock, times that happened in the last week.
142179 (\u2003 is a unicode em space which is slightly wider than a normal space.)
143180 en_US example: "%a\u2003%l:%M %p" --> "Fri 1:00 PM" */
144− g_string_assign (ret, gettext("%a\u2003%l:%M %p"));
181+ g_string_assign (ret, dgettext("indicator-datetime", "%a\u2003%l:%M %p"));
145182 break;
146183
147184 case DATE_PROXIMITY_FAR:
@@ -150,32 +187,42 @@ char* generate_full_format_string_at_time (GDateTime* now,
150187 (\u2003 is a unicode em space which is slightly wider than a normal space.)
151188 en_US example: "%a %d %b\u2003%l:%M %p" --> "Fri Oct 31 1:00 PM"
152189 en_GB example: "%a %b %d\u2003%l:%M %p" --> "Fri 31 Oct 1:00 PM" */
153− g_string_assign (ret, gettext("%a %d %b\u2003%l:%M %p"));
190+ g_string_assign (ret, dgettext("indicator-datetime", "%a %d %b\u2003%l:%M %p"));
154191 break;
155192 }
156193 } else {
157194 switch (prox) {
195+
196+ case DATE_PROXIMITY_YESTERDAY:
197+ /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
198+ This format string is used for showing, on a 24-hour clock, times that happen yesterday.
199+ (\u2003 is a unicode em space which is slightly wider than a normal space.)
200+ en_US example: "Yesterday\u2003%l:%M %p" --> "Tomorrow 13:00" */
201+ g_string_assign (ret, dgettext("indicator-datetime", "Yesterday\u2003%H:%M"));
202+ break;
203+
158204 case DATE_PROXIMITY_TODAY:
159205 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
160206 This format string is used for showing, on a 24-hour clock, times that happened today.
161207 en_US example: "%H:%M" --> "13:00" */
162− g_string_assign (ret, gettext("%H:%M"));
208+ g_string_assign (ret, dgettext("indicator-datetime", "%H:%M"));
163209 break;
164210
165− case DATE_PROXIMITY_YESTERDAY:
211+ case DATE_PROXIMITY_TOMORROW:
166212 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
167− This format string is used for showing, on a 24-hour clock, times that happen yesterday.
213+ This format string is used for showing, on a 24-hour clock, events/appointments that happen tomorrow.
168214 (\u2003 is a unicode em space which is slightly wider than a normal space.)
169− en_US example: "Yesterday\u2003%l:%M %p" --> "Tomorrow 13:00" */
170− g_string_assign (ret, gettext("Yesterday\u2003%H:%M"));
215+ en_US example: "Tomorrow\u2003%l:%M %p" --> "Tomorrow 13:00" */
216+ g_string_assign (ret, dgettext("indicator-datetime", "Tomorrow\u2003%H:%M"));
171217 break;
172218
173− case DATE_PROXIMITY_WEEK:
219+ case DATE_PROXIMITY_LAST_WEEK:
220+ case DATE_PROXIMITY_NEXT_WEEK:
174221 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
175222 This format string is used for showing, on a 24-hour clock, times that happened in the last week.
176223 (\u2003 is a unicode em space which is slightly wider than a normal space.)
177224 en_US example: "%a\u2003%H:%M" --> "Fri 13:00" */
178− g_string_assign (ret, gettext("%a\u2003%H:%M"));
225+ g_string_assign (ret, dgettext("indicator-datetime", "%a\u2003%H:%M"));
179226 break;
180227
181228 case DATE_PROXIMITY_FAR:
@@ -184,7 +231,7 @@ char* generate_full_format_string_at_time (GDateTime* now,
184231 (\u2003 is a unicode em space which is slightly wider than a normal space.)
185232 en_US example: "%a %d %b\u2003%H:%M" --> "Fri Oct 31 13:00"
186233 en_GB example: "%a %b %d\u2003%H:%M" --> "Fri 31 Oct 13:00" */
187− g_string_assign (ret, gettext("%a %d %b\u2003%H:%M"));
234+ g_string_assign (ret, dgettext("indicator-datetime", "%a %d %b\u2003%H:%M"));
188235 break;
189236 }
190237 }
plugins/Utils/timeformatter.cpp+7−1
@@ -121,6 +121,12 @@ TimeFormatter::TimeFormatter(QObject *parent): QObject(parent)
121121 g_bus_get (G_BUS_TYPE_SYSTEM, priv->cancellable, got_bus, priv);
122122}
123123
124+TimeFormatter::TimeFormatter(const QString &initialFormat, QObject *parent): TimeFormatter(parent)
125+{
126+ priv->format = initialFormat;
127+}
128+
129+
124130TimeFormatter::~TimeFormatter()
125131{
126132 if (priv->system_bus) {
@@ -177,7 +183,7 @@ QString TimeFormatter::formatTime() const
177183}
178184
179185GDateTimeFormatter::GDateTimeFormatter(QObject* parent)
180−: TimeFormatter(parent)
186+: TimeFormatter("%d-%m-%Y %I:%M%p", parent)
181187{
182188}
183189
plugins/Utils/timeformatter.h+2
@@ -48,6 +48,8 @@ Q_SIGNALS:
4848 void timeChanged(qint64 time);
4949
5050protected:
51+ TimeFormatter(const QString &initialFormat, QObject *parent = 0);
52+
5153 virtual QString formatTime() const;
5254
5355private:
tests/plugins/Utils/CMakeLists.txt+2−2
@@ -11,8 +11,8 @@ macro(run_tests)
1111 -o ${CMAKE_BINARY_DIR}/${_test}.xml,xunitxml
1212 -o -,txt)
1313 add_test(NAME ${_test} COMMAND ${testCommand})
14− set_tests_properties(${_test} PROPERTIES ENVIRONMENT "LC_ALL=C")
15− add_custom_target(${_test} env "LC_ALL=C" ${testCommand})
14+ set_tests_properties(${_test} PROPERTIES ENVIRONMENT "LC_ALL=C.UTF-8")
15+ add_custom_target(${_test} env "LC_ALL=C.UTF-8" ${testCommand})
1616 add_executable(${_test}Exec ${_test}.cpp modeltest.cpp)
1717 qt5_use_modules(${_test}Exec Test Core Qml)
1818 target_link_libraries(${_test}Exec
tests/plugins/Utils/timeformattertest.cpp+104−7
@@ -17,6 +17,9 @@
1717 */
1818
1919#include "timeformatter.h"
20+#include "relativetimeformatter.h"
21+
22+#include <locale.h>
2023
2124#include <QtTest>
2225#include <QDebug>
@@ -24,8 +27,24 @@
2427class TimeFormatterTest : public QObject
2528{
2629 Q_OBJECT
27−private Q_SLOTS:
30+private:
31+ void addTestData(QList<int> daysToAdd = QList<int>()) {
32+ QTest::addColumn<QDateTime>("time");
33+
34+ Q_FOREACH(int dayToAdd, daysToAdd) {
35+ QDateTime time = QDateTime::currentDateTime().addDays(dayToAdd);
36+ QDateTime am(time); am.setTime(QTime(1, 1, 1));
37+ QDateTime midday(time); midday.setTime(QTime(12, 0, 0));
38+ QDateTime pm(time); pm.setTime(QTime(17, 59, 59));
39+
40+ QTest::newRow(QString("%1day-am").arg(dayToAdd).toLatin1()) << am;
41+ QTest::newRow(QString("%1day-midday").arg(dayToAdd).toLatin1()) << midday;
42+ QTest::newRow(QString("%1day-pm").arg(dayToAdd).toLatin1()) << pm;
43+ }
2844
45+ }
46+
47+private Q_SLOTS:
2948 void initTestCase()
3049 {
3150 setenv("UNITY_TEST_ENV", "1", 1);
@@ -36,12 +55,12 @@ private Q_SLOTS:
3655 unsetenv("UNITY_TEST_ENV");
3756 }
3857
39− void testFormat()
58+ void testTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
59+ void testTimeFormatter_Format()
4060 {
61+ QFETCH(QDateTime, time);
4162 const QString format = "hh:mm dd.MM.yy";
4263
43− QDateTime time = QDateTime::currentDateTime();
44−
4564 TimeFormatter formatter;
4665 formatter.setTime(time.toMSecsSinceEpoch() * 1000);
4766 formatter.setFormat(format);
@@ -49,18 +68,96 @@ private Q_SLOTS:
4968 QCOMPARE(formatter.timeString(), time.toString(format));
5069 }
5170
52− void testFormatStrF()
71+ void tesGDatetTimeFormatter_Format_data() { addTestData(QList<int>() << 0); }
72+ void tesGDatetTimeFormatter_Format()
5373 {
74+ QFETCH(QDateTime, time);
5475 const QString format = "%d-%m-%Y %I:%M%p";
5576
56− QDateTime time = QDateTime::currentDateTime();
57−
5877 GDateTimeFormatter formatter;
5978 formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
6079 formatter.setFormat(format);
6180
6281 QCOMPARE(formatter.timeString(), time.toString("dd-MM-yyyy hh:mmAP"));
6382 }
83+
84+ void tesRelativeTimeFormatter_FarBack_data() { addTestData(QList<int>() << -200 << -7); }
85+ void tesRelativeTimeFormatter_FarBack()
86+ {
87+ QFETCH(QDateTime, time);
88+
89+ RelativeTimeFormatter formatter;
90+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
91+
92+ QCOMPARE(formatter.timeString(), time.toString("ddd d MMM\u2003HH:mm"));
93+ }
94+
95+ void tesRelativeTimeFormatter_WeekBack_data() { addTestData(QList<int>() << -6 << -2); }
96+ void tesRelativeTimeFormatter_WeekBack()
97+ {
98+ QFETCH(QDateTime, time);
99+
100+ RelativeTimeFormatter formatter;
101+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
102+
103+ QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
104+ }
105+
106+ void tesRelativeTimeFormatter_Yesterday_data() { addTestData(QList<int>() << -1); }
107+ void tesRelativeTimeFormatter_Yesterday()
108+ {
109+ QFETCH(QDateTime, time);
110+
111+ RelativeTimeFormatter formatter;
112+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
113+
114+ QCOMPARE(formatter.timeString(), QString("Yesterday\u2003%1").arg(time.toString("HH:mm")));
115+ }
116+
117+ void tesRelativeTimeFormatter_Today_data() { addTestData(QList<int>() << 0); }
118+ void tesRelativeTimeFormatter_Today()
119+ {
120+ QFETCH(QDateTime, time);
121+
122+ RelativeTimeFormatter formatter;
123+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
124+
125+ QCOMPARE(formatter.timeString(), time.toString("HH:mm"));
126+ }
127+
128+ void tesRelativeTimeFormatter_Tomorrow_data() { addTestData(QList<int>() << 1); }
129+ void tesRelativeTimeFormatter_Tomorrow()
130+ {
131+ QFETCH(QDateTime, time);
132+
133+ RelativeTimeFormatter formatter;
134+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
135+
136+ QCOMPARE(formatter.timeString(), QString("Tomorrow\u2003%1").arg(time.toString("HH:mm")));
137+ }
138+
139+
140+ void tesRelativeTimeFormatter_WeekForward_data() { addTestData(QList<int>() << 2 << 6); }
141+ void tesRelativeTimeFormatter_WeekForward()
142+ {
143+ QFETCH(QDateTime, time);
144+
145+ RelativeTimeFormatter formatter;
146+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
147+
148+ QCOMPARE(formatter.timeString(), time.toString("ddd\u2003HH:mm"));
149+ }
150+
151+ void tesRelativeTimeFormatter_FarForward_data() { addTestData(QList<int>() << 7 << 200); }
152+ void tesRelativeTimeFormatter_FarForward()
153+ {
154+ QFETCH(QDateTime, time);
155+
156+ RelativeTimeFormatter formatter;
157+ formatter.setTime(time.toMSecsSinceEpoch() / 1000); // strftime in seconds since epoc
158+
159+ QCOMPARE(formatter.timeString(), time.toString("ddd d MMM\u2003HH:mm"));
160+ }
64161};
65162
66163QTEST_GUILESS_MAIN(TimeFormatterTest)