示例#1
0
int
days_btwn(int m1, int d1, int y1, int m2, int d2, int y2)
{
	/*
	 * calculate the number of "full" days in between
	 * m1/d1/y1 and m2/d2/y2.
	 * NOTE: there should not be more than a year separation in the
	 * dates. also, m should be in 0 to 11, and d should be in 1 to 31.
	 */

	int	days;
	int	m;

	if ((m1 == m2) && (d1 == d2) && (y1 == y2))
		return (0);
	if ((m1 == m2) && (d1 < d2)) {
		/*
		 * In case of d2==29 ,d1==28 and m1==m2==Feb and year is not
		 * a leap year, this function should return the days till the
		 * the next Feb 29.See Bug 4257355.
		 */
		if (d2 > days_in_mon(m2, y2)) {
			int p;
			for (p = 1; ! isleap(y2+YEAR+p); p++)
				;
			return (p*365 + d2-d1-1);
		}
		return (d2-d1-1);
	}
	/* the remaining dates are on different months */
	days = (days_in_mon(m1, y1)-d1) + (d2-1);
	m = (m1 + 1) % 12;
	while (m != m2) {
		if (m == 0)
			y1++;
		days += days_in_mon(m, y1);
		m = (m + 1) % 12;
	}
	return (days);
}
示例#2
0
文件: time.c 项目: plujon/hrs3-c
status time_parse(a_time *out, const char *s, size_t len)
{
  memset(out, 0, sizeof(a_time));
  if (len < time_string_length()) {
    return NO;
  }
  int year = s_to_d(s, 4, 0); s += 4;
  int mon = s_to_d(s, 2, 0); s += 2;
  if (mon <= 0 || 13 <= mon) return NO;
  int day = s_to_d(s, 2, 0); s += 2;
  if (day < 0 ||  days_in_mon(mon, year) < day) return NO;
  int hour = s_to_d(s, 2, 0); s += 2;
  if (hour < 0 || 24 <= hour) return NO;
  int minute = s_to_d(s, 2, 0); s += 2;
  if (minute < 0 || 60 <= minute) return NO;
  int second = s_to_d(s, 2, 0); /* s += 2; scan-build: dead increment */
  time_copy(out, time_now());
  if (!time_ymdhms(out, year, mon, day, hour, minute, second))
    return NO;
  return OK;
}