The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/clock_subr.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: clock_subr.c,v 1.9 2003/08/13 11:35:25 ragge Exp $     */
    2 
    3 /*
    4  * Copyright (c) 1982, 1990, 1993
    5  *      The Regents of the University of California.  All rights reserved.
    6  *
    7  * This code is derived from software contributed to Berkeley by
    8  * the Systems Programming Group of the University of Utah Computer
    9  * Science Department.
   10  *
   11  * Redistribution and use in source and binary forms, with or without
   12  * modification, are permitted provided that the following conditions
   13  * are met:
   14  * 1. Redistributions of source code must retain the above copyright
   15  *    notice, this list of conditions and the following disclaimer.
   16  * 2. Redistributions in binary form must reproduce the above copyright
   17  *    notice, this list of conditions and the following disclaimer in the
   18  *    documentation and/or other materials provided with the distribution.
   19  * 3. Neither the name of the University nor the names of its contributors
   20  *    may be used to endorse or promote products derived from this software
   21  *    without specific prior written permission.
   22  *
   23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   33  * SUCH DAMAGE.
   34  *
   35  * from: Utah $Hdr: clock.c 1.18 91/01/21$
   36  *
   37  *      @(#)clock.c     8.2 (Berkeley) 1/12/94
   38  */
   39 
   40 /*
   41  * Copyright (c) 1988 University of Utah.
   42  *
   43  * This code is derived from software contributed to Berkeley by
   44  * the Systems Programming Group of the University of Utah Computer
   45  * Science Department.
   46  *
   47  * Redistribution and use in source and binary forms, with or without
   48  * modification, are permitted provided that the following conditions
   49  * are met:
   50  * 1. Redistributions of source code must retain the above copyright
   51  *    notice, this list of conditions and the following disclaimer.
   52  * 2. Redistributions in binary form must reproduce the above copyright
   53  *    notice, this list of conditions and the following disclaimer in the
   54  *    documentation and/or other materials provided with the distribution.
   55  * 3. All advertising materials mentioning features or use of this software
   56  *    must display the following acknowledgement:
   57  *      This product includes software developed by the University of
   58  *      California, Berkeley and its contributors.
   59  * 4. Neither the name of the University nor the names of its contributors
   60  *    may be used to endorse or promote products derived from this software
   61  *    without specific prior written permission.
   62  *
   63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   73  * SUCH DAMAGE.
   74  *
   75  * from: Utah $Hdr: clock.c 1.18 91/01/21$
   76  *
   77  *      @(#)clock.c     8.2 (Berkeley) 1/12/94
   78  */
   79 
   80 /*
   81  * Generic routines to convert between a POSIX date
   82  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
   83  * Derived from arch/hp300/hp300/clock.c
   84  */
   85 
   86 #include <sys/cdefs.h>
   87 __KERNEL_RCSID(0, "$NetBSD: clock_subr.c,v 1.9 2003/08/13 11:35:25 ragge Exp $");
   88 
   89 #include <sys/param.h>
   90 #include <sys/systm.h>
   91 
   92 #include <dev/clock_subr.h>
   93 
   94 static inline int leapyear __P((int year));
   95 #define FEBRUARY        2
   96 #define days_in_year(a)         (leapyear(a) ? 366 : 365)
   97 #define days_in_month(a)        (month_days[(a) - 1])
   98 
   99 static const int month_days[12] = {
  100         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  101 };
  102 
  103 /*
  104  * This inline avoids some unnecessary modulo operations
  105  * as compared with the usual macro:
  106  *   ( ((year % 4) == 0 &&
  107  *      (year % 100) != 0) ||
  108  *     ((year % 400) == 0) )
  109  * It is otherwise equivalent.
  110  */
  111 static inline int
  112 leapyear(year)
  113         int year;
  114 {
  115         int rv = 0;
  116 
  117         if ((year & 3) == 0) {
  118                 rv = 1;
  119                 if ((year % 100) == 0) {
  120                         rv = 0;
  121                         if ((year % 400) == 0)
  122                                 rv = 1;
  123                 }
  124         }
  125         return (rv);
  126 }
  127 
  128 time_t
  129 clock_ymdhms_to_secs(dt)
  130         struct clock_ymdhms *dt;
  131 {
  132         time_t secs;
  133         int i, year, days;
  134 
  135         year = dt->dt_year;
  136 
  137         /*
  138          * Compute days since start of time
  139          * First from years, then from months.
  140          */
  141         days = 0;
  142         for (i = POSIX_BASE_YEAR; i < year; i++)
  143                 days += days_in_year(i);
  144         if (leapyear(year) && dt->dt_mon > FEBRUARY)
  145                 days++;
  146 
  147         /* Months */
  148         for (i = 1; i < dt->dt_mon; i++)
  149                 days += days_in_month(i);
  150         days += (dt->dt_day - 1);
  151 
  152         /* Add hours, minutes, seconds. */
  153         secs = ((days
  154             * 24 + dt->dt_hour)
  155             * 60 + dt->dt_min)
  156             * 60 + dt->dt_sec;
  157 
  158         return (secs);
  159 }
  160 
  161 void
  162 clock_secs_to_ymdhms(secs, dt)
  163         time_t secs;
  164         struct clock_ymdhms *dt;
  165 {
  166         int mthdays[12];
  167         int i, days;
  168         int rsec;       /* remainder seconds */
  169 
  170         /*
  171          * This function uses a local copy of month_days[]
  172          * so the copy can be modified (and thread-safe).
  173          * See the definition of days_in_month() above.
  174          */
  175         memcpy(mthdays, month_days, sizeof(mthdays));
  176 #define month_days mthdays
  177 
  178         days = secs / SECDAY;
  179         rsec = secs % SECDAY;
  180 
  181         /* Day of week (Note: 1/1/1970 was a Thursday) */
  182         dt->dt_wday = (days + 4) % 7;
  183 
  184         /* Subtract out whole years, counting them in i. */
  185         for (i = POSIX_BASE_YEAR; days >= days_in_year(i); i++)
  186                 days -= days_in_year(i);
  187         dt->dt_year = i;
  188 
  189         /* Subtract out whole months, counting them in i. */
  190         if (leapyear(i))
  191                 days_in_month(FEBRUARY) = 29;
  192         for (i = 1; days >= days_in_month(i); i++)
  193                 days -= days_in_month(i);
  194         dt->dt_mon = i;
  195 
  196         /* Days are what is left over (+1) from all that. */
  197         dt->dt_day = days + 1;
  198 
  199         /* Hours, minutes, seconds are easy */
  200         dt->dt_hour = rsec / 3600;
  201         rsec = rsec % 3600;
  202         dt->dt_min  = rsec / 60;
  203         rsec = rsec % 60;
  204         dt->dt_sec  = rsec;
  205 #undef month_days
  206 }

Cache object: 8a608b4d203d2e40df304406eb3ff6ca


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.