Namespaces
Variants
Actions

Talk:c/chrono/difftime

From cppreference.com
< Talk:c‎ | chrono

[edit] Notes

At http://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/types.h.html I found "time_t and clock_t shall be integer or real-floating types" and "time_t - Used for time in seconds." So, POSIX can represent a fraction of a second. In the Notes, using "but" seems to present the clause "C and C++ allow fractional units for time_t" in contrast to the situation with POSIX. Yet, both POSIX and C/C++ allow fractional values. So, I think that the relevant difference is that POSIX defines time_t in seconds while C/C++ define time_t only as "capable of representing times" with no mention of a specific unit like seconds. I am trying to find a clearer statement for Notes and offer the following replacement: "On POSIX systems, time_t is measured in seconds; in C/C++, the range and precision of times representable in time_t is implementation-defined. In either case, difftime is equivalent to arithmetic subtraction and returns a value in seconds." Newatthis (talk) 06:18, 24 April 2015 (PDT)

you are looking at an outdated document (if you were to go two levels up, you'd see a note about that on the top of the page, in red letters). Current POSIX states, at sys/types.h, "time_t shall be an integer type.". Also, it would be incorrect to claim that "in either case difftime is equivalent to arithmetic subtraction". --Cubbi (talk) 06:27, 24 April 2015 (PDT)
Thanks. I'll make a second attempt to stress the difference. "On POSIX systems, time_t is measured in an integral number of seconds, and difftime is equivalent to arithmetic subtraction; however, C and C++ allow fractional values without specifying a unit of measurement for time_t." Would that pass muster? Newatthis (talk) 07:20, 24 April 2015 (PDT)
I think this would make it more confusing to read. What's wrong with what the page says now? POSIX measures time in seconds, C doesn't have to, that's what it already says. FYI, the note was added to provide an answer to the frequently asked question "why use difftime if we can just subtract". --Cubbi (talk) 07:46, 24 April 2015 (PDT)
After more reading, thinking, and learning from the standard, stackoverflow, and your comments, I believe that I am starting to understand the sentence. If C's time_t were an integer type measured in milliseconds, then difftime would still return a value in seconds while arithmetic subtraction would return a value in milliseconds - not equivalent operations. The context bridging the two clauses in this compound sentence led me to conclude that C's allowing fractional units for time_t means that difftime and arithmetic difference would not be equivalent in all cases. On the other hand, could not "allowing fractional units" mean encoding time as a double (one of the real types) measured in seconds, and therefore arithmetic difference could return seconds just like difftime? I would like to try to increase the contrast between POSIX and C expressed in the compound sentence of the Notes. "On POSIX systems, time_t is an integer type with values measured in seconds, and difftime is equivalent to arithmetic subtraction; however, on a C and C++ implementation, time_t can be one of the integer types with values measured in a fractional unit of time (such as milliseconds), and difftime is not equivalent to arithmetic subtraction." Newatthis (talk) 05:36, 27 April 2015 (PDT)
what's the point of "can be one of the integer types"? (it can be a double using fractional units too). The only reason I see to edit that sentence is really to add a couple words to mention that encoding of the time is unspecified, since whole seconds can be encoded in an integer time_t in ways that produce nonsense after subtraction. --Cubbi (talk) 06:10, 27 April 2015 (PDT)
OK, many thanks for your generous feedback. Newatthis (talk) 04:32, 29 April 2015 (PDT)