Exemplo n.º 1
0
		monthOfDate(dat, &mad);
		SHOULD_EQUAL(mad.month, 10)
		SHOULD_EQUAL(mad.day, 15)
		SHOULD_EQUAL(mad.dayofyear, 288)
		dat = 34658909;
		monthOfDate(dat, &mad);
		SHOULD_EQUAL(mad.month, 2)
		SHOULD_EQUAL(mad.day, 6)
		SHOULD_EQUAL(mad.dayofyear, 37)
		freeGlobals();
	END_IT
END_DESCRIBE

DESCRIBE(leapYear, "int leapYear (long year)")
	IT("Determines whether or not the given year is a leap year")
		SHOULD_EQUAL(leapYear(1970), 0)
		SHOULD_EQUAL(leapYear(2000), 1)
		SHOULD_EQUAL(leapYear(2200), 0)
		SHOULD_EQUAL(leapYear(2012), 1)
	END_IT
END_DESCRIBE

int main () {
	CSpec_Run(DESCRIPTION(printDate), CSpec_NewOutputUnit());
	CSpec_Run(DESCRIPTION(yearOfDate), CSpec_NewOutputUnit());
	CSpec_Run(DESCRIPTION(monthOfDate), CSpec_NewOutputUnit());
	CSpec_Run(DESCRIPTION(leapYear), CSpec_NewOutputUnit());
	
	return 0;
}
Exemplo n.º 2
0
int QDate::daysInYear() const
{
    int y, m, d;
    jul2greg( jd, y, m, d );
    return leapYear(y) ? 366 : 365;
}
Exemplo n.º 3
0
static void validateTime(struct tm *tp, struct tm *defaults)
{
    struct tm   empty;

    /*
        Fix apparent day-mon-year ordering issues. Cannot fix everything!
     */
    if ((12 <= tp->tm_mon && tp->tm_mon <= 31) && 0 <= tp->tm_mday && tp->tm_mday <= 11) {
        /*
            Looks like day month are swapped
         */
        swapDayMonth(tp);
    }

    if (tp->tm_year != -MAXINT && tp->tm_mon >= 0 && tp->tm_mday >= 0 && tp->tm_hour >= 0) {
        /*  Everything defined */
        return;
    }

    /*
        Use empty time if missing
     */
    if (defaults == NULL) {
        memset(&empty, 0, sizeof(empty));
        defaults = &empty;
        empty.tm_mday = 1;
        empty.tm_year = 70;
    }
    if (tp->tm_hour < 0 && tp->tm_min < 0 && tp->tm_sec < 0) {
        tp->tm_hour = defaults->tm_hour;
        tp->tm_min = defaults->tm_min;
        tp->tm_sec = defaults->tm_sec;
    }

    /*
        Get weekday, if before today then make next week
     */
    if (tp->tm_wday >= 0 && tp->tm_year == -MAXINT && tp->tm_mon < 0 && tp->tm_mday < 0) {
        tp->tm_mday = defaults->tm_mday + (tp->tm_wday - defaults->tm_wday + 7) % 7;
        tp->tm_mon = defaults->tm_mon;
        tp->tm_year = defaults->tm_year;
    }

    /*
        Get month, if before this month then make next year
     */
    if (tp->tm_mon >= 0 && tp->tm_mon <= 11 && tp->tm_mday < 0) {
        if (tp->tm_year == -MAXINT) {
            tp->tm_year = defaults->tm_year + (((tp->tm_mon - defaults->tm_mon) < 0) ? 1 : 0);
        }
        tp->tm_mday = defaults->tm_mday;
    }

    /*
        Get date, if before current time then make tomorrow
     */
    if (tp->tm_hour >= 0 && tp->tm_year == -MAXINT && tp->tm_mon < 0 && tp->tm_mday < 0) {
        tp->tm_mday = defaults->tm_mday + ((tp->tm_hour - defaults->tm_hour) < 0 ? 1 : 0);
        tp->tm_mon = defaults->tm_mon;
        tp->tm_year = defaults->tm_year;
    }
    if (tp->tm_year == -MAXINT) {
        tp->tm_year = defaults->tm_year;
    }
    if (tp->tm_mon < 0) {
        tp->tm_mon = defaults->tm_mon;
    }
    if (tp->tm_mday < 0) {
        tp->tm_mday = defaults->tm_mday;
    }
    if (tp->tm_yday < 0) {
        tp->tm_yday = (leapYear(tp->tm_year + 1900) ?
            leapMonthStart[tp->tm_mon] : normalMonthStart[tp->tm_mon]) + tp->tm_mday - 1;
    }
    if (tp->tm_hour < 0) {
        tp->tm_hour = defaults->tm_hour;
    }
    if (tp->tm_min < 0) {
        tp->tm_min = defaults->tm_min;
    }
    if (tp->tm_sec < 0) {
        tp->tm_sec = defaults->tm_sec;
    }
}
Exemplo n.º 4
0
int main (int argc, char *argv[])
{
	int date;
	char input[10];
	char mon[10];
	int monidx;
	int year;
	int days;
	int isleap;
	int total;

	// obtain a year
	printf("Enter the year(empty line to quit): ");

	while(fgets(input, 10, stdin) != NULL && input[0] != '\n') {

		if (!isdigit(input[0])) {
			printf("%s is not a valid year. Enter the year again(empty line to quit): ", input);
			continue;
		}
		year = atoi(input);

		// check if that year is a leap year
		isleap = leapYear(year);
		

		// obtain a valid month
		printf ("Enter the month (number, name or abbreviation): ");
		
		int f;
		while(fgets(mon, 10, stdin) == NULL || input[0] == '\n' || !(validMonth(monidx = standard(mon)))) {
			printf("Not a valid input, enter the month (number, name or abbreviation): ");
			// printf("%d\n",f);
		}
		// printf("%d\n",f);

		// transfer the input to month idx
		// monidx = standard(mon);
		
		// obtain a valid date
		// days = obtainDays(monidx);
		printf("Enter the date (1-%d): ",obtainDays(monidx));
		while(scanf("%d", &date) <= 0) {
			if (isleap && monidx == 2) {
				printf("Not a valid date, please input a valid date(1-29): ");
			}
			printf("Not a valid date, please input a valid date(1-%d): ", obtainDays(monidx));
		}

		// calculate the days
		total = calTotal(monidx, date, isleap);

		// print the results
		if (total) {
			int lastDigit = date % 10;
			if (lastDigit == 1) {
				printf("There are %d day(s) through %s %dst from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else if(lastDigit == 2) {
				printf("There are %d day(s) through %s %dnd from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else if (lastDigit == 3) {
				printf("There are %d day(s) through %s %drd from begining of %d.\n", total, months[monidx].name, date, year);
			}
			else
				printf("There are %d day(s) through %s %dth from begining of %d.\n", total, months[monidx].name, date, year);
		}
		else
			printf("Oops, I ate too much today.\n");

		while(getchar() != '\n')
			continue;
		// Do it again
		printf("Enter the year(empty line to quit): ");
	}
	return 0;
}
Exemplo n.º 5
0
/*! Creates a new date by parsing the given string
    @param str      the string to parse into a date
    @return         the new date or NIL if the date parsing failed
*/
date newDate (string* str) {
	char* sdate = str->content;
    if (strcmp(sdate, "now") == 0) {
        return time(NULL);
    } else {
    	date dat = 0;
        int size = str->size;
        while (size > 0 && sdate[size - 1] == ' ') {
            --size;
        }
        int pos = 0;
        while (size > pos && sdate[pos] == ' ') {
            ++pos;
        }
        int valid = 1;
        tm timestruct;
        memset(&timestruct, 0, sizeof(timestruct));
        if (size > pos + 2 && isNumber(sdate[pos])) {
            if (isNumber(sdate[pos + 1]) && sdate[pos + 2] == '/') {
                timestruct.tm_mon = (sdate[pos] - '0') * 10 + (sdate[pos + 1] - '0') - 1;
                pos += 3;
            } else if (sdate[pos + 1] == '/') {
                timestruct.tm_mon = (sdate[pos] - '0') - 1;
                pos += 2;
            } else {
                valid = 0;
            }
            if (size > pos && isNumber(sdate[pos])) {
            	if (size == pos + 1 || (size > pos + 1 && sdate[pos + 1] == '/')) {
                    timestruct.tm_mday = (sdate[pos] - '0');
                    pos += 2;
                } else if (isNumber(sdate[pos + 1]) && (size == pos + 2 || (size > pos + 2 && (sdate[pos + 2] == '/' || sdate[pos + 2] == ' ')))) {
                    timestruct.tm_mday = (sdate[pos] - '0') * 10 + (sdate[pos + 1] - '0');
                    pos += 3;
                } else {
                    valid = 0;
                }
                int yearchars = 0;
                while (size > pos + yearchars && isNumber(sdate[pos + yearchars])) {
                    ++yearchars;
                }
                if (yearchars == 2 || yearchars == 4) {
                    int i;
                    static int powers[4] = {1, 10, 100, 1000};
                    for (i = 0; i < yearchars; ++i) {
                        timestruct.tm_year += (sdate[pos + yearchars - i - 1] - '0') * powers[i];
                    }
                    if (yearchars == 2) {
                        if (timestruct.tm_year > CURRENT_YEAR) {
                            timestruct.tm_year -= 70;
                        } else {
                            timestruct.tm_year += 30;
                        }
                    } else {
                        timestruct.tm_year -= YEAR_ZERO;
                    }
                    pos += yearchars;
                    i = 0;
                    while (size > pos + i && sdate[pos + i] == ' ') {
                        ++i;
                    }
                    if (i > 0) {
                        pos += i;
                        if (size > pos && isNumber(sdate[pos])) {
                            if (size > pos + 2 && isNumber(sdate[pos + 1]) && sdate[pos + 2] == ':') {
                                timestruct.tm_hour = (sdate[pos] - '0') * 10 + (sdate[pos + 1] - '0');
                                pos += 3;
                            } else if (size > pos + 1 && sdate[pos + 1] == ':') {
                                timestruct.tm_hour = sdate[pos] - '0';
                                pos += 2;
                            } else {
                                valid = 0;
                            }
                            if (size > pos && isNumber(sdate[pos])) {
                                if (size > pos + 1 && isNumber(sdate[pos + 1])) {
                                    timestruct.tm_min = (sdate[pos] - '0') * 10 + (sdate[pos + 1] - '0');
                                    pos += 2;
                                } else {
                                    timestruct.tm_min = sdate[pos] - '0';
                                    pos += 2;
                                }
                                if (sdate[pos] == ':') {
                                	++pos;
		                            if (size > pos && isNumber(sdate[pos])) {
		                                if (size > pos + 1 && isNumber(sdate[pos + 1])) {
		                                    timestruct.tm_sec = (sdate[pos] - '0') * 10 + (sdate[pos + 1] - '0');
		                                    pos += 2;
		                                } else {
		                                    timestruct.tm_sec = sdate[pos] - '0';
		                                    pos += 1;
		                                }
		                                while (pos < size && sdate[pos] == ' ') {
		                                    ++pos;
		                                }
		                            }
		                        } else {
		                            if (size > pos + 1) {
		                                if ((sdate[pos] == 'p' || sdate[pos] == 'P') && (sdate[pos + 1] == 'm' || sdate[pos + 1] == 'M')) {
		                                    timestruct.tm_hour += 12;
		                                } else if (!((sdate[pos] == 'a' || sdate[pos] == 'A') && (sdate[pos + 1] == 'm' || sdate[pos + 1] == 'M'))) {
		                                    valid = 0;
		                                }
		                                if (size != pos + 2) {
		                                    valid = 0;
		                                }
		                            } else {
		                                valid = size == pos;
		                            }
		                        }
                            } else {
                                valid = size == pos;
                            }
                        } else {
                            valid = size == pos;
                        }
                    }
                } else {
                    valid = yearchars == 0;
                }
            } else {
                valid = size == pos;
            }
        } else {
            valid = size == pos;
        }
        if (valid) {
            switch (timestruct.tm_mon) {
                case 0:
                case 2:
                case 4:
                case 6:
                case 7:
                case 9:
                case 11:
                    if (timestruct.tm_mday > 31) {
                        valid = 0;
                    }
                    break;
                case 1:
                    if (leapYear(timestruct.tm_year + YEAR_ZERO) && timestruct.tm_mday > 29) {
                        valid = 0;
                    } else if (timestruct.tm_mday > 28) {
                        valid = 0;
                    }
                    break;
                case 3:
                case 5:
                case 8:
                case 10:
                    if (timestruct.tm_mday > 30) {
                        valid = 0;
                    }
                    break;
                default:
                    valid = 0;
            }
            if (timestruct.tm_hour > HOUR_IN_DAY - 1 || timestruct.tm_min > MIN_IN_HOUR || timestruct.tm_sec > SEC_IN_MIN) {
                valid = 0;
            }
        }
        if (valid) {
            dat = timestruct.tm_sec + (timestruct.tm_min * SEC_IN_MIN) + (timestruct.tm_hour * SEC_IN_HOUR) + ((timestruct.tm_mday - 1) * SEC_IN_DAY);
            int month = timestruct.tm_mon - 1;
            int monthdays = 0;
            while (month >= 0) {
                switch (month) {
                    case 0:
                    case 2:
                    case 4:
                    case 6:
                    case 7:
                    case 9:
                    case 11:
                        monthdays += 31;
                        break;
                    case 1:
                        monthdays += 28;
                        break;
                    case 3:
                    case 5:
                    case 8:
                    case 10:
                        monthdays += 30;
                        break;
                }
                --month;
            }
            int year_sign;
            if (timestruct.tm_year >= 0) {
            	year_sign = 1;
            } else {
            	year_sign = -1;
            }
            dat += (monthdays + (timestruct.tm_year * DAYS_IN_YEAR) + ((timestruct.tm_year + year_sign * 2) / 4) - ((timestruct.tm_year + year_sign * 30) / 100) + ((timestruct.tm_year + year_sign * 330) / 400)) * SEC_IN_DAY;
        } else {
            return NIL;
        }
        return dat;
    }
}
Exemplo n.º 6
0
/** Days in a year are arbitrarily distributed but weighted to make the
  * average length of a year match the length of the solar year.
  * Interestingly the length of the year is measured in days, which is a duration
  * depending on Earth's angular velocity.
  */
inline int daysInYear(int y)
{
    return 31 * 7 + 30 * 4 + 28 + leapYear(y);
}
Exemplo n.º 7
0
int ExtDate::dayOfYear(int y, int m, int d)
{
	return m_monthOrigin[m-1] + d + ((m > 1) ? (leapYear(y) ? 1 : 0) : 0);
}
Exemplo n.º 8
0
int ExtDate::daysInYear() const
{
	if ( ! isValid() ) return 365;
	return (leapYear(year()) ? 366 : 365);
}