static void
app_datacal_totaldays_to_currday(int totaldays, int start_day, int start_month, int *year, int*month, int *day)
{
	int current_year = TIME_YEAR_BASE;
	int i;

	while(totaldays != 0 && totaldays >= 365){
		totaldays -= total_day_in_month[is_leap(current_year)][12];
		current_year +=1;
	}
	if (totaldays == 0){
		*year = current_year;
		*month = start_month;
		*day = start_day;
		return;
	}
	else {
		*year = current_year;
		i = 0;
		while(i < 12) {
			if ( totaldays > total_day_in_month[is_leap(current_year)][i] )
				i++;
			else
				break;
		}
		*month = i;
		totaldays -= total_day_in_month[is_leap(current_year)][i-1];
		*day = totaldays;
		return;
	}

}
Exemple #2
0
time_t timegm(
    struct tm *tm)
{
  static const unsigned ndays[2][12] = {
      {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
      {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};

  time_t res = 0;
  int i;

  for (i = 70; i < tm->tm_year; ++i)
    res += is_leap(i) ? 366 : 365;

  for (i = 0; i < tm->tm_mon; ++i)
    res += ndays[is_leap(tm->tm_year)][i];

  res += tm->tm_mday - 1;
  res *= 24;
  res += tm->tm_hour;
  res *= 60;
  res += tm->tm_min;
  res *= 60;
  res += tm->tm_sec;

  return res;
}
Exemple #3
0
/* Drop-in replacement for timegm function as some plattforms strip the function from their libc.. */
time_t cs_timegm(struct tm *tm)
{
	time_t result = 0;
	int32_t i;
	if(tm->tm_mon > 12 || tm->tm_mon < 0 || tm->tm_mday > 31 || tm->tm_min > 60 || tm->tm_sec > 60 || tm->tm_hour > 24)
		{ return 0; }
	for(i = 70; i < tm->tm_year; ++i)
	{
		result += is_leap(i + 1900) ? 366 : 365;
	}
	for(i = 0; i < tm->tm_mon; ++i)
	{
		if(i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) { result += 31; }
		else if(i == 3 || i == 5 || i == 8 || i == 10) { result += 30; }
		else if(is_leap(tm->tm_year + 1900)) { result += 29; }
		else { result += 28; }
	}
	result += tm->tm_mday - 1;
	result *= 24;
	result += tm->tm_hour;
	result *= 60;
	result += tm->tm_min;
	result *= 60;
	result += tm->tm_sec;
	return result;
}
Exemple #4
0
static time_t rep_timegm(struct tm *tm)
{
    static const unsigned int ndays[2][12] = {
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    time_t res = 0;
    unsigned int i;

    if (tm->tm_mon > 12 ||
            tm->tm_mon < 0 ||
            tm->tm_mday > 31 ||
            tm->tm_min > 60 ||
            tm->tm_sec > 60 ||
            tm->tm_hour > 24) {
        /* invalid tm structure */
        return 0;
    }

    for (i = 70; i < tm->tm_year; ++i)
        res += is_leap(i) ? 366 : 365;

    for (i = 0; i < tm->tm_mon; ++i)
        res += ndays[is_leap(tm->tm_year)][i];
    res += tm->tm_mday - 1;
    res *= 24;
    res += tm->tm_hour;
    res *= 60;
    res += tm->tm_min;
    res *= 60;
    res += tm->tm_sec;
    return res;
}
Exemple #5
0
static int week_num(const struct tm *tm)
{
	int val = (tm->tm_yday + 7 - (tm->tm_wday+6)%7) / 7;
	/* If 1 Jan is just 1-3 days past Monday,
	 * the previous week is also in this year. */
	if ((tm->tm_wday - tm->tm_yday - 2 + 371) % 7 <= 2)
		val++;
	if (!val) {
		val = 52;
		/* If 31 December of prev year a Thursday,
		 * or Friday of a leap year, then the
		 * prev year has 53 weeks. */
		int dec31 = (tm->tm_wday - tm->tm_yday - 1 + 7) % 7;
		if (dec31 == 4 || (dec31 == 5 && is_leap(tm->tm_year%400-1)))
			val++;
	} else if (val == 53) {
		/* If 1 January is not a Thursday, and not
		 * a Wednesday of a leap year, then this
		 * year has only 52 weeks. */
		int jan1 = (tm->tm_wday - tm->tm_yday + 371) % 7;
		if (jan1 != 4 && (jan1 != 3 || !is_leap(tm->tm_year)))
			val = 1;
	}
	return val;
}
Exemple #6
0
isc_result_t
dns_time64_fromtext(const char *source, isc_int64_t *target) {
	int year, month, day, hour, minute, second;
	isc_int64_t value;
	int secs;
	int i;

#define RANGE(min, max, value) \
	do { \
		if (value < (min) || value > (max)) \
			return (ISC_R_RANGE); \
	} while (0)

	if (strlen(source) != 14U)
		return (DNS_R_SYNTAX);
	/*
	 * Confirm the source only consists digits.  sscanf() allows some
	 * minor exceptions.
	 */
	for (i = 0; i < 14; i++) {
		if (!isdigit((unsigned char)source[i]))
			return (DNS_R_SYNTAX);
	}
	if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
		   &year, &month, &day, &hour, &minute, &second) != 6)
		return (DNS_R_SYNTAX);

	RANGE(0, 9999, year);
	RANGE(1, 12, month);
	RANGE(1, days[month - 1] +
		 ((month == 2 && is_leap(year)) ? 1 : 0), day);
	RANGE(0, 23, hour);
	RANGE(0, 59, minute);
	RANGE(0, 60, second);		/* 60 == leap second. */

	/*
	 * Calculate seconds from epoch.
	 * Note: this uses a idealized calendar.
	 */
	value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
	for (i = 0; i < (month - 1); i++)
		value += days[i] * 86400;
	if (is_leap(year) && month > 2)
		value += 86400;
	if (year < 1970) {
		for (i = 1969; i >= year; i--) {
			secs = (is_leap(i) ? 366 : 365) * 86400;
			value -= secs;
		}
	} else {
		for (i = 1970; i < year; i++) {
			secs = (is_leap(i) ? 366 : 365) * 86400;
			value += secs;
		}
	}

	*target = value;
	return (ISC_R_SUCCESS);
}
bool is_valid_date(date a)
{
    if(a.year<=0) return(false);
    if(a.month<=0||a.month>=13) return(false);
    if(!is_leap(a.year)&&(a.day<=0||a.day>month_day[a.month])) return(false);
    if(is_leap(a.year)&&(a.day<=0||a.day>leap_month_day[a.month])) return(false);
    return(true);
}
Exemple #8
0
void demo05070_1() {
	int year = 2000;
	int month = 8;
	int day = 8;
	int yearday = day_of_year(year, month, day);
	printf("%d年%d月%d日是当年的第%d天    %d\n", year, month, day, yearday, is_leap(year));
	year = 2014;
	yearday = day_of_year(year, month, day);
	printf("%d年%d月%d日是当年的第%d天    %d\n", year, month, day, yearday, is_leap(year));
}
int
date2day(int year, int month, int day)
{
    const int daytab[2][13] = {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    int days = 0;
    int isleap;
    int i;

    // restrict parms
    if (year < 1 || year > 9999)
        return -1;
    if (month < 1 || month > 12 )
        return -1;
    isleap = is_leap(year);
    if (day < 1 || day > daytab[isleap][month])
        return -1;

    for(i = 1; i < month; i++)
        days += daytab[isleap][i];

    return days + day;
}
Exemple #10
0
/* day_of_year: set day of year from month & day */
int day_of_year(int year, int month, int day) {
	int i, leap;
	leap = is_leap(year);
	for (i = 1; i < month; i++)
		day += daytab[leap][i];
	return day;
}
Exemple #11
0
static void show_cal(int year, int mon, int week_start)
{
	static char *head = "Su Mo Tu We Th Fr Sa\n";
	int	i, wrap, day;

	switch (mon) {
	case 1: case 3: case 5: case 7: case 8: case 10: case 12:
		day = 31;
		break;
	case 4: case 6: case 9: case 11:
		day = 30;
		break;
	case 2:
		day = (is_leap(year) ? 29 : 28);
		break;
	default:
		fputs("month illegal\n", stderr);
		return;
	}

	printf("%s", head);
	wrap = 0;
	for (i = 0; i < week_start; i++) {
		printf("   ");
		wrap++;
	}
	for (i = 1; i <= day; i++) {
		printf("%-3d", i);
		if (++wrap % 7 == 0)
			putchar('\n');
	}
	putchar('\n');
}
Exemple #12
0
/* It's almost the same as mktime(),
** excepted for the following assumptions:
**    - it assumes to handle only UTC/GMT times,
**      thus ignoring time zone and daylight saving time;
**    - field values must be right (within the expected ranges).
*/
static time_t
cvt_tm2time( struct tm *ptm )
{
	time_t t;
	int tm_year = ptm->tm_year + 1900;

	/* Years since epoch, converted to days. */
	t = ( ptm->tm_year - 70 ) * 365;

	/* Leap days for previous years. */
	t += ( ptm->tm_year - 69 ) / 4;

	/* Days for the beginning of this month. */
	t += month_ydays_tab[ptm->tm_mon];

	/* Leap day for this year. */
	if ( ptm->tm_mon >= 2 && is_leap( tm_year ) )
		++t;

	/* Days since the beginning of this month. */
	t += ptm->tm_mday - 1;	/* 1-based field */

	/* Hours, minutes, and seconds. */
	t = t * 24 + ptm->tm_hour;
	t = t * 60 + ptm->tm_min;
	t = t * 60 + ptm->tm_sec;

	return t;
}
Exemple #13
0
inline
constexpr
unsigned
last_day_of_month(Int y, unsigned m) noexcept
{
    return m != 2 || !is_leap(y) ? last_day_of_month_common_year(m) : 29u;
}
Exemple #14
0
static  void
rtc_set_time(int *idx)
{
        uint8_t cmdbuf[5], obuf[3];
	ucell second, minute, hour, day, month, year;
	const int *days;
	uint32_t now;
	unsigned int nb_days;
	int i;

	year = POP();
	month = POP();
	day = POP();
	hour = POP();
	minute = POP();
	second = POP();

	days = is_leap(year) ?  days_month_leap : days_month;
	nb_days = (year - 1904) * 36525 / 100 + day;
	for (i = 0; i < month - 1; i++)
		nb_days += days[i];

	now = (((nb_days * 24) + hour) * 60 + minute) * 60 + second;

        cmdbuf[0] = CUDA_SET_TIME;
	cmdbuf[1] = now >> 24;
	cmdbuf[2] = now >> 16;
	cmdbuf[3] = now >> 8;
	cmdbuf[4] = now;

        cuda_request(main_cuda, CUDA_PACKET, cmdbuf, sizeof(cmdbuf), obuf);
}
Exemple #15
0
int days_in_year(int year) {
    if (is_leap(year)) {
        return DAYS_PER_L_YEAR;
    }

    return DAYS_PER_N_YEAR;
}
Exemple #16
0
int main(int argc, char *argv[])
{
    unsigned int lookup[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    unsigned int year = 1900,
                 month = 0,
                 days = 0,
                 first = 0,
                 sundays = 0;

    while (year < 2001) {
        days = lookup[month];
        if (month == 1 && is_leap(year)) {
            days++;
        }
        first = (first + days) % 7;
        if (year > 1900 && first == 6) {
            sundays++;
        }
        month++;
        if (month > 11) {
            year++;
            month = 0;
        }
    }
    printf("#sundays: %d\n", sundays);
}
Exemple #17
0
static struct tm * __cdecl common_localtime(const time_t *t, BOOL bLocal)
{
    wxLongLong i64;
    FILETIME   ft;
    wxString str ;
    SYSTEMTIME SystemTime;
    TIME_ZONE_INFORMATION pTz;
    static struct tm st_res ;             // data holder
    static struct tm * res = &st_res ;    // data pointer
    int iLeap;
    const unsigned short int __mon_yday[2][13] =
    {
        // Normal years
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
        // Leap years
        { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
    };

    if (!*t)
        ::GetLocalTime(&SystemTime);
    else
    {
        i64 = *t;
        i64 = i64 * 10000000 + 116444736000000000;

        ft.dwLowDateTime  = i64.GetLo();
        ft.dwHighDateTime = i64.GetHi();

        ::FileTimeToSystemTime(&ft, &SystemTime);
    }

    ::GetTimeZoneInformation(&pTz);

    ///////////////////////////////////////////////
    // Set timezone
    timezone = pTz.Bias * SECONDS_IN_ONE_MINUTE ;
    ///////////////////////////////////////////////

    iLeap = is_leap(SystemTime.wYear) ;

    res->tm_hour = SystemTime.wHour;
    res->tm_min  = SystemTime.wMinute;
    res->tm_sec  = SystemTime.wSecond;

    res->tm_mday = SystemTime.wDay;
    res->tm_mon = SystemTime.wMonth - 1; // this the correct month but localtime returns month aligned to zero
    res->tm_year = SystemTime.wYear;     // this the correct year
    res->tm_year = res->tm_year - 1900;  // but localtime returns the value starting at the 1900

    res->tm_wday = SystemTime.wDayOfWeek;
    res->tm_yday = __mon_yday[iLeap][res->tm_mon] + SystemTime.wDay - 1; // localtime returns year-day aligned to zero

    // if localtime behavior and daylight saving
    if (bLocal && pTz.DaylightBias != 0)
        res->tm_isdst = 1;
    else
        res->tm_isdst = 0; // without daylight saving or gmtime

    return res;
}
Exemple #18
0
time_t::time_t(clock_t c, uint8_t zone)
{
  UNUSED(zone);
  uint16_t dayno = c / SECONDS_PER_DAY;
  day = (dayno % DAYS_PER_WEEK) + 1;
  year = 0;
  while (1) {
    uint16_t days = days_per(year);
    if (dayno < days) break;
    dayno -= days;
    year += 1;
  }
  year %= 100;
  month = 1;
  while (1) {
    uint8_t days = pgm_read_byte(&days_in[month]);
    if (is_leap(year) && (month == 2)) days += 1;
    if (dayno < days) break;
    dayno -= days;
    month += 1;
  }
  date = dayno + 1;
  hours = (c % SECONDS_PER_DAY) / SECONDS_PER_HOUR;
  minutes = (c % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE;
  seconds = (c % SECONDS_PER_MINUTE);
  to_bcd();
}
Exemple #19
0
static int validate_date_time(const struct tm *tm)
{
	if (tm->tm_sec > 59)
		return -1;
	if (tm->tm_min > 59)
		return -1;
	if (tm->tm_hour > 23)
		return -1;
	if (((tm->tm_year + 1900) < 1970) || ((tm->tm_year + 1900) > 2111))
		return -1;
	if (tm->tm_mon >= 12)
		return -1;
	if (tm->tm_mon == 1) {
		if (!is_leap(tm->tm_year + 1900)) {
			if (tm->tm_mday > 28)
				return -1;
		} else  {
			if (tm->tm_mday > 29)
				return -1;
		}
	}
	switch (tm->tm_mon) {
		case 3:
		case 5:
		case 8:
		case 10:
			if(tm->tm_mday > 30)
				return -1;
	}

	if ( (tm->tm_mday < 1) || (tm->tm_mday > 31) )
		return -1;

   	return 0;
}
Exemple #20
0
/* Drop-in replacement for gmtime_r as some plattforms strip the function from their libc. */
struct tm *cs_gmtime_r(const time_t *timep, struct tm *r)
{
	static const int16_t daysPerMonth[13] = { 0,
											31,
											31 + 28,
											31 + 28 + 31,
											31 + 28 + 31 + 30,
											31 + 28 + 31 + 30 + 31,
											31 + 28 + 31 + 30 + 31 + 30,
											31 + 28 + 31 + 30 + 31 + 30 + 31,
											31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,
											31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,
											31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,
											31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,
											31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31
											};
	time_t i;
	time_t work = * timep % 86400;
	r->tm_sec = work % 60;
	work /= 60;
	r->tm_min = work % 60;
	r->tm_hour = work / 60;
	work = * timep / 86400;
	r->tm_wday = (4 + work) % 7;
	for(i = 1970; ; ++i)
	{
		time_t k = is_leap(i) ? 366 : 365;
		if(work >= k)
			{ work -= k; }
		else
			{ break; }
	}
	r->tm_year = i - 1900;
	r->tm_yday = work;
	r->tm_mday = 1;
	if(is_leap(i) && work > 58)
	{
		if(work == 59)
			{ r->tm_mday = 2; } /* 29.2. */
		work -= 1;
	}
	for(i = 11; i && daysPerMonth[i] > work; --i)
		{ ; }
	r->tm_mon   = i;
	r->tm_mday += work - daysPerMonth[i];
	return r;
}
int main(void)
{
	int n;
	printf("input year:\n");
	scanf("%d", &n);
	is_leap(n);
	return 0;
}
Exemple #22
0
/*
 * days_in_month:
 * Calculates the number of days in a month.
 * Input parameters:
 *     Calendar style (JULIAN or GREGORIAN)
 *     Year (must be >0)
 *     Month (1..12)
 * Returns:
 *     The number of days in the month (28..31)
 */
int days_in_month(int style, int year, int month)
{
    static int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    return month==2 && is_leap(style,year)
             ? 29
             : days[month-1];
}
Exemple #23
0
struct tm *gmtime_r(const time_t *tm, struct tm *result)
{
	int leapyr = 0;
	time_t ltm = *tm;

	memset(result, 0, sizeof(struct tm));
	result->tm_year = 1970;

	while(1) {
		if(ltm < SEC_PER_YR[is_leap(result->tm_year)]) {
			break;
		}
		ltm -= SEC_PER_YR[is_leap(result->tm_year)];
		result->tm_year++;
	}

	leapyr = is_leap(result->tm_year);

	while(1) {
		if(ltm < SEC_PER_MT[leapyr][result->tm_mon])
			break;
		ltm -= SEC_PER_MT[leapyr][result->tm_mon];
		result->tm_mon++;
	}

	result->tm_mday = ltm / SEC_PER_DY;
	result->tm_mday++;
	ltm = ltm % SEC_PER_DY;

	result->tm_hour = ltm / SEC_PER_HR;
	ltm = ltm % SEC_PER_HR;

	result->tm_min = ltm / 60;
	result->tm_sec = ltm % 60;

	result->tm_wday = day_of_week_get(result->tm_mon + 1, result->tm_mday, result->tm_year);

	/*
	 * Solve bug WMSDK-27. 'man gmtime' says:
	 * "tm_year   The number of years since 1900."
	 */
	result->tm_year -= 1900;

	return result;
}
Exemple #24
0
/* month_day: set month, day from day of year */
void month_day(int year, int yearday, int *pmonth, int *pday)
{
	int i, leap;
	leap = is_leap(year);
	for (i = 1; yearday > daytab[leap][i]; i++)
		yearday -= daytab[leap][i];
	*pmonth = i;
	*pday = yearday;
}
	static int
app_datacal_currday_to_totaldays(int day, int month, int year)
{
	int current_year;
	int total_days = 0;

	/*Calculting how many days there are between supplied year and base*/
	for(current_year = TIME_YEAR_BASE; current_year < year; current_year++ )
		total_days += total_day_in_month[is_leap(current_year)][12];

	/*Calculating how many days there are between starting of current year and current month*/
	total_days += total_day_in_month[is_leap(current_year)][month-1];

	total_days += day;

	return total_days;

}
static inline int days_from_1jan(int year, int month, int day)
{
    static const int days[2][12] =
    {
        { 0,31,59,90,120,151,181,212,243,273,304,334},
        { 0,31,60,91,121,152,182,213,244,274,305,335}
    };
    return days[is_leap(year)][month-1] + day - 1;
}
Exemple #27
0
int days_of_month(int m, int y) {
	int days[2][12] = {31,28,31,30,31,30,30,31,30,31,30,31,
					   31,28,31,30,31,30,30,31,30,31,30,31};
	
	if (m <= 0 || m > 12)
		return 0;

	return days[is_leap(y)][m - 1];	
}
isc_result_t
dns_time64_fromtext(const char *source, isc_int64_t *target) {
	int year, month, day, hour, minute, second;
	isc_int64_t value;
	int secs;
	int i;

#define RANGE(min, max, value) \
	do { \
		if (value < (min) || value > (max)) \
			return (ISC_R_RANGE); \
	} while (0)

	if (strlen(source) != 14U)
		return (DNS_R_SYNTAX);
	if (sscanf(source, "%4d%2d%2d%2d%2d%2d",
		   &year, &month, &day, &hour, &minute, &second) != 6)
		return (DNS_R_SYNTAX);

	RANGE(1970, 9999, year);
	RANGE(1, 12, month);
	RANGE(1, days[month - 1] +
		 ((month == 2 && is_leap(year)) ? 1 : 0), day);
	RANGE(0, 23, hour);
	RANGE(0, 59, minute);
	RANGE(0, 60, second);		/* 60 == leap second. */

	/*
	 * Calulate seconds since epoch.
	 */
	value = second + (60 * minute) + (3600 * hour) + ((day - 1) * 86400);
	for (i = 0; i < (month - 1); i++)
		value += days[i] * 86400;
	if (is_leap(year) && month > 2)
		value += 86400;
	for (i = 1970; i < year; i++) {
		secs = (is_leap(i) ? 366 : 365) * 86400;
		value += secs;
	}

	*target = value;
	return (ISC_R_SUCCESS);
}
Exemple #29
0
time_t::operator clock_t()
{
  clock_t res;
  to_binary();
  res = year * (SECONDS_PER_DAY * 365);
  for (uint8_t y = 0; y < year; y++) 
    if (is_leap(y)) 
      res +=  SECONDS_PER_DAY;
  for (uint8_t m = 1; m < month; m++) {
    uint8_t days = pgm_read_byte(&days_in[m]);
    if (is_leap(year) && (m == 2)) days += 1;
    res += SECONDS_PER_DAY * days;
  }
  res += (date - 1) * SECONDS_PER_DAY;
  res += hours * SECONDS_PER_HOUR;
  res += minutes * SECONDS_PER_MINUTE;
  res += seconds;
  return (res);
}
Exemple #30
0
/* year, month -> number of days in that month in that year */
static int
days_in_month(int year, int month)
{
    assert(month >= 1);
    assert(month <= 12);
    if (month == 2 && is_leap(year))
        return 29;
    else
        return _days_in_month[month];
}