3

The specification for strptime:

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strptime.html

is mostly clear on the possible conversion specifications and what input they require. However, there seems to be no specification for how this function stores the results in the struct tm. What is supposed to happen if multiple specifiers read partly or wholly conflicting data. A simple example would be the presence of both %m and %b (or even duplicate %m's) reading conflicting months, but perhaps a more interesting example is when %d (day of month) and %a (day of week) conflict. Which takes precedence? Is strptime even supposed to ensure a consistent output in the struct tm, or simply store the fields as-read? Certain things like %W (week of year) have no direct representation in struct tm, so I would assume they must result in the generation of derived output based on other fields, but it's unclear when this applies.

I realize since the specification seems to be lacking, I may be asking for an answer that simply doesn't exist, but things that could characterize a helpful answer would be:

  • behavior of historical implementations on which the standardized function was based.
  • citations of relevant defect reports
  • links to past discussions of the topic (mailing lists, bug trackers, usenet, etc.)
  • other related standards for time parsing
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711

1 Answers1

2

The additional fields can be used for verification of the date. Validation and verification, you can convert the string to a series of numbers that reresent a date, then you must verify that all those numbers refer to one correct date, e.g. day-of-week is correct, if the date is not valid then strptime returns NULL.

Steve-o
  • 12,678
  • 2
  • 41
  • 60
  • So everything `strptime` converts must be *consistent*, but not necessarily *complete*? – R.. GitHub STOP HELPING ICE Aug 17 '11 at 03:05
  • @R.. that's what I would go with being *reasonable* behaviour. Obviously some implementations need not verify consistency and just return garbage ala GIGO. – Steve-o Aug 17 '11 at 03:08
  • What about derived values when it's not a matter of consistency? For example if I read the day-of-year and year, can I expect to be able to find meaningful values in the month, day-of-month, and day-of-week fields, or only in the day-of-year and year fields? Surely if year is omitted `strptime` could not provide any of that except the day-of-year value it read... – R.. GitHub STOP HELPING ICE Aug 17 '11 at 04:48
  • @R.. well the options are either 0 (the start of the epoch), or take the values from the current time. So if you provide "Friday" it would assume 2011. However `man strptime` shows it only sets the values you provide, so a third option. – Steve-o Aug 17 '11 at 07:28