]> git.ipfire.org Git - thirdparty/glibc.git/blame - time/offtime.c
update from main archive 970120
[thirdparty/glibc.git] / time / offtime.c
CommitLineData
fd26970f
UD
1/* Copyright (C) 1991, 1993, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
28f540f4 3
fd26970f
UD
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.
28f540f4 8
fd26970f
UD
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.
28f540f4 13
fd26970f
UD
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. */
28f540f4 18
28f540f4
RM
19#include <time.h>
20
28f540f4 21/* Defined in mktime.c. */
fd26970f 22extern const unsigned short int __mon_yday[2][13];
28f540f4
RM
23
24#define SECS_PER_HOUR (60 * 60)
25#define SECS_PER_DAY (SECS_PER_HOUR * 24)
26
c2216480
RM
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. */
30void
fd26970f
UD
31__offtime (t, offset, tp)
32 const time_t *t;
33 long int offset;
34 struct tm *tp;
28f540f4 35{
b20e47cb 36 register long int days, rem, y;
fd26970f 37 register const unsigned short int *ip;
28f540f4 38
28f540f4
RM
39 days = *t / SECS_PER_DAY;
40 rem = *t % SECS_PER_DAY;
41 rem += offset;
42 while (rem < 0)
43 {
44 rem += SECS_PER_DAY;
45 --days;
46 }
47 while (rem >= SECS_PER_DAY)
48 {
49 rem -= SECS_PER_DAY;
50 ++days;
51 }
c2216480 52 tp->tm_hour = rem / SECS_PER_HOUR;
28f540f4 53 rem %= SECS_PER_HOUR;
c2216480
RM
54 tp->tm_min = rem / 60;
55 tp->tm_sec = rem % 60;
28f540f4 56 /* January 1, 1970 was a Thursday. */
c2216480
RM
57 tp->tm_wday = (4 + days) % 7;
58 if (tp->tm_wday < 0)
59 tp->tm_wday += 7;
28f540f4 60 y = 1970;
b20e47cb 61
fd26970f 62#define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
b20e47cb
RM
63
64 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
28f540f4 65 {
b20e47cb
RM
66 /* Guess a corrected year, assuming 365 days per year. */
67 int yg = y + days / 365 - (days % 365 < 0);
68
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;
28f540f4 74 }
c2216480
RM
75 tp->tm_year = y - 1900;
76 tp->tm_yday = days;
80fd7387
RM
77 ip = __mon_yday[__isleap(y)];
78 for (y = 11; days < ip[y]; --y)
79 continue;
80 days -= ip[y];
c2216480
RM
81 tp->tm_mon = y;
82 tp->tm_mday = days + 1;
28f540f4 83}