This commit is contained in:
Igor Dzuro 2020-03-27 04:54:58 +01:00
parent 708fc9ab3c
commit 3f743a0312
2 changed files with 74 additions and 22 deletions

Binary file not shown.

View File

@ -5,32 +5,84 @@
#include<time.h> #include<time.h>
#include<stdlib.h> #include<stdlib.h>
#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() { int main() {
char buffer[20]; char buffer[20];
struct tm tm = {0}; struct tm tm = {0};
char *retval = fgets(buffer, sizeof(buffer), stdin); char *retval = fgets(buffer, sizeof(buffer), stdin);
if (retval == 0) { if (retval == 0) {
puts("Neplatny datum"); puts("Neplatny datum");
return 0; return 0;
} }
retval = strptime(buffer, "%e.%m.%-Y", &tm); retval = strptime(buffer, "%e.%m.%-Y", &tm);
if (retval == 0) { if (retval == 0 || datevalid(tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900) == 0) {
puts("Neplatny datum"); puts("Neplatny datum");
return 0; return 0;
} }
tm.tm_isdst = -1; tm.tm_isdst = -1;
tm.tm_mday += 7; tm.tm_mday += 7;
mktime(&tm); if (mktime(&tm) == -1) {
puts("Neplatny datum");
strftime(buffer, sizeof(buffer), "%e.%m.%-Y", &tm); return 0;
puts(buffer); }
putc('\n', stdout);
strftime(buffer, sizeof(buffer), "%e.%m.%-Y", &tm);
return 0; puts(buffer);
} putc('\n', stdout);
return 0;
}