mirror of
https://github.com/theoludwig/libcproject.git
synced 2024-12-11 21:13:00 +01:00
parent
ded2ca0795
commit
95ad9f24f4
22
lib/date.c
22
lib/date.c
@ -307,3 +307,25 @@ struct date *date_get_now_utc() {
|
|||||||
|
|
||||||
return date;
|
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(¤t_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;
|
||||||
|
}
|
||||||
|
@ -330,4 +330,12 @@ void date_to_utc(struct date *date);
|
|||||||
*/
|
*/
|
||||||
struct date *date_get_now_utc();
|
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();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
6
main.c
6
main.c
@ -128,5 +128,11 @@ int main() {
|
|||||||
free(iso_string);
|
free(iso_string);
|
||||||
free(date);
|
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;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user