int main() { unsigned int numbers[MAX_TOKENS] = {0, 0, 0}; char date[11] = "", buf[MAX_INPUT_BUFFER + 1] = "", chunk[2] = ""; /* Reading stdin. */ while (read(0, chunk, 1) != 0) { if (*chunk == '\n' || *chunk == 0 || strlen(buf) == MAX_INPUT_BUFFER) break; strncat(buf, chunk, 1); } /* Parsing and processing the date. */ if (!parse_input(buf, numbers)) { printf("%s is illegal\n", buf); return 0; } if (!calc_date(numbers, date)) { printf("%s is illegal\n", buf); return 0; } puts(date); return 0; }
int main(void) { long year; long month; long day; long hour; long minute; double second; double julian; long yearCheck; long monthCheck; long dayCheck; long hourCheck; long minuteCheck; double secondCheck; // Test case 1: Noon, Jan 1, 4713 BCE, the reference date for Julian date. // Because there was no year zero 4713 BCE is year -4712. // These functions don't work properly for negative years, but at least check that the new code gets the same answer as the old code. assert(39.0 == gregorianToJulian(-4712, 1, 1, 12, 0, 0.0)); assert(39.0 == greg_2_jul( -4712, 1, 1, 12, 0, 0.0)); julianToGregorian(0.0, &year, &month, &day, &hour, &minute, &second); assert(-4712 == year && 0 == month && -1 == day && 12 == hour && 0 == minute && 0.0 == second); calc_date( 0.0, &year, &month, &day, &hour, &minute, &second); assert(-4712 == year && 0 == month && -1 == day && 12 == hour && 0 == minute && 0.0 == second); // Test case 2: Beginning midnight, Jan 1, 1, the reference date for Rata Die. assert(1721425.5 == gregorianToJulian(1, 1, 1, 0, 0, 0.0)); assert(1721425.5 == greg_2_jul( 1, 1, 1, 0, 0, 0.0)); julianToGregorian(1721425.5, &year, &month, &day, &hour, &minute, &second); assert(1 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); calc_date( 1721425.5, &year, &month, &day, &hour, &minute, &second); assert(1 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); // Test case 3: Beginning midnight, Oct 15, 1582, the reference date for Lillian date. assert(2299160.5 == gregorianToJulian(1582, 10, 15, 0, 0, 0.0)); assert(2299160.5 == greg_2_jul( 1582, 10, 15, 0, 0, 0.0)); julianToGregorian(2299160.5, &year, &month, &day, &hour, &minute, &second); assert(1582 == year && 10 == month && 15 == day && 0 == hour && 0 == minute && 0.0 == second); calc_date( 2299160.5, &year, &month, &day, &hour, &minute, &second); assert(1582 == year && 10 == month && 15 == day && 0 == hour && 0 == minute && 0.0 == second); // Test case 4: Beginning midnight, Jan 1, 1601, the reference date for ANSI date. assert(2305813.5 == gregorianToJulian(1601, 1, 1, 0, 0, 0.0)); assert(2305813.5 == greg_2_jul( 1601, 1, 1, 0, 0, 0.0)); julianToGregorian(2305813.5, &year, &month, &day, &hour, &minute, &second); assert(1601 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); calc_date( 2305813.5, &year, &month, &day, &hour, &minute, &second); assert(1601 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); // Test case 5: Noon, Nov 16, 1858, the reference date for reduced Julian date. assert(2400000.0 == gregorianToJulian(1858, 11, 16, 12, 0, 0.0)); assert(2400000.0 == greg_2_jul( 1858, 11, 16, 12, 0, 0.0)); julianToGregorian(2400000.0, &year, &month, &day, &hour, &minute, &second); assert(1858 == year && 11 == month && 16 == day && 12 == hour && 0 == minute && 0.0 == second); calc_date( 2400000.0, &year, &month, &day, &hour, &minute, &second); assert(1858 == year && 11 == month && 16 == day && 12 == hour && 0 == minute && 0.0 == second); // Test case 6: Noon, Dec 31, 1899, the reference date for Dublin Julian date. assert(2415020.0 == gregorianToJulian(1899, 12, 31, 12, 0, 0.0)); assert(2415020.0 == greg_2_jul( 1899, 12, 31, 12, 0, 0.0)); julianToGregorian(2415020.0, &year, &month, &day, &hour, &minute, &second); assert(1899 == year && 12 == month && 31 == day && 12 == hour && 0 == minute && 0.0 == second); calc_date( 2415020.0, &year, &month, &day, &hour, &minute, &second); assert(1899 == year && 12 == month && 31 == day && 12 == hour && 0 == minute && 0.0 == second); // Test case 7: Beginning midnight, Jan 1, 1970, the reference date for Unix time. assert(2440587.5 == gregorianToJulian(1970, 1, 1, 0, 0, 0.0)); assert(2440587.5 == greg_2_jul( 1970, 1, 1, 0, 0, 0.0)); julianToGregorian(2440587.5, &year, &month, &day, &hour, &minute, &second); assert(1970 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); calc_date( 2440587.5, &year, &month, &day, &hour, &minute, &second); assert(1970 == year && 1 == month && 1 == day && 0 == hour && 0 == minute && 0.0 == second); // Test case 8: Random dates between 1900 and 2100. Check old and new functions against each other. for (julian = 2415020.0; julian < 2488068.0; julian += (double)rand() / RAND_MAX) { julianToGregorian(julian, &year, &month, &day, &hour, &minute, &second); calc_date( julian, &yearCheck, &monthCheck, &dayCheck, &hourCheck, &minuteCheck, &secondCheck); assert(year == yearCheck && month == monthCheck && day == dayCheck && hour == hourCheck && minute == minuteCheck && second == secondCheck); assert(julian == gregorianToJulian(year, month, day, hour, minute, second)); assert(julian == greg_2_jul( year, month, day, hour, minute, second)); } return 0; }