19 #include "relativetimeformatter.h"
26 #include <glib/gi18n.h>
31 RelativeTimeFormatter::RelativeTimeFormatter(QObject *parent)
32 : GDateTimeFormatter(parent)
42 static const char *formats_24h[] = {
"%H",
"%R",
"%T",
"%OH",
"%k",
nullptr};
43 const char* t_fmt = nl_langinfo(T_FMT);
45 for (i=0; formats_24h[i]!=
nullptr; i++)
46 if (strstr(t_fmt, formats_24h[i]) !=
nullptr)
54 DATE_PROXIMITY_YESTERDAY,
56 DATE_PROXIMITY_TOMORROW,
57 DATE_PROXIMITY_LAST_WEEK,
58 DATE_PROXIMITY_NEXT_WEEK,
62 static date_proximity_t
63 getDateProximity(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;
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;
77 GDateTime* yesterday = g_date_time_add_days(now, -1);
78 gint tom_year, tom_month, tom_day;
79 g_date_time_get_ymd(yesterday, &tom_year, &tom_month, &tom_day);
80 g_date_time_unref(yesterday);
81 if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day)) {
82 return DATE_PROXIMITY_YESTERDAY;
86 GDateTime* tomorrow = g_date_time_add_days(now, 1);
87 g_date_time_get_ymd(tomorrow, &tom_year, &tom_month, &tom_day);
88 g_date_time_unref(tomorrow);
89 if ((tom_year == time_year) && (tom_month == time_month) && (tom_day == time_day)) {
90 return DATE_PROXIMITY_TOMORROW;
94 if (g_date_time_compare(time, now) < 0) {
95 GDateTime* last_week = g_date_time_add_days(now, -6);
96 GDateTime* last_week_bound = g_date_time_new_local(g_date_time_get_year(last_week),
97 g_date_time_get_month(last_week),
98 g_date_time_get_day_of_month(last_week),
100 if (g_date_time_compare(time, last_week_bound) >= 0)
101 prox = DATE_PROXIMITY_LAST_WEEK;
103 g_date_time_unref(last_week);
104 g_date_time_unref(last_week_bound);
106 GDateTime* next_week = g_date_time_add_days(now, 6);
107 GDateTime* next_week_bound = g_date_time_new_local(g_date_time_get_year(next_week),
108 g_date_time_get_month(next_week),
109 g_date_time_get_day_of_month(next_week),
111 if (g_date_time_compare(time, next_week_bound) <= 0)
112 prox = DATE_PROXIMITY_NEXT_WEEK;
114 g_date_time_unref(next_week);
115 g_date_time_unref(next_week_bound);
122 dgettext_datetime(
const char *text)
124 return dgettext(
"indicator-datetime", text);
142 char* generate_full_format_string_at_time (GDateTime* now,
145 GString* ret = g_string_new (
nullptr);
147 if (then !=
nullptr) {
148 const date_proximity_t prox = getDateProximity(now, then);
150 if (is_locale_12h()) {
152 case DATE_PROXIMITY_YESTERDAY:
157 g_string_assign (ret, dgettext_datetime(
"Yesterday\u2003%l:%M %p"));
160 case DATE_PROXIMITY_TODAY:
164 g_string_assign (ret, dgettext_datetime(
"%l:%M %p"));
167 case DATE_PROXIMITY_TOMORROW:
172 g_string_assign (ret, dgettext_datetime(
"Tomorrow\u2003%l:%M %p"));
175 case DATE_PROXIMITY_LAST_WEEK:
176 case DATE_PROXIMITY_NEXT_WEEK:
181 g_string_assign (ret, dgettext_datetime(
"%a\u2003%l:%M %p"));
184 case DATE_PROXIMITY_FAR:
190 g_string_assign (ret, dgettext_datetime(
"%a %d %b\u2003%l:%M %p"));
196 case DATE_PROXIMITY_YESTERDAY:
201 g_string_assign (ret, dgettext_datetime(
"Yesterday\u2003%H:%M"));
204 case DATE_PROXIMITY_TODAY:
208 g_string_assign (ret, dgettext_datetime(
"%H:%M"));
211 case DATE_PROXIMITY_TOMORROW:
216 g_string_assign (ret, dgettext_datetime(
"Tomorrow\u2003%H:%M"));
219 case DATE_PROXIMITY_LAST_WEEK:
220 case DATE_PROXIMITY_NEXT_WEEK:
225 g_string_assign (ret, dgettext_datetime(
"%a\u2003%H:%M"));
228 case DATE_PROXIMITY_FAR:
234 g_string_assign (ret, dgettext_datetime(
"%a %d %b\u2003%H:%M"));
240 return g_string_free (ret, FALSE);
243 QString RelativeTimeFormatter::format()
const
245 GDateTime* now = g_date_time_new_now_local();
246 if (!now) {
return QString(); }
248 GDateTime* then = g_date_time_new_from_unix_local(time());
249 if (!then) {
return QString(); }
251 char* time_format = generate_full_format_string_at_time(now, then);
253 QString str(QString::fromUtf8(time_format));
256 g_date_time_unref(now);
257 g_date_time_unref(then);