All pastes #2127594 Raw Edit

Someone

public c v1 · immutable
#2127594 ·published 2012-03-13 08:56 UTC
rendered paste body
/* * days_in_month(month, year) * Returns the number of days in a given month and year, taking into account leap years. * * month: numeric month (1-12) * year: numeric year * * Prec: month is an integer between 1 and 12, inclusive, and year is an integer. * Post: none */// corrected by ben at sparkyb dot netint days_in_month(int month, int year) {	// calculate number of days in a month	return month == 2 ? (year % 4 ? 28 : (year % 100 ? 29 : (year % 400 ? 28 : 29))) : ((month - 1) % 7 % 2 ? 30 : 31);}int days_in_year(int year){	int i, seconds = 0;	for(i=1; i<=12; i++){		 seconds += days_in_month(i, year);	}	return seconds;}struct datetime {     // Declare structure TimeStruct        int     sec;        // seconds              00 to 59        int     min;        // minutes              00 to 59        int     hour;       // hours                00 to 23        int     mday;       // day of the month     1 to 31        int     mon;        // month                1 to 12        int     year;       // year                 1970 to 2106};/* * Convert seconds to datetime struct */void seconds_to_datetime(int seconds, struct datetime *d) {	printf("\r\nSeconds remaining: %i\r\n", seconds);	d->year = EPOCH_YEAR;	d->mon = 0;	d->mday = 0;	d->hour = 0;	d->min = 0;	d->sec = 0;		// Run as long as we still have seconds left for a full year or leap year	while(seconds >= 60*60*24*365 || seconds >= 60*60*24*366){		if(d->year%400 ==0 || (d->year%100 != 0 && d->year%4 == 0)){			seconds -= 60*60*24*366;		} else {			seconds -= 60*60*24*365;		}		d->year++;	}	// Run as long as we still have seconds left for a month	while(seconds >= 60*60*24*30 || seconds >= 60*60*24*31 || seconds >= 60*60*24*28 || seconds >= 60*60*24*29){			seconds -= 60*60*24*days_in_month(d->mon, d->year);			d->mon++;	}	// Run as long as we still have seconds left for a day	while(seconds >= 60*60*24){			seconds -= 60*60*24;																		d->mday++;	}	// Run as long as we still have seconds left for an hour	while(seconds >= 60*60){				seconds -= 60*60;																		d->hour++;	}	// Run as long as we still have seconds left for a minute	while(seconds >= 60){				seconds -= 60;																		d->hour++;	}		// Just add reaming seconds	d->sec = seconds;						 }/* * Convert a datetime struct to seconds */int datetime_to_seconds(struct datetime *d){	int year_i, mon_i, mday_i, mhour_i, mmin_i, seconds;	// Convert time struct to seconds since 1970	for(year_i = EPOCH_YEAR; year_i<=d->year; year_i++)	{		// All years upto current		if(year_i<d->year){			//Calc number of days in year			int days = days_in_year(year_i);						seconds += 60*60*24*days;		}		// Current year		if(year_i == d->year){			// Calculate seconds in past months in current year			for(mon_i = 1; mon_i<=d->mon; mon_i++){				//Calc number of days in year				int days = days_in_month(mon_i, year_i);				seconds += 60*60*24*days;			}			// Calculate seconds in past days in current month			for(mday_i = 1; mday_i<=d->mday; mday_i++){				seconds += 60*60*24;			}			// Calculate seconds in past hours in current day			for(mhour_i = 0; mhour_i<=d->hour; mhour_i++){				seconds += 60*60*mhour_i;			}			// Calculate seconds in past min in current day			for(mmin_i = 0; mmin_i<=d->min; mmin_i++){				 seconds += 60*mmin_i;			}			// Calculate seconds in past seocnds in current minute			seconds += d->sec;		}	 }	return seconds;}