特定の形式で日付を設定できません

特定の形式で日付を設定できません

次のコマンドを使用して日付を取得するのに問題はありません。

$ date '+%d%m%y %H%M%S.%N'
250123 170411.504761505

ただし、同じ形式を使用してフォーマットすることはできません。

sudo date '+%d%m%y %H%M%S.%N' -u -s "250123 170411.504761505"
date: invalid date ‘250123 170411.504761505’

時間、分、秒の間の区切り文字が必要なようです。なぜ?マニュアルページでは言及されていないようです。

それは述べる:

%H     hour (00..23)
%M     minute (00..59)
%S     second (00..60)

したがって、ソース文字列に6桁の数字がある場合は、問題なく解析できる必要があります(IMO)。

私はナノ部分が混乱していると思ったが、そうではなかった。

$sudo date '+%d%m%y %H%M%S' -u -s "250123 170411"
date: invalid date ‘250123 170411’

答え1

書式文字列は、入力文字列を解析するのではなく、出力文字列を書式設定するためにのみ使用されます。

サポートされているカレンダーと時刻の入力形式については、GNU日付情報マニュアルのセクション29の「日付入力形式」のサブセクションで説明しています(オンライン):

29.2 Calendar date items
========================

A “calendar date item” specifies a day of the year.  It is specified
differently, depending on whether the month is specified numerically or
literally.  All these strings specify the same calendar date:

     1972-09-24     # ISO 8601.
     72-9-24        # Assume 19xx for 69 through 99,
                    # 20xx for 00 through 68.
     72-09-24       # Leading zeros are ignored.
     9/24/72        # Common U.S. writing.
     24 September 1972
     24 Sept 72     # September has a special abbreviation.
     24 Sep 72      # Three-letter abbreviations always allowed.
     Sep 24, 1972
     24-sep-72
     24sep72

   The year can also be omitted.  In this case, the last specified year
is used, or the current year if none.  For example:

     9/24
     sep 24

   Here are the rules.

   For numeric months, the ISO 8601 format ‘YEAR-MONTH-DAY’ is allowed,
where YEAR is any positive number, MONTH is a number between 01 and 12,
and DAY is a number between 01 and 31.  A leading zero must be present
if a number is less than ten.  If YEAR is 68 or smaller, then 2000 is
added to it; otherwise, if YEAR is less than 100, then 1900 is added to
it.  The construct ‘MONTH/DAY/YEAR’, popular in the United States, is
accepted.  Also ‘MONTH/DAY’, omitting the year.

   Literal months may be spelled out in full: ‘January’, ‘February’,
‘March’, ‘April’, ‘May’, ‘June’, ‘July’, ‘August’, ‘September’,
‘October’, ‘November’ or ‘December’.  Literal months may be abbreviated
to their first three letters, possibly followed by an abbreviating dot.
It is also permitted to write ‘Sept’ instead of ‘September’.

   When months are written literally, the calendar date may be given as
any of the following:

     DAY MONTH YEAR
     DAY MONTH
     MONTH DAY YEAR
     DAY-MONTH-YEAR

   Or, omitting the year:

     MONTH DAY



29.3 Time of day items
======================

A “time of day item” in date strings specifies the time on a given day.
Here are some examples, all of which represent the same time:

     20:02:00.000000
     20:02
     8:02pm
     20:02-0500      # In EST (U.S. Eastern Standard Time).

   More generally, the time of day may be given as ‘HOUR:MINUTE:SECOND’,
where HOUR is a number between 0 and 23, MINUTE is a number between 0
and 59, and SECOND is a number between 0 and 59 possibly followed by ‘.’
or ‘,’ and a fraction containing one or more digits.  Alternatively,
‘:SECOND’ can be omitted, in which case it is taken to be zero.  On the
rare hosts that support leap seconds, SECOND may be 60.

[...]

たとえば、次の入力形式が機能します。

$ sudo date -us "23-01-25 17:04:11.504761505"
Wed 25 Jan 2023 05:04:11 PM UTC

または

$ sudo date -us "25 Jan 2023 17:04:11.504761505"
Wed 25 Jan 2023 05:04:11 PM UTC

または(-debugオプションあり)

$ sudo date -us "01/25/23 17:04:11.504761505" --debug
date: warning: value 1 has less than 4 digits. Assuming MM/DD/YY[YY]
date: parsed date part: (Y-M-D) 0023-01-25
date: parsed time part: 17:04:11.504761505
date: input timezone: TZ="UTC0" environment value or -u
date: warning: adjusting year value 23 to 2023
date: using specified time as starting value: '17:04:11'
date: starting date/time: '(Y-M-D) 2023-01-25 17:04:11'
date: '(Y-M-D) 2023-01-25 17:04:11' = 1674666251 epoch-seconds
date: timezone: Universal Time
date: final: 1674666251.504761505 (epoch-seconds)
date: final: (Y-M-D) 2023-01-25 17:04:11 (UTC)
date: final: (Y-M-D) 2023-01-25 17:04:11 (UTC+00)
Wed 25 Jan 2023 05:04:11 PM UTC

関連情報