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/kern/subr_clock.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 /*-
    2  * Copyright (c) 1988 University of Utah.
    3  * Copyright (c) 1982, 1990, 1993
    4  *      The Regents of the University of California.  All rights reserved.
    5  *
    6  * This code is derived from software contributed to Berkeley by
    7  * the Systems Programming Group of the University of Utah Computer
    8  * Science Department.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  * 4. Neither the name of the University nor the names of its contributors
   19  *    may be used to endorse or promote products derived from this software
   20  *    without specific prior written permission.
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   32  * SUCH DAMAGE.
   33  *
   34  *      from: Utah $Hdr: clock.c 1.18 91/01/21$
   35  *      from: @(#)clock.c       8.2 (Berkeley) 1/12/94
   36  *      from: NetBSD: clock_subr.c,v 1.6 2001/07/07 17:04:02 thorpej Exp
   37  *      and
   38  *      from: src/sys/i386/isa/clock.c,v 1.176 2001/09/04
   39  */
   40 
   41 #include <sys/cdefs.h>
   42 __FBSDID("$FreeBSD: releng/10.1/sys/kern/subr_clock.c 265687 2014-05-08 16:12:38Z brooks $");
   43 
   44 #include <sys/param.h>
   45 #include <sys/systm.h>
   46 #include <sys/kernel.h>
   47 #include <sys/bus.h>
   48 #include <sys/clock.h>
   49 #include <sys/limits.h>
   50 #include <sys/sysctl.h>
   51 #include <sys/timetc.h>
   52 
   53 int tz_minuteswest;
   54 int tz_dsttime;
   55 
   56 /*
   57  * The adjkerntz and wall_cmos_clock sysctls are in the "machdep" sysctl
   58  * namespace because they were misplaced there originally.
   59  */
   60 static int adjkerntz;
   61 static int
   62 sysctl_machdep_adjkerntz(SYSCTL_HANDLER_ARGS)
   63 {
   64         int error;
   65         error = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req);
   66         if (!error && req->newptr)
   67                 resettodr();
   68         return (error);
   69 }
   70 SYSCTL_PROC(_machdep, OID_AUTO, adjkerntz, CTLTYPE_INT|CTLFLAG_RW,
   71     &adjkerntz, 0, sysctl_machdep_adjkerntz, "I",
   72     "Local offset from UTC in seconds");
   73 
   74 static int ct_debug;
   75 SYSCTL_INT(_debug, OID_AUTO, clocktime, CTLFLAG_RW,
   76     &ct_debug, 0, "Enable printing of clocktime debugging");
   77 
   78 static int wall_cmos_clock;
   79 SYSCTL_INT(_machdep, OID_AUTO, wall_cmos_clock, CTLFLAG_RW,
   80     &wall_cmos_clock, 0, "Enables application of machdep.adjkerntz");
   81 
   82 /*--------------------------------------------------------------------*
   83  * Generic routines to convert between a POSIX date
   84  * (seconds since 1/1/1970) and yr/mo/day/hr/min/sec
   85  * Derived from NetBSD arch/hp300/hp300/clock.c
   86  */
   87 
   88 
   89 #define FEBRUARY        2
   90 #define days_in_year(y)         (leapyear(y) ? 366 : 365)
   91 #define days_in_month(y, m) \
   92         (month_days[(m) - 1] + (m == FEBRUARY ? leapyear(y) : 0))
   93 /* Day of week. Days are counted from 1/1/1970, which was a Thursday */
   94 #define day_of_week(days)       (((days) + 4) % 7)
   95 
   96 static const int month_days[12] = {
   97         31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
   98 };
   99 
  100 
  101 /*
  102  * This inline avoids some unnecessary modulo operations
  103  * as compared with the usual macro:
  104  *   ( ((year % 4) == 0 &&
  105  *      (year % 100) != 0) ||
  106  *     ((year % 400) == 0) )
  107  * It is otherwise equivalent.
  108  */
  109 static int
  110 leapyear(int year)
  111 {
  112         int rv = 0;
  113 
  114         if ((year & 3) == 0) {
  115                 rv = 1;
  116                 if ((year % 100) == 0) {
  117                         rv = 0;
  118                         if ((year % 400) == 0)
  119                                 rv = 1;
  120                 }
  121         }
  122         return (rv);
  123 }
  124 
  125 static void
  126 print_ct(struct clocktime *ct)
  127 {
  128         printf("[%04d-%02d-%02d %02d:%02d:%02d]",
  129             ct->year, ct->mon, ct->day,
  130             ct->hour, ct->min, ct->sec);
  131 }
  132 
  133 int
  134 clock_ct_to_ts(struct clocktime *ct, struct timespec *ts)
  135 {
  136         time_t secs;
  137         int i, year, days;
  138 
  139         year = ct->year;
  140 
  141         if (ct_debug) {
  142                 printf("ct_to_ts(");
  143                 print_ct(ct);
  144                 printf(")");
  145         }
  146 
  147         /* Sanity checks. */
  148         if (ct->mon < 1 || ct->mon > 12 || ct->day < 1 ||
  149             ct->day > days_in_month(year, ct->mon) ||
  150             ct->hour > 23 ||  ct->min > 59 || ct->sec > 59 ||
  151             (sizeof(time_t) == 4 && year > 2037)) {     /* time_t overflow */
  152                 if (ct_debug)
  153                         printf(" = EINVAL\n");
  154                 return (EINVAL);
  155         }
  156 
  157         /*
  158          * Compute days since start of time
  159          * First from years, then from months.
  160          */
  161         days = 0;
  162         for (i = POSIX_BASE_YEAR; i < year; i++)
  163                 days += days_in_year(i);
  164 
  165         /* Months */
  166         for (i = 1; i < ct->mon; i++)
  167                 days += days_in_month(year, i);
  168         days += (ct->day - 1);
  169 
  170         /* Add hours, minutes, seconds. */
  171         secs = ((days * 24 + ct->hour) * 60 + ct->min) * 60 + ct->sec;
  172 
  173         ts->tv_sec = secs;
  174         ts->tv_nsec = ct->nsec;
  175         if (ct_debug)
  176                 printf(" = %ld.%09ld\n", (long)ts->tv_sec, (long)ts->tv_nsec);
  177         return (0);
  178 }
  179 
  180 void
  181 clock_ts_to_ct(struct timespec *ts, struct clocktime *ct)
  182 {
  183         int i, year, days;
  184         time_t rsec;    /* remainder seconds */
  185         time_t secs;
  186 
  187         secs = ts->tv_sec;
  188         days = secs / SECDAY;
  189         rsec = secs % SECDAY;
  190 
  191         ct->dow = day_of_week(days);
  192 
  193         /* Subtract out whole years, counting them in i. */
  194         for (year = POSIX_BASE_YEAR; days >= days_in_year(year); year++)
  195                 days -= days_in_year(year);
  196         ct->year = year;
  197 
  198         /* Subtract out whole months, counting them in i. */
  199         for (i = 1; days >= days_in_month(year, i); i++)
  200                 days -= days_in_month(year, i);
  201         ct->mon = i;
  202 
  203         /* Days are what is left over (+1) from all that. */
  204         ct->day = days + 1;
  205 
  206         /* Hours, minutes, seconds are easy */
  207         ct->hour = rsec / 3600;
  208         rsec = rsec % 3600;
  209         ct->min  = rsec / 60;
  210         rsec = rsec % 60;
  211         ct->sec  = rsec;
  212         ct->nsec = ts->tv_nsec;
  213         if (ct_debug) {
  214                 printf("ts_to_ct(%ld.%09ld) = ",
  215                     (long)ts->tv_sec, (long)ts->tv_nsec);
  216                 print_ct(ct);
  217                 printf("\n");
  218         }
  219 }
  220 
  221 int
  222 utc_offset(void)
  223 {
  224 
  225         return (tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0));
  226 }

Cache object: f318bde72389ed444b46cc8df59ac804


[ 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.