Bug 251918 - strptime(): %b always returns month 0
Summary: strptime(): %b always returns month 0
Status: Closed Works As Intended
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-12-17 11:47 UTC by Ian Chard
Modified: 2020-12-23 21:33 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Chard 2020-12-17 11:47:34 UTC
I noticed this when using 'date -f' to convert a date string:

$ date -j -f '%b %j %T %Y %Z' 'Mar 18 12:00:00 2021 GMT'
Mon Jan 18 12:00:00 GMT 2021

Passing this directly to strptime() gives the same results.  If I do

struct tm t;
strptime("Mar 18 12:00:00 2021 GMT", "%b %j %T %Y %Z", &t);

then t.tm_mon is always 0.

If I use an invalid month then strptime() returns NULL, so it is recognising the month names, just not actually setting the correct month number in the struct tm.

Tried with a couple of locales (including 'C') with the same results.
Comment 1 Yuri Pankov freebsd_committer freebsd_triage 2020-12-17 16:54:24 UTC
That's because you are using '%j' (day of the year) instead of '%d' (day of the month), and it overrides month specification as it comes earlier.
Comment 2 Yuri Pankov freebsd_committer freebsd_triage 2020-12-17 17:30:16 UTC
(In reply to Yuri Pankov from comment #1)
Actually, "as it comes earlier" part is not correct; it seems to be implementation choice as standard does not specify was happens if conflicting specifiers are provided:

- if %b and %j are specified, value converted by %b is ignored, month comes from %j
- if %d and %j are specified, month day comes from %d, month comes from %j
- if all of the %b, %d, and %j are specified, %j is ignored, values come from %b and %d
Comment 3 Yuri Pankov freebsd_committer freebsd_triage 2020-12-17 17:47:37 UTC
*** Bug 244906 has been marked as a duplicate of this bug. ***
Comment 4 Yuri Pankov freebsd_committer freebsd_triage 2020-12-23 21:33:11 UTC
Closing as "not a bug", please reopen with more information if you think otherwise.