time_t 是什么原始数据类型?

发布于 2024-09-01 07:02:35 字数 143 浏览 4 评论 0原文

我不知道time_t的数据类型。它是浮动双精度还是其他?因为如果我想显示它,我需要与 printf 对应的标签。我可以从那里处理其余的内容以显示 time_t 但我需要知道与其相对应的数据类型。

I do not know the data type of time_t. Is it a float double or something else? Because if I want to display it I need the tag that corresponds with it for printf. I can handle the rest from there for displaying time_t but I need to know the data type that corresponds with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴我老 2024-09-08 07:02:35

它是特定于平台的。但您可以将其转换为已知类型。

printf("%lld\n", (long long) time(NULL));

It's platform-specific. But you can cast it to a known type.

printf("%lld\n", (long long) time(NULL));
故人的歌 2024-09-08 07:02:35

不幸的是,它并不完全便携。它通常是整数,但它可以是任何“整数”或实浮动型”。

Unfortunately, it's not completely portable. It's usually integral, but it can be any "integer or real-floating type".

拥抱我好吗 2024-09-08 07:02:35

您可以使用函数difftime。它返回两个给定 time_t 值之间的差异,输出值为 double (参见 difftime 文档)。

time_t actual_time;
double actual_time_sec;
actual_time = time(0);
actual_time_sec = difftime(actual_time,0); 
printf("%g",actual_time_sec);

You can use the function difftime. It returns the difference between two given time_t values, the output value is double (see difftime documentation).

time_t actual_time;
double actual_time_sec;
actual_time = time(0);
actual_time_sec = difftime(actual_time,0); 
printf("%g",actual_time_sec);
善良天后 2024-09-08 07:02:35

您始终可以使用 mktime 之类的东西来创建已知时间(午夜、昨晚),并使用 difftime 来获取两者之间的双精度时间差。对于独立于平台的解决方案,除非您深入研究库的详细信息,否则您不会做得更好。根据 C 规范,time_t 的定义是实现定义的(意味着库的每个实现都可以按照自己的喜好定义它,只要使用它的库函数按照规范运行即可。)

话虽如此,大小我的 Linux 机器上的 time_t 是 8 个字节,这表明是 long int 或 double。所以我这样做了:

int main()
{
    for(;;)
    {
        printf ("%ld\n", time(NULL));
        printf ("%f\n", time(NULL));
        sleep(1);
    }
    return 0;
}

%ld 给出的时间每一步增加 1,浮点数每次打印 0.000。如果您执意要使用 printf 来显示 time_ts,那么最好的选择是尝试自己的此类实验,看看它在您的平台和编译器上的效果如何。

You could always use something like mktime to create a known time (midnight, last night) and use difftime to get a double-precision time difference between the two. For a platform-independant solution, unless you go digging into the details of your libraries, you're not going to do much better than that. According to the C spec, the definition of time_t is implementation-defined (meaning that each implementation of the library can define it however they like, as long as library functions with use it behave according to the spec.)

That being said, the size of time_t on my linux machine is 8 bytes, which suggests a long int or a double. So I did:

int main()
{
    for(;;)
    {
        printf ("%ld\n", time(NULL));
        printf ("%f\n", time(NULL));
        sleep(1);
    }
    return 0;
}

The time given by the %ld increased by one each step and the float printed 0.000 each time. If you're hell-bent on using printf to display time_ts, your best bet is to try your own such experiment and see how it work out on your platform and with your compiler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文