mirror of
				https://github.com/theoludwig/libcproject.git
				synced 2025-05-21 23:21:15 +02:00 
			
		
		
		
	| @@ -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; | ||||
| } | ||||
|   | ||||
| @@ -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 | ||||
|   | ||||
| @@ -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); | ||||
| } | ||||
|   | ||||
| @@ -21,4 +21,6 @@ void date_duration_seconds_between_2_dates_test(); | ||||
|  | ||||
| void date_to_utc_test(); | ||||
|  | ||||
| void date_get_age_test(); | ||||
|  | ||||
| #endif | ||||
|   | ||||
		Reference in New Issue
	
	Block a user