[dovecot-cvs] dovecot/src/lib utc-mktime.c,NONE,1.1 utc-mktime.h,NONE,1.1 utc-offset.c,NONE,1.1 utc-offset.h,NONE,1.1 Makefile.am,1.8,1.9 ioloop.c,1.5,1.6 ioloop.h,1.3,1.4 gmtoff.c,1.1.1.1,NONE gmtoff.h,1.1.1.1,NONE

cras at procontrol.fi cras at procontrol.fi
Thu Oct 24 04:15:41 EEST 2002


Update of /home/cvs/dovecot/src/lib
In directory danu:/tmp/cvs-serv28234/lib

Modified Files:
	Makefile.am ioloop.c ioloop.h 
Added Files:
	utc-mktime.c utc-mktime.h utc-offset.c utc-offset.h 
Removed Files:
	gmtoff.c gmtoff.h 
Log Message:
Fixes to timezone handling which were handling quite badly. added
ioloop_timezone which gets updated with ioloop_time. Changed some GMT
references to UTC. Timezone offsets are in minutes now everywhere instead of
seconds. Fixes for unsigned time_t.



--- NEW FILE: utc-mktime.c ---
/* mkgmtime.c - make time corresponding to a GMT timeval struct
 $Id: utc-mktime.c,v 1.1 2002/10/24 00:15:38 cras Exp $
 
 * Copyright (c) 1998-2000 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any other legal
 *    details, please contact  
 *      Office of Technology Transfer
 *      Carnegie Mellon University
 *      5000 Forbes Avenue
 *      Pittsburgh, PA  15213-3890
 *      (412) 268-4387, fax: (412) 268-7395
 *      tech-transfer at andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *
 */
/*
 * Copyright (c) 1987, 1989, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Arthur David Olson of the National Cancer Institute.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
** Adapted from code provided by Robert Elz, who writes:
**	The "best" way to do mktime I think is based on an idea of Bob
**	Kridle's (so its said...) from a long time ago. (mtxinu!kridle now).
**	It does a binary search of the time_t space.  Since time_t's are
**	just 32 bits, its a max of 32 iterations (even at 64 bits it
**	would still be very reasonable).
*/

#include "utc-mktime.h"

static int tmcomp(register const struct tm * const atmp,
		  register const struct tm * const btmp)
{
	register int	result;

	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
		(result = (atmp->tm_min - btmp->tm_min)) == 0)
			result = atmp->tm_sec - btmp->tm_sec;
	return result;
}

time_t utc_mktime(struct tm *tm)
{
	register int			dir;
	register int			bits;
	register int			saved_seconds;
	time_t				t;
	struct tm			yourtm, *mytm;

	yourtm = *tm;
	saved_seconds = yourtm.tm_sec;
	yourtm.tm_sec = 0;
	/*
	** Calculate the number of magnitude bits in a time_t
	** (this works regardless of whether time_t is
	** signed or unsigned, though lint complains if unsigned).
	*/
	for (bits = 0, t = 1; t > 0; ++bits, t <<= 1)
		;
	/*
	** If time_t is signed, then 0 is the median value,
	** if time_t is unsigned, then 1 << bits is median.
	*/
	t = (t < 0) ? 0 : ((time_t) 1 << bits);
	for ( ; ; ) {
		mytm = gmtime(&t);
		dir = tmcomp(mytm, &yourtm);
		if (dir != 0) {
			if (bits-- < 0)
				return (time_t)-1;
			if (bits < 0)
				--t;
			else if (dir > 0)
				t -= (time_t) 1 << bits;
			else	t += (time_t) 1 << bits;
			continue;
		}
		break;
	}
	t += saved_seconds;
	return t;
}

--- NEW FILE: utc-mktime.h ---
#ifndef __UTC_MKTIME_H
#define __UTC_MKTIME_H

#include <time.h>

/* Like mktime(), but assume that tm is in UTC. Unlike mktime(), values in
   tm fields must be in valid range. */
time_t utc_mktime(struct tm *tm);

#endif

--- NEW FILE: utc-offset.c ---
/*
 compat.c : Compatibility functions for OSes not having them

    Copyright (c) 2002 Timo Sirainen

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "lib.h"
#include "utc-offset.h"

#include <sys/time.h>

int utc_offset(struct tm *tm, time_t t __attr_unused__)
{
#ifdef HAVE_TM_GMTOFF
	return (int) (tm->tm_gmtoff/60);
#else
	struct tm ltm, gtm;
	int offset;

	/* gmtime() overwrites tm, so we need to copy it elsewhere */
	ltm = *tm;
	tm = gmtime(&t);
	gtm = *tm;

	/* max offset of 24 hours */
	if (ltm.tm_yday < gtm.tm_yday)
		offset = -24 * 60;
	else if (ltm.tm_yday > gtm.tm_yday)
		offset = 24 * 60;
	else
		offset = 0;

	offset += (ltm.tm_hour - gtm.tm_hour) * 60;
	offset += (ltm.tm_min - gtm.tm_min);

	/* restore overwritten tm */
	*tm = ltm;
	return offset;
#endif
}

--- NEW FILE: utc-offset.h ---
#ifndef __UTC_OFFSET_H
#define __UTC_OFFSET_H

#include <time.h>

/* Returns given time's offset to UTC in minutes. */
int utc_offset(struct tm *tm, time_t t);

#endif

Index: Makefile.am
===================================================================
RCS file: /home/cvs/dovecot/src/lib/Makefile.am,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- Makefile.am	13 Oct 2002 23:49:11 -0000	1.8
+++ Makefile.am	24 Oct 2002 00:15:38 -0000	1.9
@@ -18,7 +18,6 @@
 	fdpass.c \
 	file-lock.c \
 	file-set-size.c \
-	gmtoff.c \
 	hash.c \
 	hex-binary.c \
 	hostpid.c \
@@ -50,6 +49,8 @@
 	temp-string.c \
 	unlink-directory.c \
 	unlink-lockfiles.c \
+	utc-offset.c \
+	utc-mktime.c \
 	write-full.c
 
 noinst_HEADERS = \
@@ -60,7 +61,6 @@
 	fdpass.h \
 	file-lock.h \
 	file-set-size.h \
-	gmtoff.h \
 	hash.h \
 	hex-binary.h \
 	hostpid.h \
@@ -87,6 +87,8 @@
 	temp-string.h \
 	unlink-directory.h \
 	unlink-lockfiles.h \
+	utc-offset.h \
+	utc-mktime.h \
 	write-full.h
 
 EXTRA_DIST = \

Index: ioloop.c
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ioloop.c,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -d -r1.5 -r1.6
--- ioloop.c	10 Oct 2002 02:01:34 -0000	1.5
+++ ioloop.c	24 Oct 2002 00:15:38 -0000	1.6
@@ -37,6 +37,7 @@
 
 time_t ioloop_time = 0;
 struct timeval ioloop_timeval;
+struct timezone ioloop_timezone;
 
 static IOLoop current_ioloop = NULL;
 
@@ -253,7 +254,7 @@
 	struct timeval tv;
         int t_id;
 
-	gettimeofday(&ioloop_timeval, NULL);
+	gettimeofday(&ioloop_timeval, &ioloop_timezone);
 	ioloop_time = ioloop_timeval.tv_sec;
 
 	if (ioloop->timeouts == NULL || !ioloop->timeouts->run_now)
@@ -306,7 +307,7 @@
 	IOLoop ioloop;
 
 	/* initialize time */
-	gettimeofday(&ioloop_timeval, NULL);
+	gettimeofday(&ioloop_timeval, &ioloop_timezone);
 	ioloop_time = ioloop_timeval.tv_sec;
 
         ioloop = p_new(pool, struct _IOLoop, 1);

Index: ioloop.h
===================================================================
RCS file: /home/cvs/dovecot/src/lib/ioloop.h,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ioloop.h	8 Oct 2002 23:26:08 -0000	1.3
+++ ioloop.h	24 Oct 2002 00:15:38 -0000	1.4
@@ -17,6 +17,7 @@
    Can be used instead of time(NULL). */
 extern time_t ioloop_time;
 extern struct timeval ioloop_timeval;
+extern struct timezone ioloop_timezone;
 
 /* I/O listeners - you can create different handlers for IO_READ and IO_WRITE,
    but make sure you don't create multiple handlers of same type, it's not

--- gmtoff.c DELETED ---

--- gmtoff.h DELETED ---




More information about the dovecot-cvs mailing list