(__nss_lookup_function): En/Decrypt cached function pointers.
[glibc/history.git] / time / offtime.c
blobc0b1db59bd795eab835448bfe27e9073914ed9f8
1 /* Copyright (C) 1991, 1993, 1997, 1998, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <time.h>
22 #define SECS_PER_HOUR (60 * 60)
23 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
25 /* Compute the `struct tm' representation of *T,
26 offset OFFSET seconds east of UTC,
27 and store year, yday, mon, mday, wday, hour, min, sec into *TP.
28 Return nonzero if successful. */
29 int
30 __offtime (t, offset, tp)
31 const time_t *t;
32 long int offset;
33 struct tm *tp;
35 long int days, rem, y;
36 const unsigned short int *ip;
38 days = *t / SECS_PER_DAY;
39 rem = *t % SECS_PER_DAY;
40 rem += offset;
41 while (rem < 0)
43 rem += SECS_PER_DAY;
44 --days;
46 while (rem >= SECS_PER_DAY)
48 rem -= SECS_PER_DAY;
49 ++days;
51 tp->tm_hour = rem / SECS_PER_HOUR;
52 rem %= SECS_PER_HOUR;
53 tp->tm_min = rem / 60;
54 tp->tm_sec = rem % 60;
55 /* January 1, 1970 was a Thursday. */
56 tp->tm_wday = (4 + days) % 7;
57 if (tp->tm_wday < 0)
58 tp->tm_wday += 7;
59 y = 1970;
61 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
62 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
64 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
66 /* Guess a corrected year, assuming 365 days per year. */
67 long int yg = y + days / 365 - (days % 365 < 0);
69 /* Adjust DAYS and Y to match the guessed year. */
70 days -= ((yg - y) * 365
71 + LEAPS_THRU_END_OF (yg - 1)
72 - LEAPS_THRU_END_OF (y - 1));
73 y = yg;
75 tp->tm_year = y - 1900;
76 if (tp->tm_year != y - 1900)
78 /* The year cannot be represented due to overflow. */
79 __set_errno (EOVERFLOW);
80 return 0;
82 tp->tm_yday = days;
83 ip = __mon_yday[__isleap(y)];
84 for (y = 11; days < (long int) ip[y]; --y)
85 continue;
86 days -= ip[y];
87 tp->tm_mon = y;
88 tp->tm_mday = days + 1;
89 return 1;