lots of questions about strftime()
Steve Friedl
friedl at mtndew.Tustin.CA.US
Mon Aug 13 17:05:47 AEST 1990
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 at mtndew.Tustin.CA.US / {uunet,attmail}!mtndew!friedl
If Larry Ellison says it, it must be true.
More information about the Comp.std.c
mailing list