Back to index

glibc  2.9
offtime.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1993, 1997, 1998, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <time.h>
00021 
00022 #define       SECS_PER_HOUR (60 * 60)
00023 #define       SECS_PER_DAY  (SECS_PER_HOUR * 24)
00024 
00025 /* Compute the `struct tm' representation of *T,
00026    offset OFFSET seconds east of UTC,
00027    and store year, yday, mon, mday, wday, hour, min, sec into *TP.
00028    Return nonzero if successful.  */
00029 int
00030 __offtime (t, offset, tp)
00031      const time_t *t;
00032      long int offset;
00033      struct tm *tp;
00034 {
00035   long int days, rem, y;
00036   const unsigned short int *ip;
00037 
00038   days = *t / SECS_PER_DAY;
00039   rem = *t % SECS_PER_DAY;
00040   rem += offset;
00041   while (rem < 0)
00042     {
00043       rem += SECS_PER_DAY;
00044       --days;
00045     }
00046   while (rem >= SECS_PER_DAY)
00047     {
00048       rem -= SECS_PER_DAY;
00049       ++days;
00050     }
00051   tp->tm_hour = rem / SECS_PER_HOUR;
00052   rem %= SECS_PER_HOUR;
00053   tp->tm_min = rem / 60;
00054   tp->tm_sec = rem % 60;
00055   /* January 1, 1970 was a Thursday.  */
00056   tp->tm_wday = (4 + days) % 7;
00057   if (tp->tm_wday < 0)
00058     tp->tm_wday += 7;
00059   y = 1970;
00060 
00061 #define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
00062 #define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
00063 
00064   while (days < 0 || days >= (__isleap (y) ? 366 : 365))
00065     {
00066       /* Guess a corrected year, assuming 365 days per year.  */
00067       long int yg = y + days / 365 - (days % 365 < 0);
00068 
00069       /* Adjust DAYS and Y to match the guessed year.  */
00070       days -= ((yg - y) * 365
00071               + LEAPS_THRU_END_OF (yg - 1)
00072               - LEAPS_THRU_END_OF (y - 1));
00073       y = yg;
00074     }
00075   tp->tm_year = y - 1900;
00076   if (tp->tm_year != y - 1900)
00077     {
00078       /* The year cannot be represented due to overflow.  */
00079       __set_errno (EOVERFLOW);
00080       return 0;
00081     }
00082   tp->tm_yday = days;
00083   ip = __mon_yday[__isleap(y)];
00084   for (y = 11; days < (long int) ip[y]; --y)
00085     continue;
00086   days -= ip[y];
00087   tp->tm_mon = y;
00088   tp->tm_mday = days + 1;
00089   return 1;
00090 }