1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/*
* Copyright © 2000 Eazel, Inc.
* Copyright © 2002 Jorn Baayen
*
* This file is part of Epiphany.
*
* Epiphany is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Epiphany is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Epiphany. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: John Sullivan <sullivan@eazel.com>
* Jorn Baayen
*/
/* Following code is copied from Rhythmbox rb-cut-and-paste-code.c */
#include <config.h>
#include <string.h>
#include <gdesktop-enums.h>
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <glib.h>
#include "ephy-settings.h"
#include "ephy-time-helpers.h"
/* Legal conversion specifiers, as specified in the C standard. */
#define C_STANDARD_STRFTIME_CHARACTERS "aAbBcdHIjmMpSUwWxXyYZ"
#define C_STANDARD_NUMERIC_STRFTIME_CHARACTERS "dHIjmMSUwWyY"
#define SUS_EXTENDED_STRFTIME_MODIFIERS "EO"
/**
* eel_strdup_strftime:
*
* Cover for standard date-and-time-formatting routine strftime that returns
* a newly-allocated string of the correct size. The caller is responsible
* for g_free-ing the returned string.
*
* Besides the buffer management, there are two differences between this
* and the library strftime:
*
* 1) The modifiers "-" and "_" between a "%" and a numeric directive
* are defined as for the GNU version of strftime. "-" means "do not
* pad the field" and "_" means "pad with spaces instead of zeroes".
* 2) Non-ANSI extensions to strftime are flagged at runtime with a
* warning, so it's easy to notice use of the extensions without
* testing with multiple versions of the library.
*
* @format: format string to pass to strftime. See strftime documentation
* for details.
* @time_pieces: date/time, in struct format.
*
* Return value: Newly allocated string containing the formatted time.
**/
char *
eel_strdup_strftime (const char *format,
struct tm *time_pieces)
{
g_autoptr (GString) string = NULL;
const char *remainder, *percent;
char code[4], buffer[512];
char *piece, *result;
g_autofree gchar *converted = NULL;
size_t string_length;
gboolean strip_leading_zeros, turn_leading_zeros_to_spaces;
char modifier;
int i;
/* Format could be translated, and contain UTF-8 chars,
* so convert to locale encoding which strftime uses */
converted = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);
if (!converted)
converted = g_strdup (format);
string = g_string_new ("");
remainder = converted;
/* Walk from % character to % character. */
for (;;) {
percent = strchr (remainder, '%');
if (percent == NULL) {
g_string_append (string, remainder);
break;
}
g_string_append_len (string, remainder,
percent - remainder);
/* Handle the "%" character. */
remainder = percent + 1;
switch (*remainder) {
case '-':
strip_leading_zeros = TRUE;
turn_leading_zeros_to_spaces = FALSE;
remainder++;
break;
case '_':
strip_leading_zeros = FALSE;
turn_leading_zeros_to_spaces = TRUE;
remainder++;
break;
case '%':
g_string_append_c (string, '%');
remainder++;
continue;
case '\0':
g_warning ("Trailing %% passed to eel_strdup_strftime");
g_string_append_c (string, '%');
continue;
default:
strip_leading_zeros = FALSE;
turn_leading_zeros_to_spaces = FALSE;
break;
}
modifier = 0;
if (strchr (SUS_EXTENDED_STRFTIME_MODIFIERS, *remainder) != NULL) {
modifier = *remainder;
remainder++;
if (*remainder == 0) {
g_warning ("Unfinished %%%c modifier passed to eel_strdup_strftime", modifier);
break;
}
}
if (strchr (C_STANDARD_STRFTIME_CHARACTERS, *remainder) == NULL) {
g_warning ("eel_strdup_strftime does not support "
"non-standard escape code %%%c",
*remainder);
}
/* Convert code to strftime format. We have a fixed
* limit here that each code can expand to a maximum
* of 512 bytes, which is probably OK. There's no
* limit on the total size of the result string.
*/
i = 0;
code[i++] = '%';
if (modifier != 0) {
#ifdef HAVE_STRFTIME_EXTENSION
code[i++] = modifier;
#endif
}
code[i++] = *remainder;
code[i++] = '\0';
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
/* Format string under control of caller, since this is a wrapper for strftime. */
string_length = strftime (buffer, sizeof (buffer),
code, time_pieces);
#pragma GCC diagnostic pop
if (string_length == 0) {
/* We could put a warning here, but there's no
* way to tell a successful conversion to
* empty string from a failure.
*/
buffer[0] = '\0';
}
/* Strip leading zeros if requested. */
piece = buffer;
if (strip_leading_zeros || turn_leading_zeros_to_spaces) {
if (strchr (C_STANDARD_NUMERIC_STRFTIME_CHARACTERS, *remainder) == NULL) {
g_warning ("eel_strdup_strftime does not support "
"modifier for non-numeric escape code %%%c%c",
remainder[-1],
*remainder);
}
if (*piece == '0') {
do {
piece++;
} while (*piece == '0');
if (!g_ascii_isdigit (*piece)) {
piece--;
}
}
if (turn_leading_zeros_to_spaces) {
memset (buffer, ' ', piece - buffer);
piece = buffer;
}
}
remainder++;
/* Add this piece. */
g_string_append (string, piece);
}
/* Convert the string back into utf-8. */
result = g_locale_to_utf8 (string->str, -1, NULL, NULL, NULL);
return result;
}
/* Based on evolution/mail/message-list.c:filter_date() */
char *
ephy_time_helpers_utf_friendly_time (time_t date)
{
time_t nowdate;
time_t yesdate;
struct tm then, now, yesterday;
const char *format = NULL;
char *str = NULL;
gboolean done = FALSE;
GSettings *settings;
gboolean use_24;
settings = ephy_settings_get ("org.gnome.desktop.interface");
use_24 = g_settings_get_enum (settings, "clock-format") == G_DESKTOP_CLOCK_FORMAT_24H;
nowdate = time (NULL);
if (date == 0)
return NULL;
localtime_r (&date, &then);
localtime_r (&nowdate, &now);
if (then.tm_mday == now.tm_mday &&
then.tm_mon == now.tm_mon &&
then.tm_year == now.tm_year) {
if (!use_24) {
/* Translators: "friendly time" string for the current day, strftime format. like "Today 12∶34 am" */
format = _("Today %I∶%M %p");
} else {
/* Translators: "friendly time" string for the current day, strftime format. like "Today 15∶34" */
format = _("Today %H∶%M");
}
done = TRUE;
}
if (!done) {
yesdate = nowdate - 60 * 60 * 24;
localtime_r (&yesdate, &yesterday);
if (then.tm_mday == yesterday.tm_mday &&
then.tm_mon == yesterday.tm_mon &&
then.tm_year == yesterday.tm_year) {
if (!use_24) {
/* Translators: "friendly time" string for the previous day,
* strftime format. e.g. "Yesterday 12∶34 am"
*/
format = _("Yesterday %I∶%M %p");
} else {
/* Translators: "friendly time" string for the previous day,
* strftime format. e.g. "Yesterday 15∶34"
*/
format = _("Yesterday %H∶%M");
}
done = TRUE;
}
}
if (!done) {
int i;
for (i = 2; i < 7; i++) {
yesdate = nowdate - 60 * 60 * 24 * i;
localtime_r (&yesdate, &yesterday);
if (then.tm_mday == yesterday.tm_mday &&
then.tm_mon == yesterday.tm_mon &&
then.tm_year == yesterday.tm_year) {
if (!use_24) {
/* Translators: "friendly time" string for a day in the current week,
* strftime format. e.g. "Wed 12∶34 am"
*/
format = _("%a %I∶%M %p");
} else {
/* Translators: "friendly time" string for a day in the current week,
* strftime format. e.g. "Wed 15∶34"
*/
format = _("%a %H∶%M");
}
done = TRUE;
break;
}
}
}
if (!done) {
if (then.tm_year == now.tm_year) {
if (!use_24) {
/* Translators: "friendly time" string for a day in the current year,
* strftime format. e.g. "Feb 12 12∶34 am"
*/
format = _("%b %d %I∶%M %p");
} else {
/* Translators: "friendly time" string for a day in the current year,
* strftime format. e.g. "Feb 12 15∶34"
*/
format = _("%b %d %H∶%M");
}
} else {
/* Translators: "friendly time" string for a day in a different year,
* strftime format. e.g. "Feb 12 1997"
*/
format = _("%b %d %Y");
}
}
if (format != NULL) {
str = eel_strdup_strftime (format, &then);
}
if (str == NULL) {
/* impossible time or broken locale settings */
str = g_strdup (_("Unknown"));
}
return str;
}
|