diff options
author | joerg <joerg> | 2008-04-04 15:20:00 +0000 |
---|---|---|
committer | joerg <joerg> | 2008-04-04 15:20:00 +0000 |
commit | 33038af574657d80ece424b487fc842193a0a6d2 (patch) | |
tree | 340e43f9aa2c897f2f2131a902b2ae8c04dc26fd /pkgtools/libnbcompat | |
parent | 56f89e3152f03ec08a47ff1fd312c4af564d4fd6 (diff) | |
download | pkgsrc-33038af574657d80ece424b487fc842193a0a6d2.tar.gz |
Add an implement of timegm(3) from nsd (based on Python code).
Diffstat (limited to 'pkgtools/libnbcompat')
-rw-r--r-- | pkgtools/libnbcompat/files/nbcompat/time.h | 6 | ||||
-rw-r--r-- | pkgtools/libnbcompat/files/timegm.c | 80 |
2 files changed, 85 insertions, 1 deletions
diff --git a/pkgtools/libnbcompat/files/nbcompat/time.h b/pkgtools/libnbcompat/files/nbcompat/time.h index ffd7e8d65af..f811fac39e9 100644 --- a/pkgtools/libnbcompat/files/nbcompat/time.h +++ b/pkgtools/libnbcompat/files/nbcompat/time.h @@ -1,4 +1,4 @@ -/* $NetBSD: time.h,v 1.1 2004/08/10 18:47:55 jlam Exp $ */ +/* $NetBSD: time.h,v 1.2 2008/04/04 15:20:00 joerg Exp $ */ /*- * Copyright (c) 2004 The NetBSD Foundation, Inc. @@ -71,4 +71,8 @@ int utimes(const char *, const struct timeval *); #endif +#if !HAVE_TIMEGM +time_t timegm(struct tm *); +#endif + #endif /* !_NBCOMPAT_TIME_H_ */ diff --git a/pkgtools/libnbcompat/files/timegm.c b/pkgtools/libnbcompat/files/timegm.c new file mode 100644 index 00000000000..9a8bebb35a6 --- /dev/null +++ b/pkgtools/libnbcompat/files/timegm.c @@ -0,0 +1,80 @@ +/* + * 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; +} |