unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 805a1d92bf0eec308f1a5e7f434d508cfadd80c5 8302 bytes (raw)
name: lib/date.c 	 # note: path name is non-authoritative(*)

  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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
 
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "notmuch.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DAY	(24 * 60 * 60)

static void
today(struct tm *result, time_t after) {
    time_t	t;

    if (after)
	t = after;
    else
	time(&t);
    localtime_r(&t, result);
    result->tm_sec = result->tm_min = result->tm_hour = 0;
}

static int parse_today(const char *text, time_t *first, time_t *last, time_t after) {
    (void)after; /*disable unused paramter warning*/
    if (strcasecmp(text, "today") == 0) {
	struct tm n;
	today(&n, 0);
	*first = mktime(&n);
	*last = *first + DAY;
	return 0;
    }
    return 1;
}

static int parse_yesterday(const char *text, time_t *first, time_t *last, time_t after) {
    if (strcasecmp(text, "yesterday") == 0) {
	struct tm n;
	today(&n, 0);
	*last = mktime(&n);
	*first = *last - DAY;
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

static int parse_thisweek(const char *text, time_t *first, time_t *last, time_t after) {
    if (strcasecmp(text, "thisweek") == 0) {
	struct tm n;
	today(&n, 0);
	*first = mktime(&n) - (n.tm_wday * DAY);
	*last = *first + DAY * 7;
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

static int parse_lastweek(const char *text, time_t *first, time_t *last, time_t after) {
    if (strcasecmp(text, "lastweek") == 0) {
	struct tm n;
	today(&n, 0);
	*last = mktime(&n) - (n.tm_wday * DAY);
	*first = *last - DAY * 7;
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

static int parse_thismonth(const char *text, time_t *first, time_t *last, time_t after) {
    if (strcasecmp(text, "thismonth") == 0) {
	struct tm n;
	today(&n, 0);
	n.tm_mday = 1;
	*first = mktime(&n);
	if (n.tm_mon++ == 12) {
	    n.tm_mon = 0;
	    n.tm_year++;
	}
	*last = mktime(&n);
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

static int parse_lastmonth(const char *text, time_t *first, time_t *last, time_t after) {
    if (strcasecmp(text, "lastmonth") == 0) {
	struct tm n;
	today(&n, 0);
	n.tm_mday = 1;
	if (n.tm_mon == 0) {
	    n.tm_year--;
	    n.tm_mon = 11;
	} else
	    n.tm_mon--;
	*first = mktime(&n);
	if (n.tm_mon++ == 12) {
	    n.tm_mon = 0;
	    n.tm_year++;
	}
	*last = mktime(&n);
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

static const char *months[12][2] = {
    { "January", "Jan" },
    { "February", "Feb" },
    { "March", "Mar" },
    { "April", "Apr" },
    { "May", "May" },
    { "June", "Jun" },
    { "July", "Jul" },
    { "August", "Aug" },
    { "September", "Sep" },
    { "October", "Oct" },
    { "November", "Nov" },
    { "December", "Dec" },
};

static int year(const char *text, int *y) {
    char *end;
    *y = strtol(text, &end, 10);
    if (end == text)
	return 1;
    if (*end != '\0')
	return 1;
    if (*y < 1970 || *y > 2038)
	return 1;
    *y -= 1900;
    return 0;
}

static int month(const char *text, int *m) {
    char *end;
    int i;
    for (i = 0; i < 12; i++) {
	if (strcasecmp(text, months[i][0]) == 0 ||
	    strcasecmp(text, months[i][1]) == 0)
	{
	    *m = i;
	    return 0;
	}
    }
    *m = strtol(text, &end, 10);
    if (end == text)
	return 1;
    if (*end != '\0')
	return 1;
    if (*m < 1 || *m > 12)
	return 1;
    *m -= 1;
    return 0;
}

static int day(const char *text, int *d) {
    char *end;
    *d = strtol(text, &end, 10);
    if (end == text)
	return 1;
    if (*end != '\0')
	return 1;
    if (*d < 1 || *d > 31)
	return 1;
    return 0;
}

/* month[-day] */
static int parse_month(const char *text, time_t *first, time_t *last, time_t after) {
    int		m = 0, d = 0;
    int		i;
    struct tm	n;
    char	tmp[80];
    char	*t;
    char	*save;
    char	*token;

    if(strlen (text) >= sizeof (tmp))
	return 1;
    strcpy(tmp, text);

    t = tmp;
    save = NULL;
    i = 0;
    while ((token = strtok_r(t, "-", &save)) != NULL) {
	i++;
	switch(i) {
	case 1:
	    if (month(token, &m) != 0)
		return 1;
	    break;
	case 2:
	    if (day(token, &d) != 0)
		return 1;
	    break;
	default:
	    return 1;
	}
	t = NULL;
    }
    today(&n, after);
    if (after) {
	if (m < n.tm_mon)
	    n.tm_year++;
    } else {
	if (m > n.tm_mon)
	    n.tm_year--;
    }
    switch (i) {
    case 1:
	n.tm_mday = 1;
	n.tm_mon = m;
	*first = mktime(&n);
	if (++n.tm_mon > 11) {
	    n.tm_mon = 0;
	    n.tm_year++;
	}
	*last = mktime(&n);
	return 0;
    case 2:
	n.tm_mday = d;
	n.tm_mon = m;
	*first = mktime(&n);
	*last = *first + DAY;
	return 0;
    }
    return 1;
}

/* year[-month[-day]] */
static int parse_iso(const char *text, time_t *first, time_t *last, time_t after) {
    int		y = 0, m = 0, d = 0;
    int		i;
    struct tm	n;
    char	tmp[80];
    char	*t;
    char	*save;
    char	*token;

    if(strlen (text) >= sizeof (tmp))
	return 1;
    strcpy(tmp, text);

    t = tmp;
    save = NULL;
    i = 0;
    while ((token = strtok_r(t, "-", &save)) != NULL) {
	i++;
	switch(i) {
	case 1:
	    if (year(token, &y) != 0)
		return 1;
	    break;
	case 2:
	    if (month(token, &m) != 0)
		return 1;
	    break;
	case 3:
	    if (day(token, &d) != 0)
		return 1;
	    break;
	default:
	    return 1;
	}
	t = NULL;
    }
    today(&n, 0);
    switch (i) {
    case 1:
	n.tm_mday = 1;
	n.tm_mon = 0;
	n.tm_year = y;
	*first = mktime(&n);
	n.tm_year = y + 1;
	*last = mktime(&n);
	return 0;
    case 2:
	n.tm_mday = 1;
	n.tm_mon = m;
	n.tm_year = y;
	*first = mktime(&n);
	if (++n.tm_mon > 11) {
	    n.tm_mon = 0;
	    n.tm_year++;
	}
	*last = mktime(&n);
	return 0;
    case 3:
	n.tm_mday = d;
	n.tm_mon = m;
	n.tm_year = y;
	*first = mktime(&n);
	*last = *first + DAY;
	return 0;
    }
    return 1;
    (void)after; /*disable unused paramter warning*/
}

/* month[/day[/year]] */
static int parse_us(const char *text, time_t *first, time_t *last, time_t after) {
    int		y = 0, m = 0, d = 0;
    int		i;
    struct tm	n;
    char	tmp[80];
    char	*t;
    char	*save;
    char	*token;

    if(strlen (text) >= sizeof (tmp))
	return 1;
    strcpy(tmp, text);

    t = tmp;
    save = NULL;
    i = 0;
    while ((token = strtok_r(t, "/", &save)) != NULL) {
	i++;
	switch(i) {
	case 1:
	    if (month(token, &m) != 0)
		return 1;
	    break;
	case 2:
	    if (day(token, &d) != 0)
		return 1;
	    break;
	case 3:
	    if (year(token, &y) != 0)
		return 1;
	    break;
	default:
	    return 1;
	}
	t = NULL;
    }
    today(&n, after);
    if (after) {
	if (m < n.tm_mon)
	    n.tm_year++;
    } else {
	if (m > n.tm_mon)
	    n.tm_year--;
    }
    switch (i) {
    case 1:
	n.tm_mday = 1;
	n.tm_mon = m;
	*first = mktime(&n);
	if (++n.tm_mon > 11) {
	    n.tm_mon = 0;
	    n.tm_year++;
	}
	*last = mktime(&n);
	return 0;
    case 2:
	n.tm_mday = d;
	n.tm_mon = m;
	*first = mktime(&n);
	*last = *first + DAY;
	return 0;
    case 3:
	n.tm_mday = d;
	n.tm_mon = m;
	n.tm_year = y;
	*first = mktime(&n);
	*last = *first + DAY;
	return 0;
    }
    return 1;
}

static int (*parsers[])(const char *text, time_t *first, time_t *last, time_t after) = {
    parse_today,
    parse_yesterday,
    parse_thisweek,
    parse_lastweek,
    parse_thismonth,
    parse_lastmonth,
    parse_month,
    parse_iso,
    parse_us,
    0,
};

notmuch_status_t
notmuch_parse_date(const char *text, time_t *first, time_t *last, time_t after)
{
    int		i;
    for (i = 0; parsers[i]; i++)
	if (parsers[i](text, first, last, after) == 0)
	    return NOTMUCH_STATUS_SUCCESS;
    return NOTMUCH_STATUS_INVALID_DATE;
}

debug log:

solving 805a1d9 ...
found 805a1d9 in https://yhetil.org/notmuch/1264506221-9636-4-git-send-email-Sebastian@SSpaeth.de/ ||
	https://yhetil.org/notmuch/1265627652-25912-1-git-send-email-Sebastian@SSpaeth.de/
found 09c5ef9 in https://yhetil.org/notmuch/1264506221-9636-2-git-send-email-Sebastian@SSpaeth.de/

applying [1/2] https://yhetil.org/notmuch/1264506221-9636-2-git-send-email-Sebastian@SSpaeth.de/
diff --git a/lib/date.c b/lib/date.c
new file mode 100644
index 0000000..09c5ef9


applying [2/2] https://yhetil.org/notmuch/1264506221-9636-4-git-send-email-Sebastian@SSpaeth.de/
diff --git a/lib/date.c b/lib/date.c
index 09c5ef9..805a1d9 100644

Checking patch lib/date.c...
Applied patch lib/date.c cleanly.
Checking patch lib/date.c...
Applied patch lib/date.c cleanly.

skipping https://yhetil.org/notmuch/1265627652-25912-1-git-send-email-Sebastian@SSpaeth.de/ for 805a1d9
index at:
100644 805a1d92bf0eec308f1a5e7f434d508cfadd80c5	lib/date.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).