]> sourceware.org Git - glibc.git/blame - time/offtime.c
Thu Oct 5 00:59:58 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[glibc.git] / time / offtime.c
CommitLineData
28f540f4
RM
1/* Copyright (C) 1991, 1993 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19#include <ansidecl.h>
20#include <time.h>
21
22
23/* Defined in mktime.c. */
24extern CONST unsigned short int __mon_lengths[2][12];
25
26#define SECS_PER_HOUR (60 * 60)
27#define SECS_PER_DAY (SECS_PER_HOUR * 24)
28
c2216480
RM
29/* Compute the `struct tm' representation of *T,
30 offset OFFSET seconds east of UTC,
31 and store year, yday, mon, mday, wday, hour, min, sec into *TP. */
32void
33DEFUN(__offtime, (t, offset, tp),
34 CONST time_t *t AND long int offset AND struct tm *tp)
28f540f4 35{
28f540f4
RM
36 register long int days, rem;
37 register int y;
38 register CONST unsigned short int *ip;
39
28f540f4
RM
40 days = *t / SECS_PER_DAY;
41 rem = *t % SECS_PER_DAY;
42 rem += offset;
43 while (rem < 0)
44 {
45 rem += SECS_PER_DAY;
46 --days;
47 }
48 while (rem >= SECS_PER_DAY)
49 {
50 rem -= SECS_PER_DAY;
51 ++days;
52 }
c2216480 53 tp->tm_hour = rem / SECS_PER_HOUR;
28f540f4 54 rem %= SECS_PER_HOUR;
c2216480
RM
55 tp->tm_min = rem / 60;
56 tp->tm_sec = rem % 60;
28f540f4 57 /* January 1, 1970 was a Thursday. */
c2216480
RM
58 tp->tm_wday = (4 + days) % 7;
59 if (tp->tm_wday < 0)
60 tp->tm_wday += 7;
28f540f4
RM
61 y = 1970;
62 while (days >= (rem = __isleap(y) ? 366 : 365))
63 {
64 ++y;
65 days -= rem;
66 }
67 while (days < 0)
68 {
69 --y;
70 days += __isleap(y) ? 366 : 365;
71 }
c2216480
RM
72 tp->tm_year = y - 1900;
73 tp->tm_yday = days;
28f540f4
RM
74 ip = __mon_lengths[__isleap(y)];
75 for (y = 0; days >= ip[y]; ++y)
76 days -= ip[y];
c2216480
RM
77 tp->tm_mon = y;
78 tp->tm_mday = days + 1;
79 tp->tm_isdst = -1;
28f540f4 80}
This page took 0.043061 seconds and 5 git commands to generate.