3 static int months_table
[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
5 static void calc_day_year_month(time_t today
, int day_birth
, int month_birth
, int year_birth
, int *result
) {
6 int day_curr
, month_curr
, year_curr
, total_days
, total_months
, total_years
;
7 struct tm
*struct_today
;
9 struct_today
= localtime(&today
);
11 day_curr
= struct_today
->tm_mday
;
12 month_curr
= struct_today
->tm_mon
+ 1;
13 year_curr
= struct_today
->tm_year
+ 1900;
15 if((year_curr
%4==0 && year_curr
%100 !=0) || year_curr
%400==0)
20 total_years
= year_curr
- year_birth
;
21 total_months
= month_curr
- month_birth
;
22 total_days
= day_curr
- day_birth
;
25 total_days
+= months_table
[month_curr
==1 ? 12 : month_curr
-1];
29 if(total_months
< 0) {
34 result
[0] = total_days
;
35 result
[1] = total_months
;
36 result
[2] = total_years
;
40 static void construct_phrases(int *result
, char **buffer
) {
41 sprintf(buffer
[0], "%02d %s", result
[2], result
[2] > 1 ? "years" : "year");
42 sprintf(buffer
[1], "%02d %s", result
[1], result
[1] > 1 ? "months" : "month");
43 sprintf(buffer
[2], "%02d %s", result
[0], result
[0] > 1 ? "days" : "day");
47 static char **alloc_phrases() {
49 phrases
= malloc(NUMBER_OF_ROWS
* sizeof(char *));
52 for(i
= 0; i
< NUMBER_OF_ROWS
; i
++)
53 phrases
[i
] = malloc(NUMBER_OF_COLUMNS
* sizeof(char));
59 void clear_phrases(char **phrases
) {
62 for(i
= 0; i
< NUMBER_OF_ROWS
; i
++)
69 char **get_phrases(int day
, int month
, int year
) {
72 char **phrases
= alloc_phrases();
75 calc_day_year_month(today
, day
, month
, year
, result
);
76 construct_phrases(result
, phrases
);