mirror of
				https://github.com/theoludwig/libcproject.git
				synced 2025-05-21 23:21:15 +02:00 
			
		
		
		
	
							
								
								
									
										22
									
								
								lib/date.c
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								lib/date.c
									
									
									
									
									
								
							| @@ -285,3 +285,25 @@ 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(¤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; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -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,12 @@ 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(); | ||||||
|  |  | ||||||
| #endif | #endif | ||||||
|   | |||||||
							
								
								
									
										6
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.c
									
									
									
									
									
								
							| @@ -122,5 +122,11 @@ 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); | ||||||
|  |  | ||||||
|   return EXIT_SUCCESS; |   return EXIT_SUCCESS; | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user