bash 在bash中检测夏令时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19901906/
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
Detect daylight saving time in bash
提问by jhutar
I want to detect if I'm in winter or summer time. My current approach is:
我想检测我是在冬天还是夏天。我目前的做法是:
if date +%Z | grep -e CET -e EST; then
# I'm in winter time
else
# I'm in summer time
fi
which have obvious drawback as you have to know all the timezone names.
这有明显的缺点,因为您必须知道所有时区名称。
采纳答案by choroba
Perl to the rescue:
Perl 来拯救:
if perl -e 'exit ((localtime)[8])' ; then
echo winter
else
echo summer
fi
回答by UlfR
I don't now if this exactly answers your question, but it gives you some tools to help you better understand and test whats going on.
我现在不知道这是否完全回答了您的问题,但它为您提供了一些工具来帮助您更好地理解和测试正在发生的事情。
You can use date
and environment-var TZ
to help you.
您可以使用date
environment-varTZ
来帮助您。
So for example I live in Sweden so my timezone location is Europe/Stockholm. So in the winter date +%Z
reports CET and in the summer CEST. The nice thing is that you could specify timezone for the environment of a specific command, then you could specify what date the date
command should present. So by summing up this you could do any of the following:
例如,我住在瑞典,所以我的时区位置是Europe/Stockholm。所以在冬季date +%Z
报告 CET,在夏季报告 CEST。好处是您可以为特定命令的环境指定时区,然后您可以指定该date
命令应显示的日期。因此,通过总结这一点,您可以执行以下任何操作:
TZ=Europe/Stockholm date +%Z # CET or CEST depending of when its run
TZ=Europe/Stockholm date --date=20170101 +%Z # CET
TZ=Europe/Stockholm date --date=20170601 +%Z # CEST
TZ=CET date --date=20170101 +%Z # CET
TZ=CET date --date=20170601 +%Z # CEST, note that its auto adjusted to CEST
If you instead want the time difference to UTC you could use lowercase-z:
如果您想要与 UTC 的时差,您可以使用小写-z:
TZ=Europe/Stockholm date +%z # +0100 or +0200 depending of when its run
TZ=Europe/Stockholm date --date=20170101 +%z # +0100
TZ=Europe/Stockholm date --date=20170601 +%z # +0200
TZ=CET date --date=20170101 +%z # +0100
TZ=CET date --date=20170601 +%z # +0200
NOTE: You can not use TZ=CEST
or TZ=ULFR
since that is not a valid TZ:
注意:您不能使用TZ=CEST
或TZ=ULFR
因为它不是有效的 TZ:
TZ=CEST date --date=20170101 +%z # +0000
TZ=ULFR date --date=20170101 +%z # +0000
crontab example:
crontab 示例:
We run our servers on UTC but some of the jobs run by crontab needs to be run at a specified wallclock (CET/CEST) time. So since we want the jobs to be run one hour later in the winter (the clock is put one hour forward in the summer witch makes it reach a specified UTC-time one hour earlier in the summer than in the winter) we do sleep
before the actual job is executed in the winter.
我们在 UTC 上运行我们的服务器,但由 crontab 运行的一些作业需要在指定的挂钟 (CET/CEST) 时间运行。因此,由于我们希望作业在冬季晚一小时运行(夏季时钟提前一小时,使其在夏季比冬季提前一小时到达指定的 UTC 时间)我们sleep
在实际工作是在冬天执行的。
We want the job /whatever/bin/foobar
to be run at 04:15
wallclock time every day. But since cron runs on UTC the job needs to be set one hour earlier for CET and two hours earlier for CEST. That would be the same as always running the command two hours earlier but sleeping for an hour during winter-time. Ex crontab row:
我们希望作业每天/whatever/bin/foobar
在04:15
挂钟时间运行。但由于 cron 在 UTC 上运行,因此需要为 CET 提前一小时设置作业,为 CEST 提前两小时设置。这与总是提前两小时运行命令但在冬季睡一小时是一样的。Ex crontab 行:
15 2 * * * [ `TZ=CET date +\%Z` = CET ] && sleep 3600; /whatever/bin/foobar
If you have a nicer solution to this issue, then please feel free to advice me!
如果您对此问题有更好的解决方案,请随时给我建议!
回答by Toby Speight
In the northern hemisphere, in regions with daylight savings, then it's active when the offset is greater than the offset is in January. In southern hemisphere time zones, daylight savings is active when the offset is greater than that in July.
在北半球,在夏令时的地区,当偏移量大于一月份的偏移量时,它会处于活动状态。在南半球时区,当偏移量大于 7 月份时,夏令时处于活动状态。
You can discover the offsets in January and July, as well as now:
您可以在 1 月和 7 月以及现在发现偏移量:
OFF_1=$(date -d '1 Jan' +%z)
OFF_7=$(date -d '1 Jul' +%z)
OFF_NOW=$(date +%z)
If your zone doesn't have daylight savings, all three will be the same (this year). In any case, we can make two comparisons, one for the northern hemisphere and one for the southern; if the current offset is greater than either one of those, then the current zone is in daylight savings time:
如果您的区域没有夏令时,则所有三个都将相同(今年)。无论如何,我们可以做两个比较,一个是北半球,一个是南半球;如果当前偏移量大于其中之一,则当前区域处于夏令时:
if test $OFF_NOW -gt $OFF_1 || test $OFF_NOW -gt $OFF_7
then
# I'm in summer time
else
# I'm in winter time
fi
The assumption here is that all regions that observe daylight savings have their transition somewhere between July and January. That's true in all zones, as far as I know. The only case where this might fail is in a year where the winter offset changes (such 1940 in the UK, when the subsequent winter was on GMT+1).
这里的假设是所有遵守夏令时的地区都在 7 月和 1 月之间的某个时间过渡。据我所知,所有区域都是如此。唯一可能失败的情况是冬季偏移量发生变化的年份(例如英国的 1940 年,随后的冬季为 GMT+1)。