Skip to content
getdate.y 28 KiB
Newer Older
Daniel Stenberg's avatar
Daniel Stenberg committed
    }
  tm.tm_hour += cookie.yyRelHour;
  tm.tm_min += cookie.yyRelMinutes;
  tm.tm_sec += cookie.yyRelSeconds;
Daniel Stenberg's avatar
Daniel Stenberg committed

  /* Let mktime deduce tm_isdst if we have an absolute timestamp,
     or if the relative timestamp mentions days, months, or years.  */
  if (cookie.yyHaveDate | cookie.yyHaveDay | cookie.yyHaveTime |
      cookie.yyRelDay | cookie.yyRelMonth | cookie.yyRelYear)
Daniel Stenberg's avatar
Daniel Stenberg committed
    tm.tm_isdst = -1;

  tm0 = tm;

  Start = mktime (&tm);

  if (Start == (time_t) -1)
    {

      /* Guard against falsely reporting errors near the time_t boundaries
         when parsing times in other time zones.  For example, if the min
         time_t value is 1970-01-01 00:00:00 UTC and we are 8 hours ahead
         of UTC, then the min localtime value is 1970-01-01 08:00:00; if
         we apply mktime to 1970-01-01 00:00:00 we will get an error, so
         we apply mktime to 1970-01-02 08:00:00 instead and adjust the time
         zone by 24 hours to compensate.  This algorithm assumes that
         there is no DST transition within a day of the time_t boundaries.  */
      if (cookie.yyHaveZone)
Daniel Stenberg's avatar
Daniel Stenberg committed
	{
	  tm = tm0;
	  if (tm.tm_year <= EPOCH - TM_YEAR_ORIGIN)
	    {
	      tm.tm_mday++;
	      cookie.yyTimezone -= 24 * 60;
Daniel Stenberg's avatar
Daniel Stenberg committed
	    }
	  else
	    {
	      tm.tm_mday--;
	      cookie.yyTimezone += 24 * 60;
Daniel Stenberg's avatar
Daniel Stenberg committed
	    }
	  Start = mktime (&tm);
	}

      if (Start == (time_t) -1)
	return Start;
    }

  if (cookie.yyHaveDay && !cookie.yyHaveDate)
Daniel Stenberg's avatar
Daniel Stenberg committed
    {
      tm.tm_mday += ((cookie.yyDayNumber - tm.tm_wday + 7) % 7
		     + 7 * (cookie.yyDayOrdinal - (0 < cookie.yyDayOrdinal)));
Daniel Stenberg's avatar
Daniel Stenberg committed
      Start = mktime (&tm);
      if (Start == (time_t) -1)
	return Start;
    }

  if (cookie.yyHaveZone)
Daniel Stenberg's avatar
Daniel Stenberg committed
    {
      long delta;
      struct tm *gmt;
#ifdef HAVE_GMTIME_R
      /* thread-safe version */
      struct tm keeptime2;
      gmt = (struct tm *)gmtime_r(&Start, &keeptime2);
#else
      gmt = gmtime(&Start);
#endif
Daniel Stenberg's avatar
Daniel Stenberg committed
      if (!gmt)
	return -1;
      delta = cookie.yyTimezone * 60L + difftm (&tm, gmt);
Daniel Stenberg's avatar
Daniel Stenberg committed
      if ((Start + delta < Start) != (delta < 0))
	return -1;		/* time_t overflow */
      Start += delta;
    }

  return Start;
}

#if	defined (TEST)

/* ARGSUSED */
int
Daniel Stenberg's avatar
Daniel Stenberg committed
{
  char buff[MAX_BUFF_LEN + 1];
  time_t d;

  (void) printf ("Enter date, or blank line to exit.\n\t> ");
  (void) fflush (stdout);

  buff[MAX_BUFF_LEN] = 0;
  while (fgets (buff, MAX_BUFF_LEN, stdin) && buff[0])
    {
      d = curl_getdate (buff, (time_t *) NULL);
Daniel Stenberg's avatar
Daniel Stenberg committed
      if (d == -1)
	(void) printf ("Bad format - couldn't convert.\n");
      else
	(void) printf ("%s", ctime (&d));
      (void) printf ("\t> ");
      (void) fflush (stdout);
    }
  exit (0);
  /* NOTREACHED */
}
#endif /* defined (TEST) */