unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob cd45818b076a8141731d8cef4eb7e1bc9b44a675 3644 bytes (raw)
name: notmuch-time.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
 
/* notmuch - Not much of an email program, (just index and search)
 *
 * Copyright © 2009 Carl Worth
 *
 * 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, see https://www.gnu.org/licenses/ .
 *
 * Author: Carl Worth <cworth@cworth.org>
 */

#include "notmuch-client.h"

/* Format a nice representation of 'time' relative to the current time.
 *
 * Examples include:
 *
 *	5 mins. ago	(For times less than 60 minutes ago)
 *	Today 12:30	(For times >60 minutes but still today)
 *	Yest. 12:30
 *	Mon.  12:30	(Before yesterday but fewer than 7 days ago)
 *	October 12	(Between 7 and 180 days ago (about 6 months))
 *	2008-06-30	(More than 180 days ago)
 *
 * The returned string is either static data (a string literal) or
 * newly talloced data belonging to 'ctx'. That is, the caller should
 * not modify nor free the returned value. But when the caller
 * arranges for 'ctx' to be talloc_freed, then memory allocated here
 * (if any) will be reclaimed.
 *
 */
#define MINUTE (60)
#define HOUR (60 * MINUTE)
#define DAY (24 * HOUR)
#define RELATIVE_DATE_MAX 20
const char *
notmuch_time_relative_date (const void *ctx, time_t then)
{
    struct tm tm_now, tm_then;
    time_t now = time (NULL);
    time_t delta;
    char *result;

    localtime_r (&now, &tm_now);
    localtime_r (&then, &tm_then);

    result = talloc_zero_size (ctx, RELATIVE_DATE_MAX);
    if (result == NULL)
	return "when?";

    if (then > now)
	return "the future";

    delta = now - then;

    if (delta > 180 * DAY) {
	strftime (result, RELATIVE_DATE_MAX,
		  "%F", &tm_then); /* 2008-06-30 */
	return result;
    }

    if (delta < 3600) {
	snprintf (result, RELATIVE_DATE_MAX,
		  "%d mins. ago", (int) (delta / 60));
	return result;
    }

    if (delta <= 7 * DAY) {
	if (tm_then.tm_wday == tm_now.tm_wday &&
	    delta < DAY) {
	    strftime (result, RELATIVE_DATE_MAX,
		      "Today %R", &tm_then);    /* Today 12:30 */
	    return result;
	} else if ((tm_now.tm_wday + 7 - tm_then.tm_wday) % 7 == 1) {
	    strftime (result, RELATIVE_DATE_MAX,
		      "Yest. %R", &tm_then);    /* Yest. 12:30 */
	    return result;
	} else {
	    if (tm_then.tm_wday != tm_now.tm_wday) {
		strftime (result, RELATIVE_DATE_MAX,
			  "%a. %R", &tm_then);  /* Mon. 12:30 */
		return result;
	    }
	}
    }

    strftime (result, RELATIVE_DATE_MAX,
	      "%B %d", &tm_then);               /* October 12 */
    return result;
}
#undef MINUTE
#undef HOUR
#undef DAY

void
notmuch_time_print_formatted_seconds (double seconds)
{
    int hours;
    int minutes;

    if (seconds < 1) {
	printf ("almost no time");
	return;
    }

    if (seconds > 3600) {
	hours = (int) seconds / 3600;
	printf ("%dh ", hours);
	seconds -= hours * 3600;
    }

    if (seconds > 60) {
	minutes = (int) seconds / 60;
	printf ("%dm ", minutes);
	seconds -= minutes * 60;
    }

    printf ("%ds", (int) seconds);
}

/* Compute the number of seconds elapsed from start to end. */
double
notmuch_time_elapsed (struct timeval start, struct timeval end)
{
    return ((end.tv_sec - start.tv_sec) +
	    (end.tv_usec - start.tv_usec) / 1e6);
}

debug log:

solving cd45818b ...
found cd45818b in https://yhetil.org/notmuch.git/

(*) 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).