7

I wanted to get the month name on macOS 11.6, and I tried

  1. checking the man page of date
  2. checking the man page of strptime

But I couldn't figure out what format specifier to use to display the month name.

After searching the internet, it seems that %b displays it.

I would like to know, where can I find all the information about specifiers within UNIX?

What's the official source, if man page doesn't have this info?

3
  • The One Official Source is the source of the date program. It's FOSS, after all.
    – waltinator
    Jan 16, 2022 at 22:14
  • It's worth noting that most versions of the date command accept at least one more format specifier than strftime does, namely %s, to print a raw time_t value (seconds since 1970).. Jan 17, 2022 at 19:43
  • strftime, at least on macOS, is documented as accepting %s.
    – chepner
    Jan 17, 2022 at 21:38

3 Answers 3

13

The manual for the C language function strftime() (man strftime) should contain all the date format specifiers across most Unix and Unix-like systems.

The strptime() function has to do with parsing strings into time values (which is not what you want to do), whereas strftime() has to do with outputting time values as formatted strings (which you want to do).

See also the POSIX specification for the strftime() interface.

9

MacOS is POSIX compliant, and is certified as such. Therefore you can also check the POSIX documentation for date. Here it does list the available format specifiers:

Conversion Specifications

%a Locale's abbreviated weekday name.

%A Locale's full weekday name.

%b Locale's abbreviated month name.

%B Locale's full month name.

...

It's a shame that the Mac's man page omits this useful information directly. However, it does contain this paragraph:

An operand with a leading plus (+) sign signals a user-defined format string which specifies the format in which to display the date and time. The format string may contain any of the conversion specifications described in the strftime(3) manual page, as well as any arbitrary text. A newline (\n) character is always output after the characters specified by the format string.

In the SEE ALSO section there is also a reference to strftime(3).

Unfortunately, on my Mac at least, there is no strftime(3) man page, so it's not possible to get to further information without referencing documentation online. (Apparently the C library man pages are installed as part of XCode.)

2
  • 3
    I believe that the strftime(3) man page is only present if you have XCode installed.
    – jwodder
    Jan 17, 2022 at 0:04
  • The POSIX standard list the formats that an implementation must support. It's not necessarily the complete list that an implementation of date can support.
    – chepner
    Jan 17, 2022 at 21:38
2

For the date command, man dateis the right reading. It does list %b and %B, so you should have found it directly, without perusing the internet. Here's a somewhat recent copy of that man date.

But that is specific to that command. Other programs, libraries have different formats. You will need to look up their individual documentation.

5
  • 3
    on macOs, man date doesn't show %b or %B. There is no info on any format specifier.
    – mtk
    Jan 16, 2022 at 8:09
  • 2
    @mtk The macOS man page says "An operand with a leading plus (+) sign signals a user-defined format string .... The format string may contain any of the conversion specifications described in the strftime(3) manual page, ..." so man 3 strftime has the relevant list. Jan 16, 2022 at 9:16
  • @GordonDavisson: Your comment should be the accepted answer, since it's the only one showing the process of how to get from man date to the solution.
    – Heinzi
    Jan 16, 2022 at 13:28
  • @Heinzi except that on at least my Mac, there is no strftime man page installed Jan 16, 2022 at 14:35
  • @roaima That's a separate issue. The man page for date specifically refers you to the man page for strftime.
    – chepner
    Jan 17, 2022 at 21:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .