summaryrefslogtreecommitdiff
path: root/libmailutils
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2011-12-08 17:04:29 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2011-12-08 18:33:56 +0200
commitacec689ef648adaa5c420049bfb775953e184bda (patch)
treeac5a96099e5a646fc24986650b4f9073c9e83642 /libmailutils
parent5af1d3a7bd8149139ad59baa11501e3d11b138a7 (diff)
downloadmailutils-acec689ef648adaa5c420049bfb775953e184bda.tar.gz
mailutils-acec689ef648adaa5c420049bfb775953e184bda.tar.bz2
Revamp date/time calculations.
* configure.ac: Build libmailutils/datetime/Makefile.am * include/mailutils/datetime.h (mu_datetime_julianday) (mu_datetime_dayofweek,mu_datetime_dayofyear) (mu_datetime_year_days): New protos. * libmailutils/Makefile.am (SUBDIRS): Add datetime (libmailutils_la_LIBADD): Add libdatetime.la * libmailutils/base/Makefile.am (libbase_la_SOURCES): Remove date.c and strftime.c. * libmailutils/base/date.c: Remove. * libmailutils/base/strftime.c: Move to libmailutils/datetime * libmailutils/datetime/Makefile.am: New file. * libmailutils/datetime/dow.c: New file. * libmailutils/datetime/doy.c: New file. * libmailutils/datetime/jd.c: New file. * libmailutils/datetime/scantime.c: New file. * libmailutils/datetime/streamftime.c: New file. * libmailutils/datetime/tab.c: New file. * libmailutils/datetime/unixtime.c: New file. * libmailutils/datetime/utcoff.c: New file. * libmailutils/datetime/yd.c: New file. * libmailutils/tests/scantime.at: Fix yday numbers.
Diffstat (limited to 'libmailutils')
-rw-r--r--libmailutils/Makefile.am3
-rw-r--r--libmailutils/base/Makefile.am2
-rw-r--r--libmailutils/base/date.c1163
-rw-r--r--libmailutils/datetime/Makefile.am32
-rw-r--r--libmailutils/datetime/dow.c29
-rw-r--r--libmailutils/datetime/doy.c53
-rw-r--r--libmailutils/datetime/jd.c32
-rw-r--r--libmailutils/datetime/scantime.c660
-rw-r--r--libmailutils/datetime/streamftime.c411
-rw-r--r--libmailutils/datetime/strftime.c (renamed from libmailutils/base/strftime.c)0
-rw-r--r--libmailutils/datetime/tab.c45
-rw-r--r--libmailutils/datetime/unixtime.c38
-rw-r--r--libmailutils/datetime/utcoff.c32
-rw-r--r--libmailutils/datetime/yd.c29
-rw-r--r--libmailutils/tests/scantime.at58
-rw-r--r--libmailutils/tests/scantime.c2
16 files changed, 1392 insertions, 1197 deletions
diff --git a/libmailutils/Makefile.am b/libmailutils/Makefile.am
index b32138250..c01b32176 100644
--- a/libmailutils/Makefile.am
+++ b/libmailutils/Makefile.am
@@ -19,7 +19,7 @@
19SUBDIRS = \ 19SUBDIRS = \
20 auth base address list sockaddr cidr cfg diag\ 20 auth base address list sockaddr cidr cfg diag\
21 filter mailbox mailer mime server string stream stdstream\ 21 filter mailbox mailer mime server string stream stdstream\
22 property url imapio . tests 22 property url imapio datetime . tests
23 23
24lib_LTLIBRARIES = libmailutils.la 24lib_LTLIBRARIES = libmailutils.la
25 25
@@ -33,6 +33,7 @@ libmailutils_la_LIBADD = \
33 sockaddr/libsockaddr.la\ 33 sockaddr/libsockaddr.la\
34 cidr/libcidr.la\ 34 cidr/libcidr.la\
35 cfg/libcfg.la\ 35 cfg/libcfg.la\
36 datetime/libdatetime.la\
36 diag/libdiag.la\ 37 diag/libdiag.la\
37 filter/libfilter.la\ 38 filter/libfilter.la\
38 imapio/libimapio.la\ 39 imapio/libimapio.la\
diff --git a/libmailutils/base/Makefile.am b/libmailutils/base/Makefile.am
index a2e5ac5b6..4b9a3759d 100644
--- a/libmailutils/base/Makefile.am
+++ b/libmailutils/base/Makefile.am
@@ -26,7 +26,6 @@ libbase_la_SOURCES = \
26 assoc.c\ 26 assoc.c\
27 filesafety.c\ 27 filesafety.c\
28 daemon.c\ 28 daemon.c\
29 date.c\
30 fdwait.c\ 29 fdwait.c\
31 fgetpwent.c\ 30 fgetpwent.c\
32 filename.c\ 31 filename.c\
@@ -61,7 +60,6 @@ libbase_la_SOURCES = \
61 sha1.c\ 60 sha1.c\
62 secret.c\ 61 secret.c\
63 spawnvp.c\ 62 spawnvp.c\
64 strftime.c\
65 symlink.c\ 63 symlink.c\
66 tempfile.c\ 64 tempfile.c\
67 ticket.c\ 65 ticket.c\
diff --git a/libmailutils/base/date.c b/libmailutils/base/date.c
deleted file mode 100644
index b3182538d..000000000
--- a/libmailutils/base/date.c
+++ /dev/null
@@ -1,1163 +0,0 @@
1/* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001, 2002, 2007, 2009, 2010, 2011 Free
3 Software Foundation, Inc.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General
16 Public License along with this library. If not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <mailutils/diag.h>
26#include <mailutils/datetime.h>
27#include <mailutils/util.h>
28#include <mailutils/stream.h>
29#include <mailutils/errno.h>
30#include <mailutils/cstr.h>
31#include <mailutils/cctype.h>
32
33#define SECS_PER_DAY 86400
34#define ADJUSTMENT -719162L
35
36/* Julian day is the number of days since Jan 1, 4713 BC (ouch!).
37 Eg. Jan 1, 1900 is: */
38#define JULIAN_1900 1721425L
39
40/* Computes the number of days elapsed since January, 1 1900 to the
41 January, 1 of the given year */
42static unsigned
43jan1st (int year)
44{
45 year--; /* Do not consider the current year */
46 return year * 365L
47 + year/4L /* Years divisible by 4 are leap years */
48 + year/400L /* Years divisible by 400 are always leap years */
49 - year/100L; /* Years divisible by 100 but not 400 aren't */
50}
51
52static int month_start[]=
53 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
54 /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
55 31 28 31 30 31 30 31 31 30 31 30 31
56 */
57
58#define leap_year(y) ((y) % 4 == 0 && ((y) % 100 != 0 || (y) % 400 == 0))
59
60static int
61dayofyear (int year, int month, int day)
62{
63 int leap, month_days;
64
65 if (year < 0 || month < 0 || month > 11)
66 return -1;
67
68 leap = leap_year (year);
69
70 month_days = month_start[month + 1] - month_start[month]
71 + ((month == 2) ? leap : 0);
72
73 if (day < 0 || day > month_days)
74 return -1; /* Illegal Date */
75
76 if (month <= 2)
77 leap = 0;
78
79 return month_start[month] + day + leap;
80}
81
82/* Returns number of days in a given year */
83static int
84year_days (int year)
85{
86 return dayofyear (year, 11, 31);
87}
88
89static int
90julianday (unsigned *pd, int year, int month, int day)
91{
92 int total = dayofyear (year, month, day);
93 if (total == -1)
94 return -1;
95 *pd = JULIAN_1900 + total + jan1st (year);
96 return 0;
97}
98
99static int
100dayofweek (int year, int month, int day)
101{
102 unsigned jd;
103
104 if (julianday (&jd, year, month, day))
105 return -1;
106
107 /* January 1, 1900 was Monday, hence +1 */
108 return (jd + 1) % 7;
109}
110
111#define ISO_8601_START_WDAY 1 /* Monday */
112#define ISO_8601_MAX_WDAY 4 /* Thursday */
113#define MAXDAYS 366 /* Max. number of days in a year */
114#define ISO_8601_OFF ((MAXDAYS / 7 + 2) * 7)
115
116int
117ISO_8601_weekdays (int yday, int wday)
118{
119 return (yday
120 - (yday - wday + ISO_8601_MAX_WDAY + ISO_8601_OFF) % 7
121 + ISO_8601_MAX_WDAY - ISO_8601_START_WDAY);
122}
123
124/* Convert struct tm into time_t, taking into account timezone offset. */
125/* FIXME: It does not take DST into account */
126time_t
127mu_tm2time (struct tm *tm, struct mu_timezone *tz)
128{
129 time_t t;
130 int day;
131
132 day = dayofyear (tm->tm_year, tm->tm_mon, tm->tm_mday - 1);
133 if (day == -1)
134 return -1;
135 t = (day + ADJUSTMENT + jan1st (1900 + tm->tm_year)) * SECS_PER_DAY
136 + (tm->tm_hour * 60 + tm->tm_min) * 60 + tm->tm_sec
137 - (tz ? tz->utc_offset : 0);
138 return t;
139}
140
141/* Convert time 0 at UTC to our localtime, that tells us the offset
142 of our current timezone from UTC. */
143time_t
144mu_utc_offset (void)
145{
146 time_t t = 0;
147 struct tm *tm = gmtime (&t);
148
149 return - mktime (tm);
150}
151
152static const char *short_month[] =
153{
154 "Jan", "Feb", "Mar", "Apr", "May", "Jun",