简体   繁体   English

time_t 和 LONG_MAX:ctime() 和 difftime() 的问题

[英]time_t and LONG_MAX: issues with ctime() and difftime()

I know that time_t is a long int on my system implementation.我知道time_t在我的系统实现中是一个很长的整数。 For the sake of experimenting, let's initialize it with INT_MAX .为了实验,让我们用INT_MAX初始化它。 The data are displayed correctly, and running the code some second later decreases the counter accordingly.数据显示正确,稍后运行代码会相应地减少计数器。

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

int main(){
    /* Set the current and a very large time */
    time_t now;
    time(&now);
    time_t max = INT_MAX;

    /* Print them on screen. */
    printf("Last time: %s", ctime(&max));
    printf("\nCurrent time: %s", ctime(&now));

    /* Compute their difference */
    double remaining = difftime(max, now);
    printf("Remaining seconds (difftime): %lf\n", remaining);
    return 0;
}

The problem is: when I replace INT_MAX with LONG_MAX , I obtain two apparently strange behaviors.问题是:当我用LONG_MAX替换INT_MAX时,我得到了两个明显奇怪的行为。

  1. ctime(&max) returns a NULL. ctime(&max) 返回 NULL。 The reason might be that, if I remember correctly, the string is supposed to have maximum length 26, in this case exceeded.原因可能是,如果我没记错的话,字符串的最大长度应该是 26,在这种情况下超过了。 Am I right?我对吗?

  2. if I run the code some seconds later, the variable " remaining " is still the same, conversely to the MAX_INT case where it decreases second after second.如果我在几秒钟后运行代码,变量“剩余”仍然是相同的,与MAX_INT情况相反,它一秒一秒地减少。 How can I explain such a behavior?我该如何解释这种行为? (these values are surely below DBL_MAX) (这些值肯定低于 DBL_MAX)

EDIT: sorry, I wrote "size_t" instead of "time_t" in my previous version, my mistake.编辑:对不起,我在以前的版本中写了“size_t”而不是“time_t”,这是我的错误。

if I run the code some seconds later, the variable "remaining" is still the same如果我在几秒钟后运行代码,变量“remaining”仍然是相同的

Assuming your platform uses the IEEE 754 representation for a double type, then that will have a precision of approximately 16 (decimal) digits.假设您的平台对double精度类型使用IEEE 754 表示,那么它将具有大约16(十进制)位的精度。 Your (long) long int type's LONG_MAX (if 64-bits) has a value (probably) of 9223372036854775807 , which is 19 digits - so you are losing precision when subtracting now from max , and the displayed result will appear the same.您的(long) long int类型的LONG_MAX (如果是 64 位)的值(可能)为9223372036854775807 ,即19位数字 - 所以nowmax中减去时会丢失精度,并且显示的结果将显示相同。 If you were to wait more than 1,000 seconds between runs, then you would most likely see a difference.如果您在两次运行之间等待超过 1,000 秒,那么您很可能会看到差异。

ctime(&max) returns a NULL ctime(&max) 返回 NULL

See: ctime returning null .请参阅: ctime 返回 null

Type time_t is intended for timestamps.类型time_t用于时间戳。 I don't see why you would want to put LONG_MAX in there.我不明白你为什么要把LONG_MAX放在那里。 Chances are its value is too big to properly represent a time stamp the system can handle.它的值可能太大而无法正确表示系统可以处理的时间戳。 So it would make sense ctime() doesn't know how to handle it and returns NULL to indicate the error.因此, ctime()不知道如何处理它并返回 NULL 以指示错误是有意义的。

About the question why remaining has the same value when reunning again later, I don't really know, unless ctime(&max) somehow alters the value of max .关于为什么remaining在稍后再次重新运行时具有相同值的问题,我真的不知道,除非ctime(&max)以某种方式改变了max的值。

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

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