pkgsrc-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[pkgsrc/trunk]: pkgsrc/pkgtools/libnbcompat/files Add an implement of timegm(...



details:   https://anonhg.NetBSD.org/pkgsrc/rev/7fe861da7ee4
branches:  trunk
changeset: 540502:7fe861da7ee4
user:      joerg <joerg%pkgsrc.org@localhost>
date:      Fri Apr 04 15:20:00 2008 +0000

description:
Add an implement of timegm(3) from nsd (based on Python code).

diffstat:

 pkgtools/libnbcompat/files/nbcompat/time.h |   6 +-
 pkgtools/libnbcompat/files/timegm.c        |  80 ++++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+), 1 deletions(-)

diffs (102 lines):

diff -r 38bd26dd7ea4 -r 7fe861da7ee4 pkgtools/libnbcompat/files/nbcompat/time.h
--- a/pkgtools/libnbcompat/files/nbcompat/time.h        Fri Apr 04 15:19:57 2008 +0000
+++ b/pkgtools/libnbcompat/files/nbcompat/time.h        Fri Apr 04 15:20:00 2008 +0000
@@ -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 -r 38bd26dd7ea4 -r 7fe861da7ee4 pkgtools/libnbcompat/files/timegm.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/pkgtools/libnbcompat/files/timegm.c       Fri Apr 04 15:20:00 2008 +0000
@@ -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;
+}



Home | Main Index | Thread Index | Old Index