AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 733074
Accepted
ka3ak
ka3ak
Asked: 2023-01-26 06:10:34 +0800 CST2023-01-26 06:10:34 +0800 CST 2023-01-26 06:10:34 +0800 CST

无法以特定格式设置日期

  • 772

使用以下命令获取日期没有问题:

$ 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’
bash
  • 1 1 个回答
  • 53 Views

1 个回答

  • Voted
  1. Best Answer
    Freddy
    2023-01-26T08:42:08+08:002023-01-26T08:42:08+08:00

    您的格式字符串仅用于格式化输出字符串,不用于解析输入字符串。

    支持的日历和时间输入格式在 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
    
    • 1

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve