time_t是什么原始数据类型?(复制)

时间:2022-01-10 16:27:24

This question already has an answer here:

这个问题已经有了答案:

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.

我不知道time_t的数据类型。是浮点数还是别的?因为如果我想显示它,我需要与它对应的标签用于printf。我可以从那里处理剩余的显示time_t,但是我需要知道与它相对应的数据类型。

4 个解决方案

#1


26  

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

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

#2


29  

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

它是特定于平台的。但是你可以把它转换成已知的类型。

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

#3


13  

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值之间的差异,输出值是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);

#4


2  

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.)

您总是可以使用类似mktime的东西来创建一个已知的时间(昨晚的午夜),并使用difftime来获得两者之间的双精度时间差异。对于独立于平台的解决方案,除非深入研究库的细节,否则您不会做得比这更好。根据C规范,time_t的定义是实现定义的(这意味着库的每个实现都可以随心所欲地定义它,只要使用它的库函数按照规范运行)。

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:

也就是说,我的linux机器上的time_t的大小是8字节,这意味着一个长整数或双整数。所以我做了:

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.

%ld给出的时间每一步增加一个,浮动每次打印0.000。如果您想要使用printf来显示time_ts,那么最好的方法是尝试您自己的这个实验,看看它在您的平台和编译器中是如何工作的。

#1


26  

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

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

#2


29  

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

它是特定于平台的。但是你可以把它转换成已知的类型。

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

#3


13  

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值之间的差异,输出值是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);

#4


2  

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.)

您总是可以使用类似mktime的东西来创建一个已知的时间(昨晚的午夜),并使用difftime来获得两者之间的双精度时间差异。对于独立于平台的解决方案,除非深入研究库的细节,否则您不会做得比这更好。根据C规范,time_t的定义是实现定义的(这意味着库的每个实现都可以随心所欲地定义它,只要使用它的库函数按照规范运行)。

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:

也就是说,我的linux机器上的time_t的大小是8字节,这意味着一个长整数或双整数。所以我做了:

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.

%ld给出的时间每一步增加一个,浮动每次打印0.000。如果您想要使用printf来显示time_ts,那么最好的方法是尝试您自己的这个实验,看看它在您的平台和编译器中是如何工作的。