Linux 如何在 MacOS 上的 shell 脚本中将 DATE 转换为 UNIX TIMESTAMP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3817750/
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
How to convert DATE to UNIX TIMESTAMP in shell script on MacOS
提问by David Young
On Linux you can convert a date like "2010-10-02" to a unix timestamp in shell script by
在 Linux 上,您可以通过以下方式将“2010-10-02”之类的日期转换为 shell 脚本中的 unix 时间戳
date -d "2010-10-02" "+%s"
Since Mac OS does not have the equivalent -dfor date. How do you go about converting a date to a unix timestamp in a shell script.
因为Mac OS不具备相当于-d的日期。您如何在 shell 脚本中将日期转换为 unix 时间戳。
采纳答案by Lou Franco
man date
on OSX has this example
man date
在 OSX 上有这个例子
date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Which I think does what you want.
我认为这可以满足您的需求。
You can use this for a specific date
您可以将其用于特定日期
date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
Or use whatever format you want.
或者使用您想要的任何格式。
回答by Lou Franco
date -j -f "%Y-%m-%d" "2010-10-02" "+%s"
date -j -f "%Y-%m-%d" "2010-10-02" "+%s"
回答by JRiggles
date +%s
This works fine for me on OS X Lion.
这在 OS X Lion 上对我来说很好用。
回答by smarisetti
I used the following on Mac OSX.
我在 Mac OSX 上使用了以下内容。
currDate=`date +%Y%m%d`
epochDate=$(date -j -f "%Y%m%d" "${currDate}" "+%s")
回答by Scott Prive
Alternatively you can install GNU date like so:
或者,您可以像这样安装 GNU date:
- install Homebrew: https://brew.sh/
brew install coreutils
- add to your bash_profile:
alias date="/usr/local/bin/gdate"
date +%s
1547838127
- 安装 Homebrew:https: //brew.sh/
brew install coreutils
- 添加到您的 bash_profile:
alias date="/usr/local/bin/gdate"
date +%s
1547838127
Comments saying Mac has to be "different" simply reveal the commenter is ignorant of the history of UNIX. macOS is based on BSD UNIX, which is way older than Linux. Linux essentially was a copy of other UNIX systems, and Linux decided to be "different" by adopting GNU tools instead of BSD tools. GNU tools are more user friendly, but they're not usually found on any *BSD system (just the way it is).
说 Mac 必须“与众不同”的评论只是表明评论者对 UNIX 的历史一无所知。macOS 基于 BSD UNIX,它比 Linux 古老得多。Linux 本质上是其他 UNIX 系统的副本,Linux 决定通过采用 GNU 工具而不是 BSD 工具来“与众不同”。GNU 工具对用户更友好,但它们通常不会在任何 *BSD 系统上找到(只是它的方式)。
Really, if you spend most of your time in Linux, but have a Mac desktop, you probablywant to make the Mac work like Linux. There's no sense in trying to remember two different sets of options, or scripting for the mac's BSD version of Bash, unless you are writing a utility that you want to run on both BSD and GNU/Linux shells.
真的,如果您将大部分时间都花在 Linux 上,但拥有 Mac 桌面,那么您可能想让 Mac 像 Linux 一样工作。尝试记住两组不同的选项或为 mac 的 BSD 版本的 Bash 编写脚本是没有意义的,除非您正在编写要在 BSD 和 GNU/Linux shell 上运行的实用程序。
回答by vanyo
date -j -f "%Y-%m-%d %H:%M:%S" "2020-04-07 00:00:00" "+%s"
date -j -f "%Y-%m-%d %H:%M:%S" "2020-04-07 00:00:00" "+%s"
It will print the dynamic seconds when without %H:%M:%S
and 00:00:00
.
没有%H:%M:%S
和时,它将打印动态秒数00:00:00
。