summaryrefslogtreecommitdiff
path: root/src/e_date.c
blob: bca316558dbc66dec86136a456c256af32553921 (plain)
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
#include <config.h>

#include <glib.h>
#include <glib/gi18n.h>

#include <string.h>

#include "e_date.h"

/*
  all this code comes from evolution
  - e-util.c
  - message-list.c
*/


static size_t e_strftime(char *s, size_t max, const char *fmt, const struct tm *tm)
{
#ifdef HAVE_LKSTRFTIME
    return strftime(s, max, fmt, tm);
#else
    char *c, *ffmt, *ff;
    size_t ret;

    ffmt = g_strdup(fmt);
    ff = ffmt;
    while ((c = strstr(ff, "%l")) != NULL) {
        c[1] = 'I';
        ff = c;
    }

    ff = ffmt;
    while ((c = strstr(ff, "%k")) != NULL) {
        c[1] = 'H';
        ff = c;
    }

    ret = strftime(s, max, ffmt, tm);
    g_free(ffmt);
    return ret;
#endif
}


/**
 * Function to do a last minute fixup of the AM/PM stuff if the locale
 * and gettext haven't done it right. Most English speaking countries
 * except the USA use the 24 hour clock (UK, Australia etc). However
 * since they are English nobody bothers to write a language
 * translation (gettext) file. So the locale turns off the AM/PM, but
 * gettext does not turn on the 24 hour clock. Leaving a mess.
 *
 * This routine checks if AM/PM are defined in the locale, if not it
 * forces the use of the 24 hour clock.
 *
 * The function itself is a front end on strftime and takes exactly
 * the same arguments.
 *
 * TODO: Actually remove the '%p' from the fixed up string so that
 * there isn't a stray space.
 **/

static size_t e_strftime_fix_am_pm(char *s, size_t max, const char *fmt, const struct tm *tm)
{
    char buf[10];
    char *sp;
    char *ffmt;
    size_t ret;

    if (strstr(fmt, "%p")==NULL && strstr(fmt, "%P")==NULL) {
        /* No AM/PM involved - can use the fmt string directly */
        ret=e_strftime(s, max, fmt, tm);
    } else {
        /* Get the AM/PM symbol from the locale */
        e_strftime (buf, 10, "%p", tm);

        if (buf[0]) {
            /**
             * AM/PM have been defined in the locale
             * so we can use the fmt string directly
             **/
            ret=e_strftime(s, max, fmt, tm);
        } else {
            /**
             * No AM/PM defined by locale
             * must change to 24 hour clock
             **/
            ffmt=g_strdup(fmt);
            for (sp=ffmt; (sp=strstr(sp, "%l")); sp++) {
                /**
                 * Maybe this should be 'k', but I have never
                 * seen a 24 clock actually use that format
                 **/
                sp[1]='H';
            }
            for (sp=ffmt; (sp=strstr(sp, "%I")); sp++) {
                sp[1]='H';
            }
            ret=e_strftime(s, max, ffmt, tm);
            g_free(ffmt);
        }
    }
    return(ret);
}

static size_t
e_utf8_strftime_fix_am_pm(char *s, size_t max, const char *fmt, const struct tm *tm)
{
    size_t sz, ret;
    char *locale_fmt, *buf;

    locale_fmt = g_locale_from_utf8(fmt, -1, NULL, &sz, NULL);
    if (!locale_fmt)
        return 0;

    ret = e_strftime_fix_am_pm(s, max, locale_fmt, tm);
    if (!ret) {
        g_free (locale_fmt);
        return 0;
    }

    buf = g_locale_to_utf8(s, ret, NULL, &sz, NULL);
    if (!buf) {
        g_free (locale_fmt);
        return 0;
    }

    if (sz >= max) {
        char *tmp = buf + max - 1;
        tmp = g_utf8_find_prev_char(buf, tmp);
        if (tmp)
            sz = tmp - buf;
        else
            sz = 0;
    }
    memcpy(s, buf, sz);
    s[sz] = '\0';
    g_free(locale_fmt);
    g_free(buf);
    return sz;
}


static char *
filter_date (time_t date)
{
    time_t nowdate = time(NULL);
    time_t yesdate;
    struct tm then, now, yesterday;
    char buf[26];
    gboolean done = FALSE;

    if (date == 0)
        // xgettext: ? stands for unknown
        return g_strdup (_("?"));

    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) {
        e_utf8_strftime_fix_am_pm (buf, 26, _("Today %l:%M %p"), &then);
        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) {
            e_utf8_strftime_fix_am_pm (buf, 26, _("Yesterday %l:%M %p"), &then);
            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) {
                e_utf8_strftime_fix_am_pm (buf, 26, _("%a %l:%M %p"), &then);
                done = TRUE;
                break;
            }
        }
    }
    if (!done) {
        if (then.tm_year == now.tm_year) {
            e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %l:%M %p"), &then);
        } else {
            e_utf8_strftime_fix_am_pm (buf, 26, _("%b %d %Y"), &then);
        }
    }
#if 0
#ifdef CTIME_R_THREE_ARGS
    ctime_r (&date, buf, 26);
#else
    ctime_r (&date, buf);
#endif
#endif

    return g_strdup (buf);
}




char *
procman_format_date_for_display(time_t d)
{
    return filter_date(d);
}