Blame time/offtime.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
#define	SECS_PER_HOUR	(60 * 60)
Packit 6c4009
#define	SECS_PER_DAY	(SECS_PER_HOUR * 24)
Packit 6c4009
Packit 6c4009
/* Compute the `struct tm' representation of *T,
Packit 6c4009
   offset OFFSET seconds east of UTC,
Packit 6c4009
   and store year, yday, mon, mday, wday, hour, min, sec into *TP.
Packit 6c4009
   Return nonzero if successful.  */
Packit 6c4009
int
Packit 6c4009
__offtime (const time_t *t, long int offset, struct tm *tp)
Packit 6c4009
{
Packit 6c4009
  time_t days, rem, y;
Packit 6c4009
  const unsigned short int *ip;
Packit 6c4009
Packit 6c4009
  days = *t / SECS_PER_DAY;
Packit 6c4009
  rem = *t % SECS_PER_DAY;
Packit 6c4009
  rem += offset;
Packit 6c4009
  while (rem < 0)
Packit 6c4009
    {
Packit 6c4009
      rem += SECS_PER_DAY;
Packit 6c4009
      --days;
Packit 6c4009
    }
Packit 6c4009
  while (rem >= SECS_PER_DAY)
Packit 6c4009
    {
Packit 6c4009
      rem -= SECS_PER_DAY;
Packit 6c4009
      ++days;
Packit 6c4009
    }
Packit 6c4009
  tp->tm_hour = rem / SECS_PER_HOUR;
Packit 6c4009
  rem %= SECS_PER_HOUR;
Packit 6c4009
  tp->tm_min = rem / 60;
Packit 6c4009
  tp->tm_sec = rem % 60;
Packit 6c4009
  /* January 1, 1970 was a Thursday.  */
Packit 6c4009
  tp->tm_wday = (4 + days) % 7;
Packit 6c4009
  if (tp->tm_wday < 0)
Packit 6c4009
    tp->tm_wday += 7;
Packit 6c4009
  y = 1970;
Packit 6c4009
Packit 6c4009
#define DIV(a, b) ((a) / (b) - ((a) % (b) < 0))
Packit 6c4009
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
Packit 6c4009
Packit 6c4009
  while (days < 0 || days >= (__isleap (y) ? 366 : 365))
Packit 6c4009
    {
Packit 6c4009
      /* Guess a corrected year, assuming 365 days per year.  */
Packit 6c4009
      time_t yg = y + days / 365 - (days % 365 < 0);
Packit 6c4009
Packit 6c4009
      /* Adjust DAYS and Y to match the guessed year.  */
Packit 6c4009
      days -= ((yg - y) * 365
Packit 6c4009
	       + LEAPS_THRU_END_OF (yg - 1)
Packit 6c4009
	       - LEAPS_THRU_END_OF (y - 1));
Packit 6c4009
      y = yg;
Packit 6c4009
    }
Packit 6c4009
  tp->tm_year = y - 1900;
Packit 6c4009
  if (tp->tm_year != y - 1900)
Packit 6c4009
    {
Packit 6c4009
      /* The year cannot be represented due to overflow.  */
Packit 6c4009
      __set_errno (EOVERFLOW);
Packit 6c4009
      return 0;
Packit 6c4009
    }
Packit 6c4009
  tp->tm_yday = days;
Packit 6c4009
  ip = __mon_yday[__isleap(y)];
Packit 6c4009
  for (y = 11; days < (long int) ip[y]; --y)
Packit 6c4009
    continue;
Packit 6c4009
  days -= ip[y];
Packit 6c4009
  tp->tm_mon = y;
Packit 6c4009
  tp->tm_mday = days + 1;
Packit 6c4009
  return 1;
Packit 6c4009
}