Fri Apr 4 15:20:00 2008 UTC ()
Add an implement of timegm(3) from nsd (based on Python code).


(joerg)
diff -r0 -r1.4 pkgsrc/pkgtools/libnbcompat/files/timegm.c
diff -r1.1 -r1.2 pkgsrc/pkgtools/libnbcompat/files/nbcompat/time.h

File Added: pkgsrc/pkgtools/libnbcompat/files/timegm.c
/*
 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
 *
 * This software is open source.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 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.
 *
 * Neither the name of the NLNET LABS 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 COPYRIGHT HOLDERS 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.
 */

#include <time.h>

/* Number of days per month (except for February in leap years). */
static const int monoff[] = {
	0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 324, 354
};

static int
is_leap_year(int year)
{
	return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

static int
leap_days(int y1, int y2)
{
	--y1;
	--y2;
	return (y2/4 - y1/4) - (y2/100 - y1/100) + (y2/400 - y1/400);
}

/*
 * Code adapted from Python 2.4.1 sources (Lib/calendar.py).
 */
time_t
timegm(const struct tm *tm)
{
	int year;
	time_t days;
	time_t hours;
	time_t minutes;
	time_t seconds;

	year = 1900 + tm->tm_year;
	days = 365 * (year - 1970) + leap_days(1970, year);
	days += monoff[tm->tm_mon];

	if (tm->tm_mon > 1 && is_leap_year(year))
		++days;
	days += tm->tm_mday - 1;

	hours = days * 24 + tm->tm_hour;
	minutes = hours * 60 + tm->tm_min;
	seconds = minutes * 60 + tm->tm_sec;

	return seconds;
}

cvs diff -r1.1 -r1.2 pkgsrc/pkgtools/libnbcompat/files/nbcompat/time.h (expand / switch to unified diff)

--- pkgsrc/pkgtools/libnbcompat/files/nbcompat/time.h 2004/08/10 18:47:55 1.1
+++ pkgsrc/pkgtools/libnbcompat/files/nbcompat/time.h 2008/04/04 15:20:00 1.2
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: time.h,v 1.1 2004/08/10 18:47:55 jlam Exp $ */ 1/* $NetBSD: time.h,v 1.2 2008/04/04 15:20:00 joerg Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc. 4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Johnny C. Lam. 8 * by Johnny C. Lam.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -61,14 +61,18 @@ @@ -61,14 +61,18 @@
61 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ 61 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \
62 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ 62 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \
63 if ((vvp)->tv_usec < 0) { \ 63 if ((vvp)->tv_usec < 0) { \
64 (vvp)->tv_sec--; \ 64 (vvp)->tv_sec--; \
65 (vvp)->tv_usec += 1000000; \ 65 (vvp)->tv_usec += 1000000; \
66 } \ 66 } \
67 } while (0) 67 } while (0)
68#endif 68#endif
69 69
70#if !HAVE_UTIMES 70#if !HAVE_UTIMES
71int utimes(const char *, const struct timeval *); 71int utimes(const char *, const struct timeval *);
72#endif 72#endif
73 73
 74#if !HAVE_TIMEGM
 75time_t timegm(struct tm *);
 76#endif
 77
74#endif /* !_NBCOMPAT_TIME_H_ */ 78#endif /* !_NBCOMPAT_TIME_H_ */