What is time_t ultimately a typedef to?

后端 未结 10 2276
陌清茗
陌清茗 2020-11-22 08:34

I searched my Linux box and saw this typedef:

typedef __time_t time_t;

But I could not find the __time_t definition.

10条回答
  •  情书的邮戳
    2020-11-22 09:23

    What is ultimately a time_t typedef to?

    Robust code does not care what the type is.

    C species time_t to be a real type like double, long long, int64_t, int, etc.

    It even could be unsigned as the return values from many time function indicating error is not -1, but (time_t)(-1) - This implementation choice is uncommon.

    The point is that the "need-to-know" the type is rare. Code should be written to avoid the need.


    Yet a common "need-to-know" occurs when code wants to print the raw time_t. Casting to the widest integer type will accommodate most modern cases.

    time_t now = 0;
    time(&now);
    printf("%jd", (intmax_t) now);
    // or 
    printf("%lld", (long long) now);
    

    Casting to a double or long double will work too, yet could provide inexact decimal output

    printf("%.16e", (double) now);
    

提交回复
热议问题