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* 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;
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;
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 dgettext_datetime(
const char *text)
132 return dgettext(
"indicator-datetime", text);
150 char* generate_full_format_string_at_time (GDateTime* now,
153 GString* ret = g_string_new (
nullptr);
155 if (then !=
nullptr) {
156 const date_proximity_t prox = getDateProximity(now, then);
158 if (is_locale_12h()) {
160 case DATE_PROXIMITY_YESTERDAY:
165 g_string_assign (ret, dgettext_datetime(
"Yesterday\u2003%l:%M %p"));
168 case DATE_PROXIMITY_TODAY:
172 g_string_assign (ret, dgettext_datetime(
"%l:%M %p"));
175 case DATE_PROXIMITY_TOMORROW:
180 g_string_assign (ret, dgettext_datetime(
"Tomorrow\u2003%l:%M %p"));
183 case DATE_PROXIMITY_LAST_WEEK:
184 case DATE_PROXIMITY_NEXT_WEEK:
189 g_string_assign (ret, dgettext_datetime(
"%a\u2003%l:%M %p"));
192 case DATE_PROXIMITY_FAR:
198 g_string_assign (ret, dgettext_datetime(
"%a %d %b\u2003%l:%M %p"));
204 case DATE_PROXIMITY_YESTERDAY:
209 g_string_assign (ret, dgettext_datetime(
"Yesterday\u2003%H:%M"));
212 case DATE_PROXIMITY_TODAY:
216 g_string_assign (ret, dgettext_datetime(
"%H:%M"));
219 case DATE_PROXIMITY_TOMORROW:
224 g_string_assign (ret, dgettext_datetime(
"Tomorrow\u2003%H:%M"));
227 case DATE_PROXIMITY_LAST_WEEK:
228 case DATE_PROXIMITY_NEXT_WEEK:
233 g_string_assign (ret, dgettext_datetime(
"%a\u2003%H:%M"));
236 case DATE_PROXIMITY_FAR:
242 g_string_assign (ret, dgettext_datetime(
"%a %d %b\u2003%H:%M"));
248 return g_string_free (ret, FALSE);
251 QString RelativeTimeFormatter::format()
const
253 GDateTime* now = g_date_time_new_from_unix_utc(QDateTime::currentMSecsSinceEpoch() / 1000);
254 if (!now) {
return QString(); }
256 GDateTime* then = g_date_time_new_from_unix_local(time());
257 if (!then) {
return QString(); }
259 char* time_format = generate_full_format_string_at_time(now, then);
261 QString str(QString::fromUtf8(time_format));
264 g_date_time_unref(now);
265 g_date_time_unref(then);