]> sourceware.org Git - glibc.git/blame - time/offtime.c
Update.
[glibc.git] / time / offtime.c
CommitLineData
b77e6cd6 1/* Copyright (C) 1991, 1993, 1997, 1998 Free Software Foundation, Inc.
fd26970f 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
RM
21#define SECS_PER_HOUR (60 * 60)
22#define SECS_PER_DAY (SECS_PER_HOUR * 24)
23
c2216480
RM
24/* Compute the `struct tm' representation of *T,
25 offset OFFSET seconds east of UTC,
fe0ec73e
UD
26 and store year, yday, mon, mday, wday, hour, min, sec into *TP.
27 Return nonzero if successful. */
28int
fd26970f
UD
29__offtime (t, offset, tp)
30 const time_t *t;
31 long int offset;
32 struct tm *tp;
28f540f4 33{
9a0a462c
UD
34 long int days, rem, y;
35 const unsigned short int *ip;
28f540f4 36
28f540f4
RM
37 days = *t / SECS_PER_DAY;
38 rem = *t % SECS_PER_DAY;
39 rem += offset;
40 while (rem < 0)
41 {
42 rem += SECS_PER_DAY;
43 --days;
44 }
45 while (rem >= SECS_PER_DAY)
46 {
47 rem -= SECS_PER_DAY;
48 ++days;
49 }
c2216480 50 tp->tm_hour = rem / SECS_PER_HOUR;
28f540f4 51 rem %= SECS_PER_HOUR;
c2216480
RM
52 tp->tm_min = rem / 60;
53 tp->tm_sec = rem % 60;
28f540f4 54 /* January 1, 1970 was a Thursday. */
c2216480
RM
55 tp->tm_wday = (4 + days) % 7;
56 if (tp->tm_wday < 0)
57 tp->tm_wday += 7;
28f540f4 58 y = 1970;
b20e47cb 59
fe0ec73e
UD
60#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
61#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
b20e47cb
RM
62
63 while (days < 0 || days >= (__isleap (y) ? 366 : 365))
28f540f4 64 {
b20e47cb 65 /* Guess a corrected year, assuming 365 days per year. */
e61abf83 66 long int yg = y + days / 365 - (days % 365 < 0);
b20e47cb
RM
67
68 /* Adjust DAYS and Y to match the guessed year. */
69 days -= ((yg - y) * 365
70 + LEAPS_THRU_END_OF (yg - 1)
71 - LEAPS_THRU_END_OF (y - 1));
72 y = yg;
28f540f4 73 }
c2216480 74 tp->tm_year = y - 1900;
fe0ec73e
UD
75 if (tp->tm_year != y - 1900)
76 return 0;
c2216480 77 tp->tm_yday = days;
80fd7387 78 ip = __mon_yday[__isleap(y)];
9a0a462c 79 for (y = 11; days < (long int) ip[y]; --y)
80fd7387
RM
80 continue;
81 days -= ip[y];
c2216480
RM
82 tp->tm_mon = y;
83 tp->tm_mday = days + 1;
fe0ec73e 84 return 1;
28f540f4 85}
This page took 0.133125 seconds and 5 git commands to generate.