~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/time/timeconv.c

Version: ~ [ linux-5.1-rc2 ] ~ [ linux-5.0.4 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.31 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.108 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.165 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.177 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.137 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
  3  * This file is part of the GNU C Library.
  4  * Contributed by Paul Eggert (eggert@twinsun.com).
  5  *
  6  * The GNU C Library is free software; you can redistribute it and/or
  7  * modify it under the terms of the GNU Library General Public License as
  8  * published by the Free Software Foundation; either version 2 of the
  9  * License, or (at your option) any later version.
 10  *
 11  * The GNU C Library is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 14  * Library General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU Library General Public
 17  * License along with the GNU C Library; see the file COPYING.LIB.  If not,
 18  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 19  * Boston, MA 02111-1307, USA.
 20  */
 21 
 22 /*
 23  * Converts the calendar time to broken-down time representation
 24  * Based on code from glibc-2.6
 25  *
 26  * 2009-7-14:
 27  *   Moved from glibc-2.6 to kernel by Zhaolei<zhaolei@cn.fujitsu.com>
 28  */
 29 
 30 #include <linux/time.h>
 31 #include <linux/module.h>
 32 
 33 /*
 34  * Nonzero if YEAR is a leap year (every 4 years,
 35  * except every 100th isn't, and every 400th is).
 36  */
 37 static int __isleap(long year)
 38 {
 39         return (year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0);
 40 }
 41 
 42 /* do a mathdiv for long type */
 43 static long math_div(long a, long b)
 44 {
 45         return a / b - (a % b < 0);
 46 }
 47 
 48 /* How many leap years between y1 and y2, y1 must less or equal to y2 */
 49 static long leaps_between(long y1, long y2)
 50 {
 51         long leaps1 = math_div(y1 - 1, 4) - math_div(y1 - 1, 100)
 52                 + math_div(y1 - 1, 400);
 53         long leaps2 = math_div(y2 - 1, 4) - math_div(y2 - 1, 100)
 54                 + math_div(y2 - 1, 400);
 55         return leaps2 - leaps1;
 56 }
 57 
 58 /* How many days come before each month (0-12). */
 59 static const unsigned short __mon_yday[2][13] = {
 60         /* Normal years. */
 61         {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365},
 62         /* Leap years. */
 63         {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366}
 64 };
 65 
 66 #define SECS_PER_HOUR   (60 * 60)
 67 #define SECS_PER_DAY    (SECS_PER_HOUR * 24)
 68 
 69 /**
 70  * time64_to_tm - converts the calendar time to local broken-down time
 71  *
 72  * @totalsecs   the number of seconds elapsed since 00:00:00 on January 1, 1970,
 73  *              Coordinated Universal Time (UTC).
 74  * @offset      offset seconds adding to totalsecs.
 75  * @result      pointer to struct tm variable to receive broken-down time
 76  */
 77 void time64_to_tm(time64_t totalsecs, int offset, struct tm *result)
 78 {
 79         long days, rem, y;
 80         int remainder;
 81         const unsigned short *ip;
 82 
 83         days = div_s64_rem(totalsecs, SECS_PER_DAY, &remainder);
 84         rem = remainder;
 85         rem += offset;
 86         while (rem < 0) {
 87                 rem += SECS_PER_DAY;
 88                 --days;
 89         }
 90         while (rem >= SECS_PER_DAY) {
 91                 rem -= SECS_PER_DAY;
 92                 ++days;
 93         }
 94 
 95         result->tm_hour = rem / SECS_PER_HOUR;
 96         rem %= SECS_PER_HOUR;
 97         result->tm_min = rem / 60;
 98         result->tm_sec = rem % 60;
 99 
100         /* January 1, 1970 was a Thursday. */
101         result->tm_wday = (4 + days) % 7;
102         if (result->tm_wday < 0)
103                 result->tm_wday += 7;
104 
105         y = 1970;
106 
107         while (days < 0 || days >= (__isleap(y) ? 366 : 365)) {
108                 /* Guess a corrected year, assuming 365 days per year. */
109                 long yg = y + math_div(days, 365);
110 
111                 /* Adjust DAYS and Y to match the guessed year. */
112                 days -= (yg - y) * 365 + leaps_between(y, yg);
113                 y = yg;
114         }
115 
116         result->tm_year = y - 1900;
117 
118         result->tm_yday = days;
119 
120         ip = __mon_yday[__isleap(y)];
121         for (y = 11; days < ip[y]; y--)
122                 continue;
123         days -= ip[y];
124 
125         result->tm_mon = y;
126         result->tm_mday = days + 1;
127 }
128 EXPORT_SYMBOL(time64_to_tm);
129 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp