首页 > 其他分享 >C语言学习:解析时间

C语言学习:解析时间

时间:2023-02-12 13:33:42浏览次数:50  
标签:current INT C语言 学习 tm time PRINT 解析 parsed

 

 1 #define _XOPEN_SOURCE
 2 
 3 #include <io_utils.h>
 4 #include <time_utils.h>
 5 #include <time.h>
 6 
 7 int main() {
 8   long_time_t current_time_in_ms = TimeInMillisecond();
 9   int current_time_millisecond = current_time_in_ms % 1000;
10   time_t current_time;
11   time(&current_time);
12   PRINT_LLONG(current_time);
13 
14   struct tm *calendar_time = localtime(&current_time);
15   PRINT_INT(calendar_time->tm_year);
16   PRINT_INT(calendar_time->tm_mon);
17   PRINT_INT(calendar_time->tm_mday);
18   PRINT_INT(calendar_time->tm_hour);
19   PRINT_INT(calendar_time->tm_min);
20   PRINT_INT(calendar_time->tm_sec);
21 
22   puts(asctime(calendar_time));
23   puts(ctime(&current_time));
24 
25   //2020-11-09 06:59:47
26   char current_time_s[20];
27 //  size_t size = strftime(current_time_s, 20, "%Y-%m-%d %H:%M:%S", calendar_time);
28   size_t size = strftime(current_time_s, 20, "%F %T", calendar_time);
29   PRINT_INT(size);
30   puts(current_time_s);
31 
32   char *time = "2020-11-10 90:80:32.123";
33   struct tm parsed_time;
34   int millisecond;
35 //   unix
36 //  char *unparsed_string = strptime(time, "%F %T", &parsed_time);
37 
38 // msvc
39   sscanf(time, "%4d-%2d-%2d %2d:%2d:%2d.%3d",
40          &parsed_time.tm_year,
41          &parsed_time.tm_mon,
42          &parsed_time.tm_mday,
43          &parsed_time.tm_hour,
44          &parsed_time.tm_min,
45          &parsed_time.tm_sec,
46          &millisecond);
47 
48   parsed_time.tm_year -= 1900;
49   parsed_time.tm_mon -= 1;
50 
51   mktime(&parsed_time);
52 
53   PRINT_INT(parsed_time.tm_year);
54   PRINT_INT(parsed_time.tm_mon);
55   PRINT_INT(parsed_time.tm_mday);
56   PRINT_INT(parsed_time.tm_hour);
57   PRINT_INT(parsed_time.tm_min);
58   PRINT_INT(parsed_time.tm_sec);
59 
60 //  puts(unparsed_string);
61 //  sscanf(unparsed_string, ".%3d", &millisecond);
62 
63   PRINT_INT(millisecond);
64 
65   //20201109070456
66 //  size_t size2 = strftime(current_time_s, 20, "%Y%m%d%H%M%S", calendar_time);
67 //  sprintf(current_time_s + 14, "%03d", current_time_millisecond);
68 //  PRINT_INT(size2);
69 //  puts(current_time_s);
70   return 0;
71 }

 

标签:current,INT,C语言,学习,tm,time,PRINT,解析,parsed
From: https://www.cnblogs.com/liumy/p/17113708.html

相关文章