From: Richard Russon Date: Wed, 2 Aug 2017 14:01:19 +0000 (+0100) Subject: move the date functions to the library X-Git-Tag: neomutt-20170907~56^2~13 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c3973b5f47ae97e52e3c6f6eb2048a728fdc54f1;p=neomutt move the date functions to the library --- diff --git a/Makefile.am b/Makefile.am index d9d1e9f67..20f2c7381 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,7 +47,7 @@ bin_PROGRAMS = mutt $(PGPAUX_TARGET) mutt_SOURCES = account.c addrbook.c address.h alias.c alias.h attach.c \ bcache.c body.h browser.c buffer.c buffy.c charset.c color.c \ commands.c complete.c compose.c compress.c content.h context.h copy.c \ - curs_lib.c curs_main.c date.c edit.c editmsg.c enter.c enter_state.h \ + curs_lib.c curs_main.c edit.c editmsg.c enter.c enter_state.h \ envelope.h filter.c flags.c format_flags.h from.c getdomain.c group.c \ handler.c hash.c hdrline.c header.h headers.c help.c history.c hook.c \ init.c keymap.c lib.c list.h main.c mbox.c mbyte.c mbyte_table.h md5.c \ diff --git a/lib/Makefile.am b/lib/Makefile.am index 890a63c98..30c751cfe 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -3,12 +3,12 @@ include $(top_srcdir)/flymake.am AUTOMAKE_OPTIONS = 1.6 foreign -EXTRA_DIST = lib.h lib_ascii.h lib_base64.h +EXTRA_DIST = lib.h lib_ascii.h lib_base64.h lib_date.h AM_CPPFLAGS = -I$(top_srcdir) noinst_LIBRARIES = liblib.a noinst_HEADERS = -liblib_a_SOURCES = lib_ascii.c lib_base64.c +liblib_a_SOURCES = lib_ascii.c lib_base64.c lib_date.c diff --git a/lib/lib.h b/lib/lib.h index bfb98ff86..81ffc698e 100644 --- a/lib/lib.h +++ b/lib/lib.h @@ -30,6 +30,7 @@ * * -# @subpage ascii * -# @subpage base64 + * -# @subpage date */ #ifndef _LIB_LIB_H @@ -37,5 +38,6 @@ #include "lib_ascii.h" #include "lib_base64.h" +#include "lib_date.h" #endif /* _LIB_LIB_H */ diff --git a/date.c b/lib/lib_date.c similarity index 77% rename from date.c rename to lib/lib_date.c index c87022cce..5079281bc 100644 --- a/date.c +++ b/lib/lib_date.c @@ -20,14 +20,34 @@ * this program. If not, see . */ +/** + * @page date Time and date handling routines + * + * Some commonly used time and date functions. + * + * | Function | Description + * | :-------------------- | :-------------------------------------------------- + * | mutt_local_tz() | Calculate the local timezone in seconds east of UTC + * | mutt_mktime() | Convert `struct tm` to `time_t` + * | mutt_normalize_time() | Fix the contents of a struct tm + */ + #include "config.h" #include #include +/* theoretically time_t can be float but it is integer on most (if not all) systems */ +#define TIME_T_MAX ((((time_t) 1 << (sizeof(time_t) * 8 - 2)) - 1) * 2 + 1) +#define TM_YEAR_MAX \ + (1970 + (((((TIME_T_MAX - 59) / 60) - 59) / 60) - 23) / 24 / 366) + /** * compute_tz - Calculate the number of seconds east of UTC + * @param g Local time + * @param utc UTC time + * @retval number Seconds east of UTC * - * returns the seconds east of UTC given `g' and its corresponding gmtime() + * returns the seconds east of UTC given 'g' and its corresponding gmtime() * representation */ static time_t compute_tz(time_t g, struct tm *utc) @@ -41,18 +61,35 @@ static time_t compute_tz(time_t g, struct tm *utc) if ((yday = (lt->tm_yday - utc->tm_yday))) { /* This code is optimized to negative timezones (West of Greenwich) */ - if (yday == -1 || /* UTC passed midnight before localtime */ - yday > 1) /* UTC passed new year before localtime */ - t -= 24 * 60 * 60; + if ((yday == -1) || /* UTC passed midnight before localtime */ + (yday > 1)) /* UTC passed new year before localtime */ + t -= (24 * 60 * 60); else - t += 24 * 60 * 60; + t += (24 * 60 * 60); } return t; } +/** + * is_leap_year_feb - Is a given February in a leap year + * @param tm Date to be tested + * @retval true if it's a leap year + */ +static int is_leap_year_feb(struct tm *tm) +{ + if (tm->tm_mon == 1) + { + int y = tm->tm_year + 1900; + return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0))); + } + return 0; +} + /** * mutt_local_tz - Calculate the local timezone in seconds east of UTC + * @param t Time to examine + * @retval num Seconds east of UTC * * Returns the local timezone in seconds east of UTC for the time t, * or for the current time if t is zero. @@ -71,15 +108,13 @@ time_t mutt_local_tz(time_t t) return (compute_tz(t, &utc)); } -/* theoretically time_t can be float but it is integer on most (if not all) systems */ -#define TIME_T_MAX ((((time_t) 1 << (sizeof(time_t) * 8 - 2)) - 1) * 2 + 1) -#define TM_YEAR_MAX \ - (1970 + (((((TIME_T_MAX - 59) / 60) - 59) / 60) - 23) / 24 / 366) - /** * mutt_mktime - Convert `struct tm` to `time_t` + * @param t Time to convert + * @param local Should the local timezone be considered + * @retval num Time in Unix format * - * converts struct tm to time_t, but does not take the local timezone into + * Convert a struct tm to time_t, but don't take the local timezone into * account unless ``local'' is nonzero */ time_t mutt_mktime(struct tm *t, int local) @@ -128,20 +163,14 @@ time_t mutt_mktime(struct tm *t, int local) } /** - * is_leap_year_feb - Is it a leap year - * @param tm Date to be tested - * @retval true if it's a leap year + * mutt_normalize_time - Fix the contents of a struct tm + * @param tm Time to correct + * + * If values have been added/subtracted from a struct tm, it can lead to + * invalid dates, e.g. Adding 10 days to the 25th of a month. + * + * This function will correct any over/under-flow. */ -static int is_leap_year_feb(struct tm *tm) -{ - if (tm->tm_mon == 1) - { - int y = tm->tm_year + 1900; - return (((y & 3) == 0) && (((y % 100) != 0) || ((y % 400) == 0))); - } - return 0; -} - void mutt_normalize_time(struct tm *tm) { static const char DaysPerMonth[12] = { diff --git a/lib/lib_date.h b/lib/lib_date.h new file mode 100644 index 000000000..f20c5eb9e --- /dev/null +++ b/lib/lib_date.h @@ -0,0 +1,32 @@ +/** + * @file + * Time and date handling routines + * + * @authors + * Copyright (C) 1996-2000 Michael R. Elkins + * + * @copyright + * 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 2 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 . + */ + +#ifndef _LIB_DATE_H +#define _LIB_DATE_H + +#include + +time_t mutt_local_tz(time_t t); +time_t mutt_mktime(struct tm *t, int local); +void mutt_normalize_time(struct tm *tm); + +#endif /* _LIB_DATE_H */ diff --git a/po/POTFILES.in b/po/POTFILES.in index 928936557..dfeaae236 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -18,7 +18,6 @@ ncrypt/cryptglue.c ncrypt/crypt_mod.c curs_lib.c curs_main.c -date.c doc/makedoc.c edit.c editmsg.c @@ -62,6 +61,7 @@ keymap_alldefs.h lib.c lib/lib_ascii.c lib/lib_base64.c +lib/lib_date.c main.c mbox.c mbyte.c diff --git a/protos.h b/protos.h index a1056ce7c..c446f0d01 100644 --- a/protos.h +++ b/protos.h @@ -122,8 +122,6 @@ struct Envelope *mutt_read_rfc822_header(FILE *f, struct Header *hdr, short user void mutt_set_mtime(const char *from, const char *to); time_t mutt_decrease_mtime(const char *f, struct stat *st); -time_t mutt_local_tz(time_t t); -time_t mutt_mktime(struct tm *t, int local); time_t mutt_parse_date(const char *s, struct Header *h); int is_from(const char *s, char *path, size_t pathlen, time_t *tp); void mutt_touch_atime(int f); @@ -222,7 +220,6 @@ void mutt_message_to_7bit(struct Body *a, FILE *fp); #define mutt_mktemp_pfx_sfx(a, b, c, d) _mutt_mktemp(a, b, c, d, __FILE__, __LINE__) void _mutt_mktemp(char *s, size_t slen, const char *prefix, const char *suffix, const char *src, int line); -void mutt_normalize_time(struct tm *tm); void mutt_paddstr(int n, const char *s); void mutt_parse_mime_message(struct Context *ctx, struct Header *cur); void mutt_parse_part(FILE *fp, struct Body *b);