无法以特定格式设置日期

无法以特定格式设置日期

用下面的命令获取日期是没有问题的:

$ 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

相关内容