friedl@mtndew.Tustin.CA.US (Steve Friedl) (08/13/90)
Hi folks, I am writing myself a mostly ANSI-compliant[%] version of the strftime(4.12.3.5) function, and I've a couple of questions on it. I am reading from the 7 Dec 88[*] draft of the standard plus the 12/8/8 version of the SVID 3, and my reference compilers are a very old AT&T C Issue 5 for the 3B2 and the IBM RS6000 xlc compiler, both purported to be ANSI. First, can strftime() assume that the broken-down time struct is "normalized" and correct? What should be done with out-of-range values (say, using the tm_mon field as an index into a table of pointers to month-name string). It seems that both xlc and CI5 libraries either copy in garbage or just dump core. A related question, must tm_yday and tm_mday be set or must the function figure them out as mktime() will? Certainly when passing a pointer obtained from (say) localtime() it will be all OK, but if I am building a tm struct myself it looks like I have to calculate these myself. Or are callers expected to call mktime() first to "normalize" (my made-up term) them before calling strftime() if we feel the need? Next, am I allowed to define and use additional conversions internally? The SVID3 defines a handful of them that I would not mind adding and using here but don't know whether this would be a bad idea. Do any other standards define strftime() with more conversions? I would love to know why the %U and %W conversions were added, especially with such, um, "interesting" definitions. For those who are not familiar with these, they are: %U - the week number of the year (with the first Sunday being the start of week 1) as a decimal number (00-53) %W - the week number of the year (with the first Monday being the start of week 1) as a decimal number (00-53) It took me a couple of hours to figure out what I think is the best way to calculate these, and I offer them here to maybe save somebody else some time -- I'd love to hear it if anybody has a better way (the yday and wday fields should have "tm->tm_" in front of them): U_weekno = (yday + (7 - (7000 + yday - wday) % 7)) / 7; W_weekno = (yday + (7 - (7001 + yday - wday) % 7)) / 7; Next, and more generally, are there any restrictions on a library function using setjmp/longjmp as long as they are self-contained? My strftime() function is using this in the store-a-char-in-the-user-buffer routine to detect when it has run out of room, and it means that I don't have to have tons of cascading error returns or checks all throughout the code. I know that setjmp/longjmp are a little strange in that there are all kinds of oddities with them, but is there any flat-out restrictions on this? Note: the Standard is clearly not explicit about many of the above choices, but I would like to follow the conventional wisdom where it's clear. Any input is welcome, and I'll summarize and post. Steve [%] - "mostly compliant" means I'm not messing with locales just yet. To quote Andrew Hume, "sorry japan" :-) [*] - yes, I know I should get a copy of the latest, but I've not found anybody pointing out any differences. -- Stephen J. Friedl, KA8CMY / Software Consultant / Tustin, CA / 3B2-kind-of-guy +1 714 544 6561 / friedl@mtndew.Tustin.CA.US / {uunet,attmail}!mtndew!friedl If Larry Ellison says it, it must be true.
zvs@bby.oz.au (Zev Sero) (08/16/90)
In article <496@mtndew.Tustin.CA.US> friedl@mtndew.Tustin.CA.US (Steve Friedl) writes:
%U - the week number of the year (with the first Sunday
being the start of week 1) as a decimal number (00-53)
The Sunday after 1st January is the beginning of week 01. This makes
a difference when the year begins on a Sunday.
%W - the week number of the year (with the first Monday
being the start of week 1) as a decimal number (00-53)
Ditto.
U_weekno = (yday + (7 - (7000 + yday - wday) % 7)) / 7;
W_weekno = (yday + (7 - (7001 + yday - wday) % 7)) / 7;
U_weekno = (6 + yday - wday) / 7;
W_weekno = (6 + yday - (wday ? wday - 1 : 6)) / 7;
--
Zev Sero - zvs@bby.oz.au
Violence is not a pleasant thing. It has caused much suffering in the world
since its invention, and many are convinced that it is Quite A Bad Thing.
- Steven Megachiropter Foster
DSTONE@WL9.Prime.COM (08/16/90)
Can anybody explain why the %U and %W specifiers of strftime are defined as they are? ISO 8601, which defines ISO week numbers, says that weeks begin on Monday and that week 1 is that week containing the first Thursday of the year. It also says that Monday is day 1 of the week, not 0 as the %w specifier gives. Moreover, also unlike %U and %W, there is no ISO week 0: any days in a year before week belong to week 52 or 53 of the previous year. Is there some ANSI standard which keeps the USA out of line from the international standard? Another question: does %Z reflect the daylight saving setting shown by tm_isdst? In other words, does it return (for example) "EDT" rather then "EST" in the summer? Or must it always return the basic time zone abbreviation (here "EST") if any? David Stone <dstone@wl9.prime.com> speaking for myself only.