tech-userlevel archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Looking for comments on changing strptime(3) %p behavior
The current behavior of strptime(3) and the %p format specifier is
a bit inconsistent/counter-intuitive/surprising. The results aren't
always what one would expect. Nor what is seen from other strptime(3)
implementations. Looking for feedback on the following.
a. strptime(str, "%H %p", &tm)
Currently if str contains 0-11, for the hour, and a am/pm string
the time will be adjusted accordingly. However if the str
contains 12-23, for the hour, and a am/pm string strptime(3)
returns NULL (error). This seems inconsistent if not a bug.
Does it make sense to apply %p to any hour parsed by %H (or %k)
as %H implies a 24-hour clock value? The current code is
inconsistent; %p is valid with 0-11 while %p is invalid with
12-23 (strptime(3) returns NULL). The tm_hour is only adjusted
when %pm is PM and < 11.
Would a more consistent approach would be to not apply %p to
any 24-hour clock value? It doesn't necessarily make sense that
%H (24-hour clock) with %p should be an error regardless of the
value parsed. The %p would be parsed but ignored when combined
with a 24-hour clock.
b. strptime(str, "%p", &tm)
Currently if str contains a am/pm string and no hour is specified
then tm_hour is adjusted as per above or an error is returned
based on tm_hour. This would seem to be a bug on both counts.
The %p should be parsed and only applied to a valid 12-hour
clock value obtained via the same parse.
Additionally what should happen if no hour is parsed, either a
12-hour or 24-hour clock? Should the tm_hour be set to a default
AM (0) or PM (12) or just left alone?
c. strptime("12", "%I", &tm)
Currently the 12 is converted tm_hour = 0. This seems rather
odd since %I must be in the range of 1-12. If this isn't a bug
then clearly it violates POLA having 12 == 0. It makes more
sense, in the absence of %p, to have 12 == 12.
Changes are worked up for all of these. Just waiting on feedback
about some of the above questions before committing.
Additionally, the changes will also allow for parsing (and applying)
%p before %I (or %H if the current behavior is kept). Currently
a %p must be specified after the hour for it to be applied.
Thanks,
Brian
Home |
Main Index |
Thread Index |
Old Index