Bash 中的组合日期和时间表示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19561001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combined date and time representation in Bash
提问by Misha Slyusarev
How can I obtain current date in the following format using Bash
如何使用 Bash 以下列格式获取当前日期
YYYY-MM-DDThh:mm:ss
YYYY-MM-DDThh:mm:ss
回答by fedorqui 'SO stop harming'
With these options:
使用这些选项:
$ date "+%Y-%m-%d %H:%M:%S"
2013-10-24 10:40:12
or even (thanks Birei!)
甚至(感谢 Birei!)
$ date "+%F %T"
2013-10-24 10:40:12
All format controls can be get from man date
.
所有格式控件都可以从man date
.
I don't know what your T
means, but for example you have:
我不知道你的T
意思,但例如你有:
$ date "+%F %T %Z"
2013-10-24 10:40:12 CEST
回答by Alec Istomin
Use date -I (iso-8601) format, as already pointed by @karfau
使用 date -I (iso-8601) 格式,正如@karfau 已经指出的那样
$ man date
-I[FMT], --iso-8601[=FMT]
output date/time in ISO 8601 format. FMT='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns' for date and time to the indicated
precision.
Example: 2006-08-14T02:34:56-06:00
Examples:
例子:
$ date -I
2018-10-23
$ date -Iminutes
2018-10-23T14:56-07:00
$ date -Iseconds
2018-10-23T14:56:44-07:00
回答by Michael Treanor
Making Your macOS Bash Utilities More Compatible
使您的 macOS Bash 实用程序更加兼容
When you are starting out with the command line, it is so great to have a huge community of users who can answer questions and help you on your way. If you use macOS, some of the very good advice will not work exactly right due to Apple's particular choices and implementations.
当您开始使用命令行时,拥有庞大的用户社区可以回答问题并帮助您前进,真是太好了。如果您使用 macOS,由于 Apple 的特定选择和实现,一些非常好的建议将不会完全正确。
Be Compatible
兼容
There are a lot of ways to do things. I don't have any certain preference either way, except that I just want things to work simply and correctly. Being "compatible" with the majority of users who post on here will make your life simpler. Coreutils
is a freesoftware that will make macOS more "compatible" with standard linux tools.
有很多方法可以做事。无论哪种方式,我都没有任何偏好,只是我只想让事情简单而正确地工作。与在这里发帖的大多数用户“兼容”将使您的生活更简单。Coreutils
是一个免费软件,它将使 macOS 与标准 linux 工具更加“兼容”。
Nerd Sidebar
If you are interested in the super nerd details and philosophy, or perhaps wish to make your own utilities, I like this pageon MaiZure's Projectsthat explores the design of the tools. This is the official githubrepo and it contains a variety of tools and improvements from the decades of development that started in 1983. As you might expect, it is a gumbo of different classic sysoplanguages.
书呆子侧边栏
如果您对超级书呆子的细节和哲学感兴趣,或者可能希望制作自己的实用程序,我喜欢探索工具设计的MaiZure 项目上的这个页面。这是官方的github 存储库,它包含了从 1983 年开始的几十年开发中的各种工具和改进。如您所料,它是不同经典sysop语言的混合汤。
Just Make It Work!
让它发挥作用!
For most people who just want it to work right, install coreutils
and symlink them to replace the originals. Instructions are included in the package.
对于大多数只希望它正常工作的人来说,安装coreutils
并符号链接它们以替换原件。说明包含在包装中。
Coreutils
is a standard (non builtin) set of utilities that is much more "compatible" with much of the "linux-ish" advice you see around the internet. Whichever shell (BASH, ZSH, etc) you use, you can make your life mucheasier by installing this set of GNU core utilities.
Coreutils
是一组标准(非内置)实用程序,与您在 Internet 上看到的许多“linux-ish”建议更加“兼容”。无论您使用哪种 shell(BASH、ZSH 等),通过安装这组 GNU 核心实用程序,您都可以使您的生活更加轻松。
From the GNU website:
从 GNU 网站:
Coreutils - GNU core utilities
Introduction to Coreutils
The GNU Core Utilities are the basic file, shell and text manipulation utilities of the GNU operating system. These are the core utilities which are expected to exist on every operating system.
Coreutils - GNU 核心实用程序
Coreutils 简介
GNU 核心实用程序是 GNU 操作系统的基本文件、shell 和文本操作实用程序。 这些是预期存在于每个操作系统上的核心实用程序。
The easiest way to install them on macOS is with Homebrew.
在 macOS 上安装它们的最简单方法是使用Homebrew。
$ brew install coreutils
If this gets you interested, there is also an unrelated active project called moreutils
to consider and produce moreuseful utils. I definitely consider these more optional and situational, but handy:
如果这让你感兴趣,还有一个不相关的活动项目被称为moreutils
考虑并产生更多有用的utils。我绝对认为这些更具可选性和情境性,但很方便:
$ brew install moreutils
If you are determined to find a specific solution to this problem, add the following to your ~/.bash_profile (or whichever shell you have):
如果您决心找到此问题的特定解决方案,请将以下内容添加到您的 ~/.bash_profile(或您拥有的任何 shell)中:
mydate() { [[ "" = "-I" ]] && command date "+%Y-%m-%dT%H:%M:%S%z" || command date "$@"; }
There are so many ways to do thing. Most of the assessments of them are necessarily opinionated. There is a rich history of many different tools, simple and effective, that have brought us to where we are today. I love the variety of it. It truly epitomizes the idea of 'your mileage may vary.'
有很多方法可以做事。对他们的大多数评估必然是固执己见的。许多不同工具的丰富历史,简单而有效,将我们带到了今天。我喜欢它的多样性。它真正体现了“您的里程可能会有所不同”的想法。
- ymmv
- 嗯嗯