Update.
[glibc.git] / time / offtime.c
blob52e40703c83e4417c58df1197fd699750c0fc286
1 /* Copyright (C) 1991, 1993, 1997 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <time.h>
21 /* Defined in mktime.c. */
22 extern const unsigned short int __mon_yday[2][13];
24 #define SECS_PER_HOUR (60 * 60)
25 #define SECS_PER_DAY (SECS_PER_HOUR * 24)
27 /* Compute the `struct tm' representation of *T,
28 offset OFFSET seconds east of UTC,
29 and store year, yday, mon, mday, wday, hour, min, sec into *TP.
30 Return nonzero if successful. */
31 int
32 __offtime (t, offset, tp)
33 const time_t *t;
34 long int offset;
35 struct tm *tp;
37 long int days, rem, y;
38 const unsigned short int *ip;
40 days = *t / SECS_PER_DAY;
41 rem = *t % SECS_PER_DAY;
42 rem += offset;
43 while (rem < 0)
45 rem += SECS_PER_DAY;
46 --days;
48 while (rem >= SECS_PER_DAY)
50 rem -= SECS_PER_DAY;
51 ++days;
53 tp->tm_hour = rem / SECS_PER_HOUR;
54 rem %= SECS_PER_HOUR;
55 tp->tm_min = rem / 60;
56 tp->tm_sec = rem % 60;
57 /* January 1, 1970 was a Thursday. */
58 tp->tm_wday = (4 + days) % 7;
59 if (tp->tm_wday < 0)
60 tp->tm_wday += 7;
61 y = 1970;
63 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
64 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
66 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
68 /* Guess a corrected year, assuming 365 days per year. */
69 long int yg = y + days / 365 - (days % 365 < 0);
71 /* Adjust DAYS and Y to match the guessed year. */
72 days -= ((yg - y) * 365
73 + LEAPS_THRU_END_OF (yg - 1)
74 - LEAPS_THRU_END_OF (y - 1));
75 y = yg;
77 tp->tm_year = y - 1900;
78 if (tp->tm_year != y - 1900)
79 return 0;
80 tp->tm_yday = days;
81 ip = __mon_yday[__isleap(y)];
82 for (y = 11; days < (long int) ip[y]; --y)
83 continue;
84 days -= ip[y];
85 tp->tm_mon = y;
86 tp->tm_mday = days + 1;
87 return 1;