wmusic: Print message when connecting to new player.
[dockapps.git] / wmframepic / src / age_calc.c
blob11e88550ab6bec5be7c6bec3ed6cf0eb028bfdd0
1 #include "age_calc.h"
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)
16 months_table[2] = 29;
17 else
18 months_table[2] = 28;
20 total_years = year_curr - year_birth;
21 total_months = month_curr - month_birth;
22 total_days = day_curr - day_birth;
24 if(total_days < 0) {
25 total_days += months_table[month_curr==1 ? 12 : month_curr-1];
26 total_months--;
29 if(total_months < 0) {
30 total_months += 12;
31 total_years--;
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() {
48 char **phrases;
49 phrases = malloc(NUMBER_OF_ROWS * sizeof(char *));
50 int i;
52 for(i = 0; i < NUMBER_OF_ROWS; i++)
53 phrases[i] = malloc(NUMBER_OF_COLUMNS * sizeof(char));
55 return phrases;
59 void clear_phrases(char **phrases) {
60 int i;
62 for(i = 0; i < NUMBER_OF_ROWS; i++)
63 free(phrases[i]);
65 free(phrases);
69 char **get_phrases(int day, int month, int year) {
70 time_t today;
71 time(&today);
72 char **phrases = alloc_phrases();
73 int result[3];
75 calc_day_year_month(today, day, month, year, result);
76 construct_phrases(result, phrases);
78 return phrases;