특정 형식으로 날짜를 설정할 수 없습니다

특정 형식으로 날짜를 설정할 수 없습니다

다음 명령을 사용하여 날짜를 가져오는 데 문제가 없습니다.

$ 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

관련 정보