[英]Using date utility on Linux and AIX
To verify a valid date I use the command line below: 要验证有效日期,请使用以下命令行:
date -d "${DATE}" "+%Y/%m/%d" > /dev/null 2>&1
It works perfectly under Cygwin, but not in my test environment. 它可以在Cygwin下完美运行,但不能在我的测试环境中运行。 Here's some information about my test environment: 以下是有关我的测试环境的信息:
uname -a
AIX 152101a07e 3 5 00CDE2314C00
Using the command line: 使用命令行:
echo $?
The result is always 1, even if the date is valid. 结果始终为1,即使日期有效。
Do you have any idea why this test fails? 您知道为什么该测试失败吗?
Apparently, your two environments are not running the same version of date
. 显然,您的两个环境没有运行相同版本的date
。 You should check the man
page for date
in your new environment (run man date
). 您应该检查的man
的页面date
在新的环境中(运行man date
)。 On my machine (OS X), the -d
option is used for setting daylight savings time. 在我的机器(OS X)上, -d
选项用于设置夏令时。
For example, the following works for me : 例如,以下内容对我有用 :
date -j -f "%Y/%m/%d" "2014/07/09" > /dev/null 2>&1
echo $? # 0
date -j -f "%Y/%m/%d" "2014.07.09" > /dev/null 2>&1
echo $? # 1
-j
tells it to not try to set my system date. -j
告诉它不要尝试设置系统日期。 -f
means to use the specified format. -f
表示使用指定的格式。
Yours may be different, but you can find out the details by reading the manual. 您的情况可能有所不同,但是您可以通过阅读手册了解详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.