Debian Codegraph @

src/legacy/e_date.c

raw symbols SCIP
1 #include <config.h>
2
3 #include <glib.h>
4 #include <glib/gi18n.h>
5
6 #include <string.h>
7
8 #include "e_date.h"
9
10 /*
11 all this code comes from evolution
12 - e-util.c
13 - message-list.c
14 */
15
16
17 static size_t
18 e_strftime (char *s,
19 size_t max,
20 const char *fmt,
21 const struct tm *tm)
22 {
23 #ifdef HAVE_LKSTRFTIME
24 return strftime (s, max, fmt, tm);
25 #else
26 char *c, *ffmt, *ff;
27 size_t ret;
28
29 ffmt = g_strdup (fmt);
30 ff = ffmt;
31 while ((c = strstr (ff, "%l")) != NULL)
32 {
33 c[1] = 'I';
34 ff = c;
35 }
36
37 ff = ffmt;
38 while ((c = strstr (ff, "%k")) != NULL)
39 {
40 c[1] = 'H';
41 ff = c;
42 }
43
44 ret = strftime (s, max, ffmt, tm);
45 g_free (ffmt);
46 return ret;
47 #endif
48 }
49
50
51 /**
52 * Function to do a last minute fixup of the AM/PM stuff if the locale
53 * and gettext haven't done it right. Most English speaking countries
54 * except the USA use the 24 hour clock (UK, Australia etc). However
55 * since they are English nobody bothers to write a language
56 * translation (gettext) file. So the locale turns off the AM/PM, but
57 * gettext does not turn on the 24 hour clock. Leaving a mess.
58 *
59 * This routine checks if AM/PM are defined in the locale, if not it
60 * forces the use of the 24 hour clock.
61 *
62 * The function itself is a front end on strftime and takes exactly
63 * the same arguments.
64 *
65 * TODO: Actually remove the '%p' from the fixed up string so that
66 * there isn't a stray space.
67 **/
68
69 static size_t
70 e_strftime_fix_am_pm (char *s,
71 size_t max,
72 const char *fmt,
73 const struct tm *tm)
74 {
75 char buf[10];
76 char *sp;
77 char *ffmt;
78 size_t ret;
79
80 if (strstr (fmt, "%p") == NULL && strstr (fmt, "%P") == NULL)
81 {
82 /* No AM/PM involved - can use the fmt string directly */
83 ret = e_strftime (s, max, fmt, tm);
84 }
85 else
86 {
87 /* Get the AM/PM symbol from the locale */
88 e_strftime (buf, 10, "%p", tm);
89
90 if (buf[0])
91 {
92 /**
93 * AM/PM have been defined in the locale
94 * so we can use the fmt string directly
95 **/
96 ret = e_strftime (s, max, fmt, tm);
97 }
98 else
99 {
100 /**
101 * No AM/PM defined by locale
102 * must change to 24 hour clock
103 **/
104 ffmt = g_strdup (fmt);
105 for (sp = ffmt; (sp = strstr (sp, "%l")); sp++)
106 /**
107 * Maybe this should be 'k', but I have never
108 * seen a 24 clock actually use that format
109 **/
110 sp[1] = 'H';
111 for (sp = ffmt; (sp = strstr (sp, "%I")); sp++)
112 sp[1] = 'H';
113 ret = e_strftime (s, max, ffmt, tm);
114 g_free (ffmt);
115 }
116 }
117 return(ret);
118 }
119
120 static size_t
121 e_utf8_strftime_fix_am_pm (char *s,
122 size_t max,
123 const char *fmt,
124 const struct tm *tm)
125 {
126 size_t sz, ret;
127 char *locale_fmt, *buf;
128
129 locale_fmt = g_locale_from_utf8 (fmt, -1, NULL, &sz, NULL);
130 if (!locale_fmt)
131 return 0;
132
133 ret = e_strftime_fix_am_pm (s, max, locale_fmt, tm);
134 if (!ret)
135 {
136 g_free (locale_fmt);
137 return 0;
138 }
139
140 buf = g_locale_to_utf8 (s, ret, NULL, &sz, NULL);
141 if (!buf)
142 {
143 g_free (locale_fmt);
144 return 0;
145 }
146
147 if (sz >= max)
148 {
149 char *tmp = buf + max - 1;
150 tmp = g_utf8_find_prev_char (buf, tmp);
151 if (tmp)
152 sz = tmp - buf;
153 else
154 sz = 0;
155 }
156 memcpy (s, buf, sz);
157 s[sz] = '\0';
158 g_free (locale_fmt);
159 g_free (buf);
160 return sz;
161 }
162
163
164 static char *
165 filter_date (time_t date)
166 {
167 time_t nowdate = time (NULL);
168 time_t yesdate;
169 struct tm then, now, yesterday;
170 char buf[26];
171 gboolean done = FALSE;
172
173 if (date == 0)
174 // xgettext: ? stands for unknown
175 return g_strdup (_("?"));
176
177 localtime_r (&date, &then);
178 localtime_r (&nowdate, &now);
179 if (then.tm_mday == now.tm_mday &&
180 then.tm_mon == now.tm_mon &&
181 then.tm_year == now.tm_year)
182 {
183 e_utf8_strftime_fix_am_pm (buf, 26, _("Today %l∶%M %p"), &then);
184 done = TRUE;
185 }
186 if (!done)
187 {
188 yesdate = nowdate - 60 * 60 * 24;
189 localtime_r (&yesdate, &yesterday);
190 if (then.tm_mday == yesterday.tm_mday &&
191 then.tm_mon == yesterday.tm_mon &&
192 then.tm_year == yesterday.tm_year)
193 {
194 e_utf8_strftime_fix_am_pm (buf, 26, _("Yesterday %l∶%M %p"), &then);
195 done = TRUE;
196 }
197 }
198 if (!done)
199 {
200 int i;
201 for (i = 2; i < 7; i++)
202 {
203 yesdate = nowdate - 60 * 60 * 24 * i;
204 localtime_r (&yesdate, &yesterday);
205 if (then.tm_mday == yesterday.tm_mday &&
206 then.tm_mon == yesterday.tm_mon &&
207 then.tm_year == yesterday.tm_year)
208 {
209 e_utf8_strftime_fix_am_pm (buf, 26, _("%a %l∶%M %p"), &then);
210 done = TRUE;
211 break;
212 }
213 }
214 }
215 if (!done)
216 {
217 if (then.tm_year == now.tm_year)
218 e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %l∶%M %p"), &then);
219 else
220 e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %Y"), &then);
221 }
222
223 return g_strdup (buf);
224 }
225
226
227
228
229 char *
230 procman_format_date_for_display (time_t d)
231 {
232 return filter_date (d);
233 }