bash 日期命令不遵循 Linux 规范 (Mac OS X Lion)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9804966/
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
Date command does not follow Linux specifications (Mac OS X Lion)
提问by Kaushik Shankar
I have been developing a script on my linux box for quite some time, and wanted to run it on my Mac as well.
一段时间以来,我一直在我的 linux 机器上开发一个脚本,并希望在我的 Mac 上运行它。
I thought that the functions on the Mac were the same as the functions on linux, but today I realized it was wrong. I knew that fewer functions existed on the Mac, but I thought that the functions that did exist, had the same implementation.
本来以为Mac上的功能和linux上的功能一样,今天发现不对。我知道 Mac 上存在的功能较少,但我认为确实存在的功能具有相同的实现。
This problem is specifically in regards to the date
command.
这个问题特别与date
命令有关。
When I run the command on my linux machine with the parameter to provide some time in nanoseconds, I get the correct result, but when I run it on my mac, it does not have that option.
当我在我的 linux 机器上运行带有参数以提供一些纳秒时间的命令时,我得到了正确的结果,但是当我在我的 mac 上运行它时,它没有那个选项。
Linux-Machine> date +%N
55555555555 #Current time in nanoseconds
Mac-Machine> date +%N
N
How do I go about getting the current time in nanoseconds as a bash command on the Mac?
如何在 Mac 上以 bash 命令的形式获取以纳秒为单位的当前时间?
Worst case is I create a small piece of code that calls a system function in C or something and then call it within my script.
最坏的情况是我创建了一小段代码,在 C 或其他东西中调用系统函数,然后在我的脚本中调用它。
Any help is much appreciated!
任何帮助深表感谢!
回答by JoeLinux
This is because OSX and Linux use two different sets of tools. Linux uses the GNU version of the date
command (hence, GNU/Linux). Remember that Linux is Linux and OS X is Unix. They're different.
这是因为 OSX 和 Linux 使用两组不同的工具。Linux 使用该date
命令的 GNU 版本(因此称为 GNU/Linux)。请记住,Linux 是 Linux,而 OS X 是 Unix。他们是不同的。
You can install the GNU date
command which is included in the "coreutils" package from MacPorts. It will be installed on your system as gdate
. You can either use that, or link the date
binary with the new gdate
binary; your choice.
您可以date
从MacPorts安装包含在“coreutils”包中的 GNU命令。它将作为gdate
. 您可以使用它,也可以将date
二进制文件与新的gdate
二进制文件链接起来;你的选择。
回答by callumacrae
man date
indicates that it doesn't go beyond one second. I would recommend trying another language (Python 2):
man date
表示它不会超过一秒。我建议尝试另一种语言(Python 2):
$ python -c 'import time; print repr(time.time())'
1332334298.898616
For Python 3, use:
对于 Python 3,请使用:
$ python -c 'import time; print(repr(time.time()))'
回答by tripleee
There are "Linux specifications" but they do not regulate the behavior of the date
command much. What you have is really the opposite -- Linux (or more specifically the GNU user-space tools) has a large number of extensions which are not compatible with Unixby any reasonable definition.
有“Linux 规范”,但它们并没有date
过多地规范命令的行为。你所拥有的恰恰相反——Linux(或者更具体地说是 GNU 用户空间工具)有大量的扩展,根据任何合理的定义,这些扩展与Unix不兼容。
There is a large number of standards which doregulate these things. The one you should be looking at is POSIXwhich requires
有大量标准确实规范了这些事情。您应该查看的是POSIX,它需要
date [-u] [+format]
and nothing more to be supported by adhering implementations. (There are other standards like XPG and SUS which you might want to look at as well, but at the very least, you should require and expect POSIX these days ... finally.)
并没有更多的东西可以通过坚持实现来支持。(还有其他标准,如 XPG 和 SUS,您可能也想查看,但至少,这些天您应该要求并期待 POSIX ......终于。)
The POSIX document contains a number of examples but there is nothing for date conversionwhich is however a practical problem which many scripts turn to date
for. Also, for your concrete problem, there is nothing for reporting times with sub-second accuracy in POSIX.
POSIX 文档包含许多示例,但没有用于日期转换的内容,但这是许多脚本转向的实际问题date
。此外,对于您的具体问题,在 POSIX 中没有任何用于以亚秒级精度报告时间的内容。
Anyway, griping that *BSD isn't Linux isn't really helpful here; you just have to understand what the differences are, and code defensively. If your requirements are complex or unusual, perhaps turn to a scripting language like Perl or Python which perform these types of date formatting operations more or less out of the box in a standard installation (though neither Perl nor Python have a quick and elegant way to do date conversionout of the box, either; solutions tend to be somewhat tortured).
无论如何,在这里抱怨 *BSD 不是 Linux 并没有什么帮助;你只需要了解差异是什么,并防御性地编码。如果您的需求很复杂或不寻常,也许可以求助于像 Perl 或 Python 这样的脚本语言,它们在标准安装中或多或少地开箱即用地执行这些类型的日期格式化操作(尽管 Perl 和 Python 都没有一种快速而优雅的方法来执行)也可以开箱即用地进行日期转换;解决方案往往会受到一些折磨)。
In practical terms, you can compare the MacOS date
man pageand the Linux oneand try to reconcile your requirements.
实际上,您可以比较MacOSdate
手册页和Linux手册页并尝试调和您的要求。
For your practical requirement, MacOS date
does not support any format string with nanosecond accuracy, but nor are you likely to receive useful results on that scale when the execution of the command will take a significant number of nanoseconds. I would settle for millisecond-level accuracy (and even that is going to be thrown off by the execution time in the final digits) and multiply to get the number in nanosecond scale.
对于您的实际需求,MacOSdate
不支持任何具有纳秒精度的格式字符串,但是当命令的执行将花费大量纳秒时,您也不太可能收到该规模的有用结果。我会满足于毫秒级的精度(即使最后一位数的执行时间也会忽略这一点)并乘以获得纳秒级的数字。
nanoseconds () {
python -c 'import time; print(int(time.time()*1000*1000*1000))'
}
(Notice the parentheses around the argument to print()
for Python 3.) You will notice that Python doesreport a value at nanosecond accuracy (the last digits are often not zeros), though by the time you have run time.time()
the value will obviously no longer be correct.
(注意print()
Python 3的参数周围的括号。)您会注意到 Python确实以纳秒精度报告了一个值(最后一位数字通常不是零),但到您运行时,time.time()
该值显然不再正确.
To get an idea of the error rate,
要了解错误率,
bash@macos-high-sierra$ python3
Python 3.5.1 (default, Dec 26 2015, 18:08:53)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> import timeit
>>> def nanoseconds ():
... return int(time.time()*1000*1000*1000)
...
>>> timeit.timeit(nanoseconds, number=10000)
0.0066173350023746025
>>> timeit.timeit('int(time.time()*1000*1000*1000)', number=10000)
0.00557799199668807
The overhead of startingPython and printing the value is probably going to add a few orders of magnitude of overhead, realistically, but I haven't attempted to quantify that. (The output from timeit
is in seconds.)
实际上,启动Python 和打印值的开销可能会增加几个数量级的开销,但我还没有尝试对此进行量化。(输出timeit
以秒为单位。)