From 3a63661c35d4b5f93501efab7e5478386ad02565 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Sun, 5 Nov 2006 15:47:46 +0100 Subject: [PATCH] push some date functions in the lib-lib. Signed-off-by: Pierre Habouzit --- Makefile.am | 2 +- date.c | 168 ------------------------------------ globals.h | 2 + lib-crypt/gnupgparse.c | 1 + lib-lib/Makefile.am | 8 +- lib-lib/date.c | 188 +++++++++++++++++++++++++++++++++++++++++ lib-lib/date.h | 29 +++++++ protos.h | 3 - 8 files changed, 227 insertions(+), 174 deletions(-) delete mode 100644 date.c create mode 100644 lib-lib/date.c create mode 100644 lib-lib/date.h diff --git a/Makefile.am b/Makefile.am index c3a287f..1a34a07 100644 --- a/Makefile.am +++ b/Makefile.am @@ -22,7 +22,7 @@ muttng_SOURCES = $(BUILT_SOURCES) \ alias.c attach.c base64.c browser.c buffy.c \ charset.c color.c compress.c commands.c complete.c \ compose.c copy.c curs_lib.c curs_main.c \ - date.c editmsg.c enter.c \ + editmsg.c enter.c \ flags.c filter.c from.c getdomain.c \ handler.c hcache.c hdrline.c headers.c help.c history.c hook.c \ init.c keymap.c lib.c \ diff --git a/date.c b/date.c deleted file mode 100644 index fda07a0..0000000 --- a/date.c +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright notice from original mutt: - * Copyright (C) 1996-2000 Michael R. Elkins - * - * This file is part of mutt-ng, see http://www.muttng.org/. - * It's licensed under the GNU General Public License, - * please see the file GPL in the top level source directory. - */ - -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#include "mutt.h" -#include - -/* returns the seconds east of UTC given `g' and its corresponding gmtime() - representation */ -static time_t compute_tz (time_t g, struct tm *utc) -{ - struct tm *lt = localtime (&g); - time_t t; - int yday; - - t = (((lt->tm_hour - utc->tm_hour) * 60) + (lt->tm_min - utc->tm_min)) * 60; - - 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; - else - t += 24 * 60 * 60; - } - - return t; -} - -/* Returns the local timezone in seconds east of UTC for the time t, - * or for the current time if t is zero. - */ -time_t mutt_local_tz (time_t t) -{ - struct tm *ptm; - struct tm utc; - - if (!t) - t = time (NULL); - ptm = gmtime (&t); - /* need to make a copy because gmtime/localtime return a pointer to - static memory (grr!) */ - memcpy (&utc, ptm, sizeof (utc)); - return (compute_tz (t, &utc)); -} - -/* converts struct tm to time_t, but does not take the local timezone into - account unless ``local'' is nonzero */ -time_t mutt_mktime (struct tm * t, int local) -{ - time_t g; - - static int AccumDaysPerMonth[12] = { - 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 - }; - - /* Compute the number of days since January 1 in the same year */ - g = AccumDaysPerMonth[t->tm_mon % 12]; - - /* The leap years are 1972 and every 4. year until 2096, - * but this algoritm will fail after year 2099 */ - g += t->tm_mday; - if ((t->tm_year % 4) || t->tm_mon < 2) - g--; - t->tm_yday = g; - - /* Compute the number of days since January 1, 1970 */ - g += (t->tm_year - 70) * 365; - g += (t->tm_year - 69) / 4; - - /* Compute the number of hours */ - g *= 24; - g += t->tm_hour; - - /* Compute the number of minutes */ - g *= 60; - g += t->tm_min; - - /* Compute the number of seconds */ - g *= 60; - g += t->tm_sec; - - if (local) - g -= compute_tz (g, t); - - return (g); -} - -/* Return 1 if month is February of leap year, else 0 */ -static int isLeapYearFeb (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 char DaysPerMonth[12] = { - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - }; - int nLeap; - - while (tm->tm_sec < 0) { - tm->tm_sec += 60; - tm->tm_min--; - } - while (tm->tm_sec >= 60) { - tm->tm_sec -= 60; - tm->tm_min++; - } - while (tm->tm_min < 0) { - tm->tm_min += 60; - tm->tm_hour--; - } - while (tm->tm_min >= 60) { - tm->tm_min -= 60; - tm->tm_hour++; - } - while (tm->tm_hour < 0) { - tm->tm_hour += 24; - tm->tm_mday--; - } - while (tm->tm_hour >= 24) { - tm->tm_hour -= 24; - tm->tm_mday++; - } - /* use loops on NNNdwmy user input values? */ - while (tm->tm_mon < 0) { - tm->tm_mon += 12; - tm->tm_year--; - } - while (tm->tm_mon >= 12) { - tm->tm_mon -= 12; - tm->tm_year++; - } - while (tm->tm_mday <= 0) { - if (tm->tm_mon) - tm->tm_mon--; - else { - tm->tm_mon = 11; - tm->tm_year--; - } - tm->tm_mday += DaysPerMonth[tm->tm_mon] + isLeapYearFeb (tm); - } - while (tm->tm_mday > (DaysPerMonth[tm->tm_mon] + - (nLeap = isLeapYearFeb (tm)))) { - tm->tm_mday -= DaysPerMonth[tm->tm_mon] + nLeap; - if (tm->tm_mon < 11) - tm->tm_mon++; - else { - tm->tm_mon = 0; - tm->tm_year++; - } - } -} diff --git a/globals.h b/globals.h index 5bec12e..e8d498f 100644 --- a/globals.h +++ b/globals.h @@ -12,6 +12,8 @@ #include "alias.h" #include +#include + #include "lib/list.h" #include "lib/rx.h" diff --git a/lib-crypt/gnupgparse.c b/lib-crypt/gnupgparse.c index 03bab1a..dff28ca 100644 --- a/lib-crypt/gnupgparse.c +++ b/lib-crypt/gnupgparse.c @@ -35,6 +35,7 @@ #include #include +#include #include diff --git a/lib-lib/Makefile.am b/lib-lib/Makefile.am index d7dd815..716d64b 100644 --- a/lib-lib/Makefile.am +++ b/lib-lib/Makefile.am @@ -1,8 +1,12 @@ noinst_LIBRARIES = liblib.a liblib_a_SOURCES = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h \ - str.c ascii.c buffer.c hash.c list.c file.c mapping.c + str.c ascii.c buffer.c hash.c list.c file.c mapping.c \ + \ + date.h \ + date.c -noinst_HEADERS = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h +noinst_HEADERS = mem.h str.h ascii.h buffer.h hash.h list.h file.h mapping.h \ + date.c -include ../cflags.mk diff --git a/lib-lib/date.c b/lib-lib/date.c new file mode 100644 index 0000000..4bc6873 --- /dev/null +++ b/lib-lib/date.c @@ -0,0 +1,188 @@ +/* + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2006 Pierre Habouzit + */ + +/* + * Copyright notice from original mutt: + * Copyright (C) 1996-2000 Michael R. Elkins + * + * This file is part of mutt-ng, see http://www.muttng.org/. + * It's licensed under the GNU General Public License, + * please see the file GPL in the top level source directory. + */ + +#include "date.h" + +/* returns the seconds east of UTC given `g' and its corresponding gmtime() + representation */ +static time_t compute_tz(time_t g, struct tm *utc) +{ + struct tm *lt = localtime (&g); + time_t t; + int yday; + + t = (lt->tm_hour - utc->tm_hour) * 3600 + + (lt->tm_min - utc->tm_min) * 60; + yday = (lt->tm_yday - utc->tm_yday); + + if (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; + else + t += 24 * 60 * 60; + } + + return t; +} + +/* Returns the local timezone in seconds east of UTC for the time t, + or for the current time if t is zero. */ +time_t mutt_local_tz(time_t t) +{ + struct tm *ptm; + struct tm utc; + + if (!t) + t = time(NULL); + + /* need to make a copy because gmtime/localtime return a pointer to + static memory (grr!) */ + ptm = gmtime(&t); + utc = *ptm; + return compute_tz(t, &utc); +} + +/* converts struct tm to time_t, but does not take the local timezone into + account unless ``local'' is nonzero */ +time_t mutt_mktime(struct tm *t, int local) +{ + static short const AccumDaysPerMonth[12] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 + }; + + time_t g; + + /* Compute the number of days since January 1 in the same year */ + g = AccumDaysPerMonth[t->tm_mon % 12]; + + /* The leap years are 1972 and every 4. year until 2096, + * but this algoritm will fail after year 2099 */ + g += t->tm_mday; + if ((t->tm_year % 4) || t->tm_mon < 2) + g--; + t->tm_yday = g; + + /* Compute the number of days since January 1, 1970 */ + g += (t->tm_year - 70) * 365; + g += (t->tm_year - 69) / 4; + + /* Compute the number of hours */ + g *= 24; + g += t->tm_hour; + + /* Compute the number of minutes */ + g *= 60; + g += t->tm_min; + + /* Compute the number of seconds */ + g *= 60; + g += t->tm_sec; + + if (local) + g -= compute_tz(g, t); + + return g; +} + +/* Return 1 if month is February of leap year, else 0 */ +__attribute__((const)) +static int isLeapYearFeb(int m, int y) +{ + return (m == 1) /* february ? */ + && !(y & 3) /* multiple of 4 ? */ + && ((y % 100) || !((y + 1900) % 400)); + /* !multiple of 100, or multiple of 400 */ +} + +__attribute__((const)) +static inline int nbDaysInMonth(int m, int y) +{ + static char const DaysPerMonth[12] = { + 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 + }; + + return DaysPerMonth[m] + isLeapYearFeb(m, y); +} + +void mutt_normalize_time(struct tm *tm) +{ + while (tm->tm_sec < 0) { + tm->tm_sec += 60; + tm->tm_min--; + } + while (tm->tm_sec >= 60) { + tm->tm_sec -= 60; + tm->tm_min++; + } + while (tm->tm_min < 0) { + tm->tm_min += 60; + tm->tm_hour--; + } + while (tm->tm_min >= 60) { + tm->tm_min -= 60; + tm->tm_hour++; + } + while (tm->tm_hour < 0) { + tm->tm_hour += 24; + tm->tm_mday--; + } + while (tm->tm_hour >= 24) { + tm->tm_hour -= 24; + tm->tm_mday++; + } + /* use loops on NNNdwmy user input values? */ + while (tm->tm_mon < 0) { + tm->tm_mon += 12; + tm->tm_year--; + } + while (tm->tm_mon >= 12) { + tm->tm_mon -= 12; + tm->tm_year++; + } + while (tm->tm_mday <= 0) { + if (tm->tm_mon) { + tm->tm_mon--; + } else { + tm->tm_mon = 11; + tm->tm_year--; + } + tm->tm_mday += nbDaysInMonth(tm->tm_mon, tm->tm_year); + } + + while (tm->tm_mday > nbDaysInMonth(tm->tm_mon, tm->tm_year)) { + tm->tm_mday -= nbDaysInMonth(tm->tm_mon, tm->tm_year); + if (tm->tm_mon < 11) { + tm->tm_mon++; + } else { + tm->tm_mon = 0; + tm->tm_year++; + } + } +} diff --git a/lib-lib/date.h b/lib-lib/date.h new file mode 100644 index 0000000..ffb79ae --- /dev/null +++ b/lib-lib/date.h @@ -0,0 +1,29 @@ +/* + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * Copyright © 2006 Pierre Habouzit + */ + +#ifndef MUTT_LIB_LIB_STR_H +#define MUTT_LIB_LIB_STR_H + +#include + +time_t mutt_local_tz(time_t); +time_t mutt_mktime(struct tm *, int); +void mutt_normalize_time(struct tm *); + +#endif /* MUTT_LIB_LIB_STR_H */ diff --git a/protos.h b/protos.h index 95fa295..7129041 100644 --- a/protos.h +++ b/protos.h @@ -86,8 +86,6 @@ int mutt_cmp_body (const BODY * b1, const BODY * b2); time_t mutt_decrease_mtime (const char *, struct stat *); void mutt_set_mtime (const char*, const char*); -time_t mutt_local_tz (time_t); -time_t mutt_mktime (struct tm *, int); time_t mutt_parse_date (const char *, HEADER *); int is_from (const char *, char *, size_t, time_t *); @@ -160,7 +158,6 @@ void mutt_message_to_7bit (BODY *, FILE *); #define mutt_mktemp(a) _mutt_mktemp (a, __FILE__, __LINE__) void _mutt_mktemp (char *, const char *, int); -void mutt_normalize_time (struct tm *); void mutt_paddstr (int, const char *); void mutt_parse_mime_message (CONTEXT * ctx, HEADER *); void mutt_parse_part (FILE *, BODY *); -- 2.20.1