1 /* notmuch - Not much of an email program, (just index and search)
3 * Copyright © 2009 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "notmuch-client.h"
23 /* Format a nice representation of 'time' relative to the current time.
27 * 5 mins. ago (For times less than 60 minutes ago)
28 * Today 12:30 (For times >60 minutes but still today)
30 * Mon. 12:30 (Before yesterday but fewer than 7 days ago)
31 * October 12 (Between 7 and 180 days ago (about 6 months))
32 * 2008-06-30 (More than 180 days ago)
34 * The returned string is either static data (a string literal) or
35 * newly talloced data belonging to 'ctx'. That is, the caller should
36 * not modify nor free the returned value. But when the caller
37 * arranges for 'ctx' to be talloc_freed, then memory allocated here
38 * (if any) will be reclaimed.
42 #define HOUR (60 * MINUTE)
43 #define DAY (24 * HOUR)
44 #define RELATIVE_DATE_MAX 20
46 notmuch_time_relative_date (const void *ctx, time_t then)
48 struct tm tm_now, tm_then;
49 time_t now = time (NULL);
53 localtime_r (&now, &tm_now);
54 localtime_r (&then, &tm_then);
56 result = talloc_zero_size (ctx, RELATIVE_DATE_MAX);
65 if (delta > 180 * DAY) {
66 strftime (result, RELATIVE_DATE_MAX,
67 "%F", &tm_then); /* 2008-06-30 */
72 snprintf (result, RELATIVE_DATE_MAX,
73 "%d mins. ago", (int) (delta / 60));
77 if (delta <= 7 * DAY) {
78 if (tm_then.tm_wday == tm_now.tm_wday &&
80 strftime (result, RELATIVE_DATE_MAX,
81 "Today %R", &tm_then); /* Today 12:30 */
83 } else if ((tm_now.tm_wday + 7 - tm_then.tm_wday) % 7 == 1) {
84 strftime (result, RELATIVE_DATE_MAX,
85 "Yest. %R", &tm_then); /* Yest. 12:30 */
88 if (tm_then.tm_wday != tm_now.tm_wday) {
89 strftime (result, RELATIVE_DATE_MAX,
90 "%a. %R", &tm_then); /* Mon. 12:30 */
96 strftime (result, RELATIVE_DATE_MAX,
97 "%B %d", &tm_then); /* October 12 */
105 notmuch_time_print_formatted_seconds (double seconds)
111 printf ("almost no time");
115 if (seconds > 3600) {
116 hours = (int) seconds / 3600;
117 printf ("%dh ", hours);
118 seconds -= hours * 3600;
122 minutes = (int) seconds / 60;
123 printf ("%dm ", minutes);
124 seconds -= minutes * 60;
127 printf ("%ds", (int) seconds);
130 /* Compute the number of seconds elapsed from start to end. */
132 notmuch_time_elapsed (struct timeval start, struct timeval end)
134 return ((end.tv_sec - start.tv_sec) +
135 (end.tv_usec - start.tv_usec) / 1e6);