pvjc20/du3/program.c

91 lines
1.4 KiB
C
Raw Normal View History

2020-03-27 02:39:01 +00:00
#define __USE_XOPEN
#define _GNU_SOURCE
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
2020-03-27 03:54:58 +00:00
#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;
}
2020-03-27 04:17:48 +00:00
if (m == 2) {
if (isleap(y)) {
if (d > 29) {
return 0;
2020-03-27 03:54:58 +00:00
}
} else {
2020-03-27 04:17:48 +00:00
if (d > 28) {
return 0;
}
2020-03-27 03:54:58 +00:00
}
}
if ( m == 4 || m == 6 || m == 9 || m == 11 ) {
if(d <= 30) {
return 1;
} else {
return 0;
}
}
return 1;
}
2020-03-27 02:39:01 +00:00
int main() {
2020-03-27 03:54:58 +00:00
char buffer[20];
struct tm tm = {0};
char *retval = fgets(buffer, sizeof(buffer), stdin);
if (retval == 0) {
puts("Neplatny datum");
return 0;
}
retval = strptime(buffer, "%e.%m.%-Y", &tm);
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;
if (mktime(&tm) == -1) {
puts("Neplatny datum");
return 0;
}
strftime(buffer, sizeof(buffer), "%e.%m.%-Y", &tm);
puts(buffer);
putc('\n', stdout);
return 0;
}