我的日志数据范围从 1000 到 100000 个条目,全部来自不同的来源。目的是按时间(快速)对这些数据进行排序。我在 C 中制作原型(prototype),但我被卡住了,因为它看起来像 tm 不支持毫秒。我在这里错过了什么吗?有什么建议 ?
想法:
打开文件。逐行解析并通过 strtok 将字段标记为 c 结构
1.1 将日期和时间转换为“struct tm”。
1.2 建立链表
- 根据时间对链表进行排序(不知道我会使用哪种排序算法)
- 输出
[例子]
YYYY-MM-DD,HH:MM:SS:MS , PID, TID , COMPONENT, Message
2017-03-29,20:56:27:088, 3436,2568,COMPONENT, Message String blah blah
2017-03-29,20:56:27:089, 3436,2568,COMPONENT, Message String baaaaa
2017-03-29,20:56:27:079, 3436,2568,COMPONENT, Message String roarrr
2017-03-29,20:56:28:061, 3436,2568,COMPONENT, Message String meow
2017-03-29,20:56:25:044, 3436,2568,COMPONENT, Message String ruff ruff
.
.
.
100000 entries
const char T[] = "2017-03-29,20:56:27:088"; //Test String
time_t result = 0;
int year = 0, month = 0, day = 0;
int hour = 0, min = 0, sec = 0, ms = 0;
if (sscanf(T, "%4d-%2d-%2d,%2d:%2d:%2d", &year, &month, &day, &hour, &min, &sec) == 6) {
struct tm tv = {0};
tv.tm_year = year;
tv.tm_mon = month;
tv.tm_mday = day;
tv.tm_hour = hour;
tv.tm_min = min;
tv.tm_sec = sec;
//Crap, it doesn't look like tm handles milliseconds ????
if ((result = mktime(&tv)) == (time_t)-1) {
fprintf(stderr, "Conversion Error\n");
}
puts(ctime(&result));
}
else {
fprintf(stderr, "Invalid Format\n");
}
最佳答案
我不明白为什么您需要将这些时间戳转换为 struct tm
或任何其他表示形式。它们已经按照您想要的方式以当前的字符串形式按字典顺序排序。所以就这样做吧。
关于c - time.h - struct tm 和毫秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45042799/