From: Pietro Cerutti Date: Mon, 2 Sep 2019 09:44:21 +0000 (+0000) Subject: Implement mutt_date_epoch() X-Git-Tag: 2019-10-25~49 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=95de0e2db67427ab5774c06303d98c718b54f1a6;p=neomutt Implement mutt_date_epoch() Closes: #1824 - Use mutt_date_epoch() instead of time() - Use mutt_date_epoch_ms in progress - Undo type conversion, avoid double call to mutt_date_epoch - Give a magic number a name --- diff --git a/autocrypt/autocrypt.c b/autocrypt/autocrypt.c index cc5e1b94e..f47c86aa0 100644 --- a/autocrypt/autocrypt.c +++ b/autocrypt/autocrypt.c @@ -288,7 +288,7 @@ int mutt_autocrypt_process_autocrypt_header(struct Email *e, struct Envelope *en /* Ignore emails that appear to be more than a week in the future, * since they can block all future updates during that time. */ - if (e->date_sent > (mutt_date_epoch_ms() / 1000 + (7 * 24 * 60 * 60))) + if (e->date_sent > (mutt_date_epoch() + (7 * 24 * 60 * 60))) return 0; for (struct AutocryptHeader *ac_hdr = env->autocrypt; ac_hdr; ac_hdr = ac_hdr->next) @@ -430,7 +430,7 @@ int mutt_autocrypt_process_gossip_header(struct Email *e, struct Envelope *prot_ /* Ignore emails that appear to be more than a week in the future, * since they can block all future updates during that time. */ - if (e->date_sent > (mutt_date_epoch_ms() / 1000 + (7 * 24 * 60 * 60))) + if (e->date_sent > (mutt_date_epoch() + (7 * 24 * 60 * 60))) return 0; struct Buffer *keyid = mutt_buffer_pool_get(); diff --git a/browser.c b/browser.c index 5c809bd69..16ba67e72 100644 --- a/browser.c +++ b/browser.c @@ -39,7 +39,6 @@ #include #include #include -#include #include "mutt/mutt.h" #include "config/lib.h" #include "email/lib.h" @@ -412,8 +411,8 @@ static const char *folder_format_str(char *buf, size_t buflen, size_t col, int c } else { - time_t tnow = time(NULL); - t_fmt = ((tnow - folder->ff->mtime) < 31536000) ? "%b %d %H:%M" : "%b %d %Y"; + static const time_t one_year = 31536000; + t_fmt = ((mutt_date_epoch() - folder->ff->mtime) < one_year) ? "%b %d %H:%M" : "%b %d %Y"; } if (!do_locales) diff --git a/conn/ssl_gnutls.c b/conn/ssl_gnutls.c index fa0846b90..3fbade93c 100644 --- a/conn/ssl_gnutls.c +++ b/conn/ssl_gnutls.c @@ -35,7 +35,6 @@ #include #include #include -#include #include "mutt/mutt.h" #include "config/lib.h" #include "conn_globals.h" @@ -349,9 +348,9 @@ static int tls_check_preauth(const gnutls_datum_t *certdata, * GNUTLS_CERT_EXPIRED and GNUTLS_CERT_NOT_ACTIVATED bits set. */ if (C_SslVerifyDates != MUTT_NO) { - if (gnutls_x509_crt_get_expiration_time(cert) < time(NULL)) + if (gnutls_x509_crt_get_expiration_time(cert) < mutt_date_epoch()) *certerr |= CERTERR_EXPIRED; - if (gnutls_x509_crt_get_activation_time(cert) > time(NULL)) + if (gnutls_x509_crt_get_activation_time(cert) > mutt_date_epoch()) *certerr |= CERTERR_NOTYETVALID; } diff --git a/email/parse.c b/email/parse.c index b12d4db89..24709547c 100644 --- a/email/parse.c +++ b/email/parse.c @@ -736,7 +736,7 @@ int mutt_rfc822_parse_line(struct Envelope *env, struct Email *e, char *line, case 'e': if ((mutt_str_strcasecmp("xpires", line + 1) == 0) && e && - (mutt_date_parse_date(p, NULL) < time(NULL))) + (mutt_date_parse_date(p, NULL) < mutt_date_epoch())) { e->expired = true; } diff --git a/handler.c b/handler.c index b886ea03b..3cd571d3f 100644 --- a/handler.c +++ b/handler.c @@ -875,7 +875,7 @@ static int external_body_handler(struct Body *b, struct State *s) mutt_copy_hdr(s->fp_in, s->fp_out, ftello(s->fp_in), b->parts->offset, chflags, NULL); } } - else if (expiration && (expire < time(NULL))) + else if (expiration && (expire < mutt_date_epoch())) { if (s->flags & MUTT_DISPLAY) { diff --git a/hdrline.c b/hdrline.c index 2f3b9aef2..75baac7b6 100644 --- a/hdrline.c +++ b/hdrline.c @@ -682,7 +682,7 @@ static const char *index_format_str(char *buf, size_t buflen, size_t col, int co if (optional && ((op == '[') || (op == '('))) { - now = time(NULL); + now = mutt_date_epoch(); struct tm tm = mutt_date_localtime(now); now -= (op == '(') ? e->received : e->date_sent; diff --git a/imap/command.c b/imap/command.c index 49ceadc2c..8731fd61d 100644 --- a/imap/command.c +++ b/imap/command.c @@ -1123,7 +1123,7 @@ int imap_cmd_step(struct ImapAccountData *adata) mutt_debug(LL_DEBUG3, "shrank buffer to %lu bytes\n", adata->blen); } - adata->lastread = time(NULL); + adata->lastread = mutt_date_epoch(); /* handle untagged messages. The caller still gets its shot afterwards. */ if ((mutt_str_startswith(adata->buf, "* ", CASE_MATCH) || diff --git a/imap/imap.c b/imap/imap.c index fd94292c3..397feb47d 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -1197,7 +1197,7 @@ int imap_check_mailbox(struct Mailbox *m, bool force) /* try IDLE first, unless force is set */ if (!force && C_ImapIdle && (adata->capabilities & IMAP_CAP_IDLE) && - ((adata->state != IMAP_IDLE) || (time(NULL) >= adata->lastread + C_ImapKeepalive))) + ((adata->state != IMAP_IDLE) || (mutt_date_epoch() >= adata->lastread + C_ImapKeepalive))) { if (imap_cmd_idle(adata) < 0) return -1; @@ -1219,7 +1219,7 @@ int imap_check_mailbox(struct Mailbox *m, bool force) } } - if ((force || ((adata->state != IMAP_IDLE) && (time(NULL) >= adata->lastread + C_Timeout))) && + if ((force || ((adata->state != IMAP_IDLE) && (mutt_date_epoch() >= adata->lastread + C_Timeout))) && (imap_exec(adata, "NOOP", IMAP_CMD_POLL) != IMAP_EXEC_SUCCESS)) { return -1; diff --git a/imap/util.c b/imap/util.c index bc684bfd3..69faacfc1 100644 --- a/imap/util.c +++ b/imap/util.c @@ -1052,7 +1052,7 @@ void imap_unmunge_mbox_name(bool unicode, char *s) */ void imap_keepalive(void) { - time_t now = time(NULL); + time_t now = mutt_date_epoch(); struct Account *np = NULL; TAILQ_FOREACH(np, &NeoMutt->accounts, entries) { diff --git a/init.c b/init.c index b6f70ee79..7ab7da38c 100644 --- a/init.c +++ b/init.c @@ -2988,8 +2988,8 @@ int mutt_init(bool skip_sys_rc, struct ListHead *commands) snprintf(AttachmentMarker, sizeof(AttachmentMarker), "\033]9;%" PRIu64 "\a", // Escape mutt_rand64()); - snprintf(ProtectedHeaderMarker, sizeof(ProtectedHeaderMarker), "\033]8;%ld\a", // Escape - (long) time(NULL)); + snprintf(ProtectedHeaderMarker, sizeof(ProtectedHeaderMarker), "\033]8;%lld\a", // Escape + (long long) mutt_date_epoch()); /* "$spoolfile" precedence: config file, environment */ const char *p = mutt_str_getenv("MAIL"); diff --git a/maildir/maildir.c b/maildir/maildir.c index ff1e87a72..de47cd18d 100644 --- a/maildir/maildir.c +++ b/maildir/maildir.c @@ -603,8 +603,8 @@ int maildir_msg_open_new(struct Mailbox *m, struct Message *msg, struct Email *e while (true) { snprintf(path, sizeof(path), "%s/tmp/%s.%lld.R%" PRIu64 ".%s%s", - mailbox_path(m), subdir, (long long) time(NULL), mutt_rand64(), - NONULL(ShortHostname), suffix); + mailbox_path(m), subdir, (long long) mutt_date_epoch(), + mutt_rand64(), NONULL(ShortHostname), suffix); mutt_debug(LL_DEBUG2, "Trying %s\n", path); diff --git a/maildir/shared.c b/maildir/shared.c index a826af7dd..c1f0667ec 100644 --- a/maildir/shared.c +++ b/maildir/shared.c @@ -997,8 +997,9 @@ int md_commit_message(struct Mailbox *m, struct Message *msg, struct Email *e) struct Buffer *full = mutt_buffer_pool_get(); while (true) { - mutt_buffer_printf(path, "%s/%lld.R%" PRIu64 ".%s%s", subdir, (long long) time(NULL), - mutt_rand64(), NONULL(ShortHostname), suffix); + mutt_buffer_printf(path, "%s/%lld.R%" PRIu64 ".%s%s", subdir, + (long long) mutt_date_epoch(), mutt_rand64(), + NONULL(ShortHostname), suffix); mutt_buffer_printf(full, "%s/%s", mailbox_path(m), mutt_b2s(path)); mutt_debug(LL_DEBUG2, "renaming %s to %s\n", msg->path, mutt_b2s(full)); diff --git a/mutt/date.c b/mutt/date.c index ce50c873f..bf53aeabd 100644 --- a/mutt/date.c +++ b/mutt/date.c @@ -205,7 +205,7 @@ time_t mutt_date_local_tz(time_t t) return 0; if (t == 0) - t = time(NULL); + t = mutt_date_epoch(); struct tm tm = mutt_date_gmtime(t); return compute_tz(t, &tm); @@ -373,7 +373,7 @@ char *mutt_date_make_date(char *buf, size_t buflen) if (!buf) return NULL; - time_t t = time(NULL); + time_t t = mutt_date_epoch(); struct tm tm = mutt_date_localtime(t); time_t tz = mutt_date_local_tz(t); @@ -403,6 +403,15 @@ int mutt_date_check_month(const char *s) return -1; /* error */ } +/** + * mutt_date_epoch - Return the number of seconds since the Unix epoch + * @retval s The number of s since the Unix epoch, or 0 on failure + */ +time_t mutt_date_epoch(void) +{ + return mutt_date_epoch_ms() / 1000; +} + /** * mutt_date_epoch_ms - Return the number of milliseconds since the Unix epoch * @retval ms The number of ms since the Unix epoch, or 0 on failure @@ -729,7 +738,7 @@ struct tm mutt_date_localtime(time_t t) struct tm tm = { 0 }; if (t == MUTT_DATE_NOW) - t = time(NULL); + t = mutt_date_epoch(); localtime_r(&t, &tm); return tm; @@ -747,7 +756,7 @@ struct tm mutt_date_gmtime(time_t t) struct tm tm = { 0 }; if (t == MUTT_DATE_NOW) - t = time(NULL); + t = mutt_date_epoch(); gmtime_r(&t, &tm); return tm; diff --git a/mutt/date.h b/mutt/date.h index 81e4755f6..eb19adf3c 100644 --- a/mutt/date.h +++ b/mutt/date.h @@ -48,6 +48,7 @@ struct Tz time_t mutt_date_add_timeout(time_t now, long timeout); int mutt_date_check_month(const char *s); +time_t mutt_date_epoch(void); size_t mutt_date_epoch_ms(void); struct tm mutt_date_gmtime(time_t t); bool mutt_date_is_day_name(const char *s); diff --git a/mutt/file.c b/mutt/file.c index 080e7a212..9cd394af3 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -42,6 +42,7 @@ #include #include #include "file.h" +#include "date.h" #include "buffer.h" #include "logging.h" #include "memory.h" @@ -972,7 +973,7 @@ time_t mutt_file_decrease_mtime(const char *fp, struct stat *st) } mtime = st->st_mtime; - if (mtime == time(NULL)) + if (mtime == mutt_date_epoch()) { mtime -= 1; utim.actime = mtime; diff --git a/mutt/logging.c b/mutt/logging.c index a885a8727..62370cafa 100644 --- a/mutt/logging.c +++ b/mutt/logging.c @@ -80,7 +80,7 @@ static const char *timestamp(time_t stamp) static time_t last = 0; if (stamp == 0) - stamp = time(NULL); + stamp = mutt_date_epoch(); if (stamp != last) { @@ -424,7 +424,7 @@ int log_disp_queue(time_t stamp, const char *file, int line, } struct LogLine *ll = mutt_mem_calloc(1, sizeof(*ll)); - ll->time = (stamp != 0) ? stamp : time(NULL); + ll->time = (stamp != 0) ? stamp : mutt_date_epoch(); ll->file = file; ll->line = line; ll->function = function; diff --git a/mutt_mailbox.c b/mutt_mailbox.c index 987909ab2..3925d6ba6 100644 --- a/mutt_mailbox.c +++ b/mutt_mailbox.c @@ -141,7 +141,7 @@ int mutt_mailbox_check(struct Mailbox *m_cur, int force) if (TAILQ_EMPTY(&NeoMutt->accounts)) return 0; - t = time(NULL); + t = mutt_date_epoch(); if (!force && (t - MailboxTime < C_MailCheck)) return MailboxCount; @@ -276,8 +276,8 @@ void mutt_mailbox_set_notified(struct Mailbox *m) #if HAVE_CLOCK_GETTIME clock_gettime(CLOCK_REALTIME, &m->last_visited); #else + m->last_visited.tv_sec = mutt_date_epoch(); m->last_visited.tv_nsec = 0; - time(&m->last_visited.tv_sec); #endif } @@ -377,7 +377,7 @@ void mutt_mailbox_cleanup(const char *path, struct stat *st) utimensat(0, buf, ts, 0); #else ut.actime = st->st_atime; - ut.modtime = time(NULL); + ut.modtime = mutt_date_epoch(); utime(path, &ut); #endif } diff --git a/mx.c b/mx.c index b4a4c3779..5ef920822 100644 --- a/mx.c +++ b/mx.c @@ -980,7 +980,7 @@ struct Message *mx_msg_open_new(struct Mailbox *m, struct Email *e, MsgOpenFlags } if (msg->received == 0) - time(&msg->received); + msg->received = mutt_date_epoch(); if (m->mx_ops->msg_open_new(m, msg, e) == 0) { diff --git a/ncrypt/pgp.c b/ncrypt/pgp.c index 265cb2c83..9218c36fc 100644 --- a/ncrypt/pgp.c +++ b/ncrypt/pgp.c @@ -93,15 +93,13 @@ void pgp_class_void_passphrase(void) */ bool pgp_class_valid_passphrase(void) { - time_t now = time(NULL); - if (pgp_use_gpg_agent()) { *PgpPass = '\0'; return true; /* handled by gpg-agent */ } - if (now < PgpExptime) + if (mutt_date_epoch() < PgpExptime) { /* Use cached copy. */ return true; @@ -111,7 +109,7 @@ bool pgp_class_valid_passphrase(void) if (mutt_get_password(_("Enter PGP passphrase:"), PgpPass, sizeof(PgpPass)) == 0) { - PgpExptime = mutt_date_add_timeout(time(NULL), C_PgpTimeout); + PgpExptime = mutt_date_add_timeout(mutt_date_epoch(), C_PgpTimeout); return true; } else diff --git a/ncrypt/smime.c b/ncrypt/smime.c index 882c19bd7..0f55e5447 100644 --- a/ncrypt/smime.c +++ b/ncrypt/smime.c @@ -171,8 +171,7 @@ void smime_class_void_passphrase(void) */ bool smime_class_valid_passphrase(void) { - time_t now = time(NULL); - + const time_t now = mutt_date_epoch(); if (now < SmimeExptime) { /* Use cached copy. */ @@ -183,7 +182,7 @@ bool smime_class_valid_passphrase(void) if (mutt_get_password(_("Enter S/MIME passphrase:"), SmimePass, sizeof(SmimePass)) == 0) { - SmimeExptime = mutt_date_add_timeout(time(NULL), C_SmimeTimeout); + SmimeExptime = mutt_date_add_timeout(now, C_SmimeTimeout); return true; } else diff --git a/nntp/nntp.c b/nntp/nntp.c index 7410b2c17..0ced10413 100644 --- a/nntp/nntp.c +++ b/nntp/nntp.c @@ -1543,7 +1543,7 @@ static int check_mailbox(struct Mailbox *m) struct NntpMboxData *mdata = m->mdata; struct NntpAccountData *adata = mdata->adata; - time_t now = time(NULL); + time_t now = mutt_date_epoch(); int rc = 0; void *hc = NULL; @@ -1772,7 +1772,7 @@ static int nntp_date(struct NntpAccountData *adata, time_t *now) } } } - time(now); + *now = mutt_date_epoch(); return 0; } @@ -2533,7 +2533,7 @@ static int nntp_mbox_open(struct Mailbox *m) } } - time(&adata->check_time); + adata->check_time = mutt_date_epoch(); m->mdata = mdata; // Every known newsgroup has an mdata which is stored in adata->groups_list. // Currently we don't let the Mailbox free the mdata. diff --git a/notmuch/mutt_notmuch.c b/notmuch/mutt_notmuch.c index 090eee148..31e8e01d9 100644 --- a/notmuch/mutt_notmuch.c +++ b/notmuch/mutt_notmuch.c @@ -49,7 +49,6 @@ #include #include #include -#include #include #include "notmuch_private.h" #include "mutt/mutt.h" @@ -1691,7 +1690,7 @@ int nm_read_entire_thread(struct Mailbox *m, struct Email *e) notmuch_query_set_sort(q, NOTMUCH_SORT_NEWEST_FIRST); read_threads_query(m, q, true, 0); - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; rc = 0; @@ -1917,7 +1916,7 @@ int nm_update_filename(struct Mailbox *m, const char *old_file, int rc = rename_filename(m, old_file, new_file, e); nm_db_release(m); - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; return rc; } @@ -2200,7 +2199,7 @@ static int nm_mbox_open(struct Mailbox *m) nm_db_release(m); - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; mdata->oldmsgcount = 0; @@ -2327,7 +2326,7 @@ done: nm_db_release(m); - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; mutt_debug(LL_DEBUG1, "nm: ... check done [count=%d, new_flags=%d, occult=%d]\n", @@ -2418,7 +2417,7 @@ static int nm_mbox_sync(struct Mailbox *m, int *index_hint) if (changed) { - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; } @@ -2530,7 +2529,7 @@ done: nm_db_release(m); if (e->changed) { - m->mtime.tv_sec = time(NULL); + m->mtime.tv_sec = mutt_date_epoch(); m->mtime.tv_nsec = 0; } mutt_debug(LL_DEBUG1, "nm: tags modify done [rc=%d]\n", rc); diff --git a/pop/pop.c b/pop/pop.c index 3f015e316..e5ceefab1 100644 --- a/pop/pop.c +++ b/pop/pop.c @@ -383,7 +383,7 @@ static int pop_fetch_headers(struct Mailbox *m) header_cache_t *hc = pop_hcache_open(adata, mailbox_path(m)); #endif - time(&adata->check_time); + adata->check_time = mutt_date_epoch(); adata->clear_cache = false; if (!m->emails) @@ -896,7 +896,7 @@ static int pop_mbox_check(struct Mailbox *m, int *index_hint) struct PopAccountData *adata = pop_adata_get(m); - if ((adata->check_time + C_PopCheckinterval) > time(NULL)) + if ((adata->check_time + C_PopCheckinterval) > mutt_date_epoch()) return 0; pop_logout(m); diff --git a/progress.c b/progress.c index c42a4aff4..993a73435 100644 --- a/progress.c +++ b/progress.c @@ -143,17 +143,6 @@ static bool progress_time_needs_update(const struct Progress *progress, size_t n return (C_TimeInc == 0) || (now < progress->timestamp) || (C_TimeInc < elapsed); } -/** - * progress_timestamp - Return the number of milliseconds since the Unix epoch - * @retval ms Milliseconds since the Unix epoch - */ -static size_t progress_timestamp(void) -{ - struct timeval tv = { 0, 0 }; - gettimeofday(&tv, NULL); - return tv.tv_sec * 1000 + tv.tv_usec / 1000; -} - /** * mutt_progress_init - Set up a progress bar * @param progress Progress bar @@ -225,7 +214,7 @@ void mutt_progress_update(struct Progress *progress, size_t pos, int percent) if (OptNoCurses) return; - const size_t now = progress_timestamp(); + const size_t now = mutt_date_epoch_ms(); const bool update = (pos == 0) /* always show the first update */ || (progress_pos_needs_update(progress, pos) && diff --git a/send.c b/send.c index 799b55703..59292c1a5 100644 --- a/send.c +++ b/send.c @@ -1660,7 +1660,7 @@ full_fcc: /* update received time so that when storing to a mbox-style folder * the From_ line contains the current time instead of when the * message was first postponed. */ - e->received = time(NULL); + e->received = mutt_date_epoch(); rc = mutt_write_multiple_fcc(fcc, e, NULL, false, NULL, finalpath); while (rc && !(flags & SEND_BATCH)) { diff --git a/sendlib.c b/sendlib.c index d5e6c9985..b32709f4c 100644 --- a/sendlib.c +++ b/sendlib.c @@ -1417,7 +1417,7 @@ static void set_encoding(struct Body *b, struct Content *info) */ void mutt_stamp_attachment(struct Body *a) { - a->stamp = time(NULL); + a->stamp = mutt_date_epoch(); } /** diff --git a/test/date/mutt_date_add_timeout.c b/test/date/mutt_date_add_timeout.c index 0c90beffe..811856b82 100644 --- a/test/date/mutt_date_add_timeout.c +++ b/test/date/mutt_date_add_timeout.c @@ -29,7 +29,7 @@ void test_mutt_date_add_timeout(void) { // time_t mutt_date_add_timeout(time_t now, long timeout); - time_t now = time(NULL); + time_t now = mutt_date_epoch(); TEST_CHECK(mutt_date_add_timeout(now, -1000) == now); TEST_CHECK(mutt_date_add_timeout(now, -1) == now);