August 13 C Standard Rationale on mktime; seismo's retirement
The attached material appears on pages 99 and 100 of the 13 August 1987 "Rationale for Draft Proposed American National Standard for Information Systems Programming Language C". Note that mktime is supposed to examine tm_isdst when converting a "struct tm" to a "time_t". Coding errors in previous versions of the Rationale have also been corrected, although I believe that the when = *localtime(now); line should read when = *localtime(&now); Seismo's retirement from the mail forwarding business makes it unlikely that the time zone mailing list can be continued; this should not be a problem since volume has been nonexistent of late. If all else fails, try comp.std.c. --ado 4.12.2.3 The mktime function mktime was invented by the Committee to complete the set of time functions. With this function it becomes possible to perform portable calculations involving clock times and broken-down times. The rules on the ranges of the fields within the *timeptr record are crafted to permit useful arithmetic to be done. For instance, here is a paradigm for continuing some loop for an hour: #include <time.h> struct tm when; time_t now; time_t deadline; /* ... */ now = time(0); when = *localtime(now); when.tm_hour += 1; /* result is in the range [1,24] */ deadline = mktime(&when); printf("Loop will finish: %s\n", asctime(&when)); while (difftime(time(0), deadline) > 0) whatever(); The specification of mktime guarantees that the addition to the tm_hour field produces the correct result even when the new value of tm_hour is 24, i.e., a value outside the range ever returned by a library function in a struct tm object. One of the reasons for adding this function is to replace the capability to do such arithmetic which is lost when a programmer cannot depend on time_t being an integral multiple of some known time unit. Several readers of earlier versions of this Rationale have pointed out apparent problems in this example if now is just before a transition into or out of daylight saviings time. However, when.tm_isdst tags what sort of time was the basis of the calculation. Implementors, take heed.
Grunge, I'm not sure I believe this. Now the sole purpose of mktime is for doing arithmetic on time_t's. What a waste. Because that's just about what it amounts to if the caller has to set tm_isdst before calling mktime(). The use that most people want for mktime() is to be able to parse a string (or whatever) into the tm_hour fields, etc, and then produce a time_t .. having to second guess whether dst applied at that particular time is never going to work (and you can't look at the second char of the zone string and see if it happens to be 'D' .. even assuming that there is a zone string). This also doesn't account for time zone changes that skip hours from one dst timezone to another (when dst changes from +1 hour to +2 hours, in both cases tm_isdst will be true). kre
participants (2)
-
ado -
Robert Elz