C语言学习:格式化时间

 

 1 #include <io_utils.h>
 2 #include <time_utils.h>
 3 #include <time.h>
 4 
 5 int main() {
 6   long_time_t current_time_in_ms = TimeInMillisecond();
 7   int current_time_millisecond = current_time_in_ms % 1000;
 8   time_t current_time;
 9   time(&current_time);
10   PRINT_LLONG(current_time);
11 
12   struct tm *calendar_time = localtime(&current_time);
13   PRINT_INT(calendar_time->tm_year);
14   PRINT_INT(calendar_time->tm_mon);
15   PRINT_INT(calendar_time->tm_mday);
16   PRINT_INT(calendar_time->tm_hour);
17   PRINT_INT(calendar_time->tm_min);
18   PRINT_INT(calendar_time->tm_sec);
19 
20   puts(asctime(calendar_time));
21   puts(ctime(&current_time));
22 
23   //2020-11-09 06:59:47
24   char current_time_s[20];
25 //  size_t size = strftime(current_time_s, 20, "%Y-%m-%d %H:%M:%S", calendar_time);
26   size_t size = strftime(current_time_s, 20, "%F %T", calendar_time);
27   PRINT_INT(size);
28   puts(current_time_s);
29 
30   //20201109070456
31   size_t size2 = strftime(current_time_s, 20, "%Y%m%d%H%M%S", calendar_time);
32   sprintf(current_time_s + 14, "%03d", current_time_millisecond);
33   PRINT_INT(size2);
34   puts(current_time_s);
35   return 0;
36 }
View Code

 

posted @ 2023-02-12 13:14  泥古拉斯赵四  阅读(40)  评论(0编辑  收藏  举报