2 * Copyright 2014 Canonical Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19#include "relativetimeformatter.h"
26#include <glib/gi18n.h>
31RelativeTimeFormatter::RelativeTimeFormatter(QObject *parent)
32 : GDateTimeFormatter(parent)
36 /* Check the system locale setting to see if the format is 24-hour
37 time or 12-hour time */
42 static const char *formats_24h[] = {"%H", "%R", "%T", "%OH", "%k", NULL};
43 const char* t_fmt = nl_langinfo(T_FMT);
45 for (i=0; formats_24h[i]!=NULL; i++)
46 if (strstr(t_fmt, formats_24h[i]) != NULL)
54 DATE_PROXIMITY_YESTERDAY,
56 DATE_PROXIMITY_TOMORROW,
57 DATE_PROXIMITY_LAST_WEEK,
58 DATE_PROXIMITY_NEXT_WEEK,
62static date_proximity_t
63getDateProximity(GDateTime* now, GDateTime* time)
65 date_proximity_t prox = DATE_PROXIMITY_FAR;
66 gint now_year, now_month, now_day;
67 gint time_year, time_month, time_day;
69 // does it happen today?
70 g_date_time_get_ymd(now, &now_year, &now_month, &now_day);
71 g_date_time_get_ymd(time, &time_year, &time_month, &time_day);
72 if ((now_year == time_year) && (now_month == time_month) && (now_day == time_day)) {
73 return DATE_PROXIMITY_TODAY;
76 // did it happen yesterday?
77 GDateTime* tomorrow = g_date_time_add_days(now, -1);
78 gint tom_year, tom_month, tom_day;
79 g_date_time_get_ymd(tomorrow, &tom_year, &tom_month, &tom_day);
80 g_date_time_unref(tomorrow);
81 if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day)) {
82 return DATE_PROXIMITY_YESTERDAY;
85 // does it happen tomorrow?
86 if (prox == DATE_PROXIMITY_FAR)
88 GDateTime* tomorrow = g_date_time_add_days(now, 1);
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;
98 // does it happen this week?
99 if (prox == DATE_PROXIMITY_FAR) {
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),
106 if (g_date_time_compare(time, last_week_bound) >= 0)
107 prox = DATE_PROXIMITY_LAST_WEEK;
109 g_date_time_unref(last_week);
110 g_date_time_unref(last_week_bound);
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),
117 if (g_date_time_compare(time, next_week_bound) <= 0)
118 prox = DATE_PROXIMITY_NEXT_WEEK;
120 g_date_time_unref(next_week);
121 g_date_time_unref(next_week_bound);
130 * _ a time yesterday should be shown as (e.g. “Yesterday 3:55 PM”)
131 * _ a time today should be shown as just the time (e.g. “3:55 PM”)
132 * _ a time tomorrow should be shown as(e.g. “Tomorrow 3:55 PM”)
133 * _ a time any other day this week should be shown as the short version of the
134 * day and time (e.g. “Wed 3:55 PM”)
135 * weekday (e.g. “Friday”)
136 * _ a time after this week should be shown as the short version of the day,
137 * date, and time (e.g. “Wed 21 Apr 3:55 PM”)
138 * _ in addition, when presenting the times of upcoming events, the time should
139 * be followed by the timezone if it is different from the one the computer
140 * is currently set to. For example, “Wed 3:55 PM UTC−5”.
142 * TODO - keep inline with indicator-datetime
144char* generate_full_format_string_at_time (GDateTime* now,
147 GString* ret = g_string_new (NULL);
150 const date_proximity_t prox = getDateProximity(now, then);
152 if (is_locale_12h()) {
154 case DATE_PROXIMITY_YESTERDAY:
155 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
156 This format string is used for showing, on a 12-hour clock, times that happen yesterday.
157 (\u2003 is a unicode em space which is slightly wider than a normal space.)
158 en_US example: "Yesterday\u2003%l:%M %p" --> "Yesterday 1:00 PM" */
159 g_string_assign (ret, dgettext("indicator-datetime", "Yesterday\u2003%l:%M %p"));
162 case DATE_PROXIMITY_TODAY:
163 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
164 This format string is used for showing, on a 12-hour clock, times that happened today.
165 en_US example: "%l:%M %p" --> "1:00 PM" */
166 g_string_assign (ret, dgettext("indicator-datetime", "%l:%M %p"));
169 case DATE_PROXIMITY_TOMORROW:
170 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
171 This format string is used for showing, on a 12-hour clock, events/appointments that happen tomorrow.
172 (\u2003 is a unicode em space which is slightly wider than a normal space.)
173 en_US example: "Tomorrow\u2003%l:%M %p" --> "Tomorrow 1:00 PM" */
174 g_string_assign (ret, dgettext("indicator-datetime", "Tomorrow\u2003%l:%M %p"));
177 case DATE_PROXIMITY_LAST_WEEK:
178 case DATE_PROXIMITY_NEXT_WEEK:
179 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
180 This format string is used for showing, on a 12-hour clock, times that happened in the last week.
181 (\u2003 is a unicode em space which is slightly wider than a normal space.)
182 en_US example: "%a\u2003%l:%M %p" --> "Fri 1:00 PM" */
183 g_string_assign (ret, dgettext("indicator-datetime", "%a\u2003%l:%M %p"));
186 case DATE_PROXIMITY_FAR:
187 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
188 This format string is used for showing, on a 12-hour clock, times that happened before a week from now.
189 (\u2003 is a unicode em space which is slightly wider than a normal space.)
190 en_US example: "%a %d %b\u2003%l:%M %p" --> "Fri Oct 31 1:00 PM"
191 en_GB example: "%a %b %d\u2003%l:%M %p" --> "Fri 31 Oct 1:00 PM" */
192 g_string_assign (ret, dgettext("indicator-datetime", "%a %d %b\u2003%l:%M %p"));
198 case DATE_PROXIMITY_YESTERDAY:
199 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
200 This format string is used for showing, on a 24-hour clock, times that happen yesterday.
201 (\u2003 is a unicode em space which is slightly wider than a normal space.)
202 en_US example: "Yesterday\u2003%l:%M %p" --> "Yesterday 13:00" */
203 g_string_assign (ret, dgettext("indicator-datetime", "Yesterday\u2003%H:%M"));
206 case DATE_PROXIMITY_TODAY:
207 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
208 This format string is used for showing, on a 24-hour clock, times that happened today.
209 en_US example: "%H:%M" --> "13:00" */
210 g_string_assign (ret, dgettext("indicator-datetime", "%H:%M"));
213 case DATE_PROXIMITY_TOMORROW:
214 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
215 This format string is used for showing, on a 24-hour clock, events/appointments that happen tomorrow.
216 (\u2003 is a unicode em space which is slightly wider than a normal space.)
217 en_US example: "Tomorrow\u2003%l:%M %p" --> "Tomorrow 13:00" */
218 g_string_assign (ret, dgettext("indicator-datetime", "Tomorrow\u2003%H:%M"));
221 case DATE_PROXIMITY_LAST_WEEK:
222 case DATE_PROXIMITY_NEXT_WEEK:
223 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
224 This format string is used for showing, on a 24-hour clock, times that happened in the last week.
225 (\u2003 is a unicode em space which is slightly wider than a normal space.)
226 en_US example: "%a\u2003%H:%M" --> "Fri 13:00" */
227 g_string_assign (ret, dgettext("indicator-datetime", "%a\u2003%H:%M"));
230 case DATE_PROXIMITY_FAR:
231 /* Translators, please edit/rearrange these strftime(3) tokens to suit your locale!
232 This format string is used for showing, on a 24-hour clock, times that happened before a week from now.
233 (\u2003 is a unicode em space which is slightly wider than a normal space.)
234 en_US example: "%a %d %b\u2003%H:%M" --> "Fri Oct 31 13:00"
235 en_GB example: "%a %b %d\u2003%H:%M" --> "Fri 31 Oct 13:00" */
236 g_string_assign (ret, dgettext("indicator-datetime", "%a %d %b\u2003%H:%M"));
242 return g_string_free (ret, FALSE);
245QString RelativeTimeFormatter::format() const
247 GDateTime* now = g_date_time_new_from_unix_utc(QDateTime::currentMSecsSinceEpoch() / 1000);
248 if (!now) { return QString(); }
250 GDateTime* then = g_date_time_new_from_unix_local(time());
251 if (!then) { return QString(); }
253 char* time_format = generate_full_format_string_at_time(now, then);
255 QString str(QString::fromUtf8(time_format));
258 g_date_time_unref(now);
259 g_date_time_unref(then);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list