1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-12-11 21:13:00 +01:00

feat(date): add date_get_age

Fixes #7
This commit is contained in:
Théo LUDWIG 2024-10-08 08:47:16 +02:00
parent 95ad9f24f4
commit b0fd3bf373
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 46 additions and 0 deletions

View File

@ -329,3 +329,11 @@ struct date *date_get_now_local() {
return date;
}
uint16_t date_get_age(struct date *birth_date, struct date *current_date) {
uint16_t age = current_date->year - birth_date->year;
if (current_date->month < birth_date->month || (current_date->month == birth_date->month && current_date->day < birth_date->day)) {
age--;
}
return age;
}

View File

@ -338,4 +338,13 @@ struct date *date_get_now_utc();
*/
struct date *date_get_now_local();
/**
* @brief Calculates the age of a person based on their birth date.
*
* @param birth_date
* @return uint16_t
* @since v5.1.0
*/
uint16_t date_get_age(struct date *birth_date, struct date *current_date);
#endif

View File

@ -8,6 +8,7 @@ void date_test() {
date_get_is_leap_year_test();
date_duration_seconds_between_2_dates_test();
date_to_utc_test();
date_get_age_test();
}
void date_copy_test() {
@ -162,3 +163,29 @@ void date_to_utc_test() {
free(iso_string);
free(date);
}
void date_get_age_test() {
struct date *birth_date = date_from_iso_string("1980-02-20T00:00:00.000Z");
struct date *current_date = date_from_iso_string("2018-03-20T00:00:00.000Z");
assert(date_get_age(birth_date, current_date) == 38);
free(birth_date);
free(current_date);
birth_date = date_from_iso_string("1980-07-20T00:00:00.000Z");
current_date = date_from_iso_string("2018-03-20T00:00:00.000Z");
assert(date_get_age(birth_date, current_date) == 37);
free(birth_date);
free(current_date);
birth_date = date_from_iso_string("1980-03-20T00:00:00.000Z");
current_date = date_from_iso_string("2018-03-20T00:00:00.000Z");
assert(date_get_age(birth_date, current_date) == 38);
free(birth_date);
free(current_date);
birth_date = date_from_iso_string("1980-03-25T00:00:00.000Z");
current_date = date_from_iso_string("2018-03-20T00:00:00.000Z");
assert(date_get_age(birth_date, current_date) == 37);
free(birth_date);
free(current_date);
}

View File

@ -21,4 +21,6 @@ void date_duration_seconds_between_2_dates_test();
void date_to_utc_test();
void date_get_age_test();
#endif