简体   繁体   English

time.h-结构tm和毫秒

[英]time.h - struct tm and milliseconds

I have log data ranging from 1000 to 100000 entries all from different sources. 我的日志数据来自不同来源,范围从1000到100000。 The objective is to sort this data by time (quickly). 目的是按时间(快速)对数据进行排序。 I was prototyping in C and i'm stuck because it looks like tm doesn't support milliseconds. 我正在使用C进行原型设计,但因为看起来tm不支持毫秒,所以我陷入了困境。 Am i missing something here ? 我在这里想念什么吗? Any suggestions ? 有什么建议么 ?

Idea: 理念:

  1. Open file. 打开文件。 Parse line-by-line and tokenize fields to c struct via strtok 通过strtok逐行解析并标记化字段以构造c

    1.1 Convert Date & Time to "struct tm". 1.1将日期和时间转换为“ struct tm”。

    1.2 Build linked list 1.2建立链表

  2. Sort linked list based on time (don't know which sort algorithm i'd use) 根据时间对链接列表进行排序(不知道我要使用哪种排序算法)
  3. Output 输出量

[Example] [例]

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");
}

I don't understand why you need to convert those timestamps into a struct tm , or into any other representation. 我不明白为什么您需要将这些时间戳转换为struct tm或任何其他表示形式。 They will already lexicographically sort just the way you want, in their current string form. 他们将按照其当前的字符串形式按照您希望的方式对字典进行排序。 So just do that. 所以就那样做。

You can take the result you get back from mktime and convert it to milliseconds. 您可以获取从mktime返回的结果,并将其转换为毫秒。

if ((result = mktime(&tv)) == (time_t)-1) {
    fprintf(stderr, "Conversion Error\n");
}
long long resultmilli = (result * 1000LL) + ms;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM