Linux 格式化结构时间规范

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8304259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:36:10  来源:igfitidea点击:

Formatting struct timespec

clinuxgcc

提问by Cartesius00

How to format struct timespecto string? This structure is returned e.g. by clock_gettime()on Linux gcc:

如何格式化struct timespec为字符串?此结构由clock_gettime()Linux gcc返回,例如:

struct timespec {
    time_t   tv_sec;        /* seconds */
    long     tv_nsec;       /* nanoseconds */
};

采纳答案by caf

One way to format it is:

格式化它的一种方法是:

printf("%lld.%.9ld", (long long)ts.tv_sec, ts.tv_nsec)

回答by Sh4pe

You could use a std::stringstream. You can stream anything into it:

您可以使用 std::stringstream。您可以将任何内容流式传输到其中:

std::stringstream stream;
stream << 5.7;
stream << foo.bar;

std::string s = stream.str();

That should be a quite general approach. (Works only for C++, but you asked the question for this language too.)

这应该是一个非常通用的方法。(仅适用于 C++,但您也针对此语言提出了问题。)

回答by Adrian Cornish

You can pass the tv_sec parameter to some of the formatting function. Have a look at gmtime, localtime(). Then look at snprintf.

您可以将 tv_sec 参数传递给某些格式化函数。看看 gmtime, localtime()。然后看snprintf。

回答by stefanct

I wanted to ask the same question. Here is my current solution to obtain a string like this: 2013-02-07 09:24:40.749355372I am not sure if there is a more straight forward solution than this, but at least the string format is freely configurable with this approach.

我想问同样的问题。这是我当前获取这样的字符串的解决方案:2013-02-07 09:24:40.749355372我不确定是否有比这更直接的解决方案,但至少可以使用这种方法自由配置字符串格式。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define NANO 1000000000L

// buf needs to store 30 characters
int timespec2str(char *buf, uint len, struct timespec *ts) {
    int ret;
    struct tm t;

    tzset();
    if (localtime_r(&(ts->tv_sec), &t) == NULL)
        return 1;

    ret = strftime(buf, len, "%F %T", &t);
    if (ret == 0)
        return 2;
    len -= ret - 1;

    ret = snprintf(&buf[strlen(buf)], len, ".%09ld", ts->tv_nsec);
    if (ret >= len)
        return 3;

    return 0;
}

int main(int argc, char **argv) {
    clockid_t clk_id = CLOCK_REALTIME;
    const uint TIME_FMT = strlen("2012-12-31 12:59:59.123456789") + 1;
    char timestr[TIME_FMT];

    struct timespec ts, res;
    clock_getres(clk_id, &res);
    clock_gettime(clk_id, &ts);

    if (timespec2str(timestr, sizeof(timestr), &ts) != 0) {
        printf("timespec2str failed!\n");
        return EXIT_FAILURE;
    } else {
        unsigned long resol = res.tv_sec * NANO + res.tv_nsec;
        printf("CLOCK_REALTIME: res=%ld ns, time=%s\n", resol, timestr);
        return EXIT_SUCCESS;
    }
}

output:

输出:

gcc mwe.c -lrt 
$ ./a.out 
CLOCK_REALTIME: res=1 ns, time=2013-02-07 13:41:17.994326501