0

I am recently checking GNU manual frequently. Here is one thing I am not able to figure out at all.

About date command: date %F

Doc states that

full date in ISO 8601 format; like ‘%+4Y-%m-%d’ except that any flags or field width override the ‘+’ and (after subtracting 6) the ‘4’.

What does this sentence mean? What are flags and field width? Why should we include the plus sign + in %+4Y-%m-%d? date +%4Y-%m-%d prints 2021-03-02 but date +%+4Y-%m-%d prints %+4Y-03-02

Please help and thanks in advance.

2 Answers 2

1

If you specify flags or field width %F, it subtracts 6 from the width (because the -%m-%d part of the date always takes 6 characters) and uses that as the width of the %Y part of the date, and the options replace the options to %Y.

So %12F is equivalent to %6Y-%m-%d. The flags don't include + and the width 12 becomes width 6 for the years.

Flags and field widths are a GNU date extension. The flags are documented here

Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for your reply. I think the problem is that I have no idea what flags and field width are. I guess the number between % and F specifies the field width.
date +%12F prints ` 2021-03-02` with 2 spaces padded at the beginning of the string. date %6Y-%m-%d prints 002021-03-02. So I think they are not equivalent.
You might have noticed that format in the doc includes a + between % and 4 in %+4Y-%m-%d. What is the meaning of this plus sign? I also tried it in terminal like date +%+4Y-%m-%d but it prints %+4Y-03-02. Is it a legal syntax for date command?
See the date documentation. The plus is how you specify the format; it's kind of like an option, except it's not.
@tripleee thanks, I am following date documentation. Plus should appear at the beginning of the format, right? But it appears after the first % which makes the syntax invalid I think.
|
1

To summurize the reason why date +%+4Y-%m-%d prints %+4Y-03-02 instead 2021-03-02 is that the version of coreutils installed on my machine is 8.30 which is different from the version of the web manual which is 8.32.

And according to coreutils release announcement for 8.31 here

'date' now supports the '+' conversion specification flag,
introduced in POSIX.1-2017.

date starts to support '+' flag since version 8.31.

Tips for others who found GNU manual mysterious:

  1. Check the version of web manual you are reading first.

  2. Check the version of coreutils installed by apt-cache policy coreutils

  3. Use info <command> to check for doc locally first to find the correct manual responding to your command version if you don't feel like to update coreutils.

  4. If you are curious since which version the changes made, visit https://lists.gnu.org/archive/html/coreutils-announce/ to browse the release announcements.

  5. If you'd like to use newer version of GNU package and to find the correct Ubuntu version to install, say coreutils, you can visit this page. Under the section of Search package directories, input coreutils, select Distribution as any, press search button and there you go. You can find the version of Ubuntu with associated version of coreutils.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.