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

Compare commits

..

4 Commits

Author SHA1 Message Date
semantic-release-bot
d14b56904e
chore(release): 5.1.0 [skip ci] 2024-10-08 06:50:16 +00:00
b0fd3bf373
feat(date): add date_get_age
Fixes #7
2024-10-08 08:47:16 +02:00
95ad9f24f4
feat(date): add date_get_now_local
Related #7
2024-10-08 08:37:24 +02:00
ded2ca0795
feat(date): add date_get_now_utc
Related #7
2024-10-08 08:36:14 +02:00
6 changed files with 120 additions and 1 deletions

View File

@ -285,3 +285,55 @@ void date_to_utc(struct date *date) {
date->timezone_utc_offset = 0; date->timezone_utc_offset = 0;
date_add_hours(date, mathematics_opposite(timezone_utc_offset)); date_add_hours(date, mathematics_opposite(timezone_utc_offset));
} }
struct date *date_get_now_utc() {
struct date *date = malloc(sizeof(struct date));
if (date == NULL) {
perror("Error (date_get_now_utc)");
exit(EXIT_FAILURE);
}
time_t current_time = time(NULL);
struct tm *current_time_tm = gmtime(&current_time);
date->year = (uint16_t)(current_time_tm->tm_year + 1900);
date->month = (uint8_t)(current_time_tm->tm_mon + 1);
date->day = (uint8_t)current_time_tm->tm_mday;
date->hours = (uint8_t)current_time_tm->tm_hour;
date->minutes = (uint8_t)current_time_tm->tm_min;
date->seconds = (uint8_t)current_time_tm->tm_sec;
date->milliseconds = 0;
date->timezone_utc_offset = 0;
return date;
}
struct date *date_get_now_local() {
struct date *date = malloc(sizeof(struct date));
if (date == NULL) {
perror("Error (date_get_now_local)");
exit(EXIT_FAILURE);
}
time_t current_time = time(NULL);
struct tm *current_time_tm = localtime(&current_time);
date->year = (uint16_t)(current_time_tm->tm_year + 1900);
date->month = (uint8_t)(current_time_tm->tm_mon + 1);
date->day = (uint8_t)current_time_tm->tm_mday;
date->hours = (uint8_t)current_time_tm->tm_hour;
date->minutes = (uint8_t)current_time_tm->tm_min;
date->seconds = (uint8_t)current_time_tm->tm_sec;
date->milliseconds = 0;
date->timezone_utc_offset = 0;
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

@ -6,6 +6,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include "convert.h" #include "convert.h"
#include "mathematics.h" #include "mathematics.h"
@ -321,4 +322,29 @@ void date_add_days(struct date *date, int64_t days);
*/ */
void date_to_utc(struct date *date); void date_to_utc(struct date *date);
/**
* @brief Get the current date in UTC.
*
* @return struct date*
* @since v5.1.0
*/
struct date *date_get_now_utc();
/**
* @brief Get the current date in local time.
*
* @return struct date*
* @since v5.1.0
*/
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 #endif

12
main.c
View File

@ -122,5 +122,17 @@ int main() {
terminal_print_array_list(array_list, terminal_print_int); terminal_print_array_list(array_list, terminal_print_int);
array_list_free(array_list); array_list_free(array_list);
struct date* date = date_get_now_utc();
string_t iso_string = date_to_iso_string(date);
printf("date_get_now_utc = %s\n", iso_string);
free(iso_string);
free(date);
date = date_get_now_local();
iso_string = date_to_iso_string(date);
printf("date_get_now_local = %s\n", iso_string);
free(iso_string);
free(date);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -8,6 +8,7 @@ void date_test() {
date_get_is_leap_year_test(); date_get_is_leap_year_test();
date_duration_seconds_between_2_dates_test(); date_duration_seconds_between_2_dates_test();
date_to_utc_test(); date_to_utc_test();
date_get_age_test();
} }
void date_copy_test() { void date_copy_test() {
@ -162,3 +163,29 @@ void date_to_utc_test() {
free(iso_string); free(iso_string);
free(date); 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_to_utc_test();
void date_get_age_test();
#endif #endif

View File

@ -1,4 +1,4 @@
#ifndef __LIBCPROJECT_VERSION__ #ifndef __LIBCPROJECT_VERSION__
#define __LIBCPROJECT_VERSION__ "5.0.0" #define __LIBCPROJECT_VERSION__ "5.1.0"
#endif #endif