diff --git a/du3/program b/du3/program index ed77f5f..e021d85 100755 Binary files a/du3/program and b/du3/program differ diff --git a/du3/program.c b/du3/program.c index a3e22ef..c6ffa0c 100644 --- a/du3/program.c +++ b/du3/program.c @@ -5,32 +5,84 @@ #include #include +#define max_yr 9999 +#define min_yr 1900 + +int isleap(int y) { + if (y % 4 != 0) { + return 0; + } else if (y % 100 != 0) { + return 1; + } else if (y % 400 != 0) { + return 0; + } + + return 1; +} + +int datevalid(int d, int m, int y) { + if(y < min_yr || y > max_yr) { + return 0; + } + + if(m < 1 || m > 12) { + return 0; + } + + if(d < 1 || d > 31) { + return 0; + } + + if(m == 2) { + if(isleap(y)) { + if(d <= 29) { + return 1; + } + } else { + return 0; + } + } + + if ( m == 4 || m == 6 || m == 9 || m == 11 ) { + if(d <= 30) { + return 1; + } else { + return 0; + } + } + + return 1; +} + int main() { - char buffer[20]; - struct tm tm = {0}; + char buffer[20]; + struct tm tm = {0}; - char *retval = fgets(buffer, sizeof(buffer), stdin); + char *retval = fgets(buffer, sizeof(buffer), stdin); - if (retval == 0) { - puts("Neplatny datum"); - return 0; - } + if (retval == 0) { + puts("Neplatny datum"); + return 0; + } - retval = strptime(buffer, "%e.%m.%-Y", &tm); + retval = strptime(buffer, "%e.%m.%-Y", &tm); - if (retval == 0) { - puts("Neplatny datum"); - return 0; - } + if (retval == 0 || datevalid(tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900) == 0) { + puts("Neplatny datum"); + return 0; + } - tm.tm_isdst = -1; - tm.tm_mday += 7; + tm.tm_isdst = -1; + tm.tm_mday += 7; - mktime(&tm); - - strftime(buffer, sizeof(buffer), "%e.%m.%-Y", &tm); - puts(buffer); - putc('\n', stdout); - - return 0; -} \ No newline at end of file + if (mktime(&tm) == -1) { + puts("Neplatny datum"); + return 0; + } + + strftime(buffer, sizeof(buffer), "%e.%m.%-Y", &tm); + puts(buffer); + putc('\n', stdout); + + return 0; +}