Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash—给定日期中的小时数_Linux_Bash_Date_Time_Dst - Fatal编程技术网

Linux Bash—给定日期中的小时数

Linux Bash—给定日期中的小时数,linux,bash,date,time,dst,Linux,Bash,Date,Time,Dst,在Linux上使用bashshell,并给定一个datetime,我如何确定在那个特定的一天有多少小时 datetime属于夏令时的某些时区,例如MET。10月30日是英国最后一次夏季时间变更。通过这种方式,我可以从壳牌公司获得当天的25个小时: t1=$(TZ='Europe/London' date --date='20161030' +%s) t2=$(TZ='Europe/London' date --date='20161031' +%s) echo $((($t2 - $t1) /

在Linux上使用bashshell,并给定一个datetime,我如何确定在那个特定的一天有多少小时


datetime属于夏令时的某些时区,例如MET。

10月30日是英国最后一次夏季时间变更。通过这种方式,我可以从壳牌公司获得当天的25个小时:

t1=$(TZ='Europe/London' date --date='20161030' +%s)
t2=$(TZ='Europe/London' date --date='20161031' +%s)
echo $((($t2 - $t1) / 3600))

<> P>我不完全相信这将在每个BASH shell中工作,可能需要稍微调整一下。

为了完全解释所有的场景,你需要考虑一些事情:

  • 并非每个本地日都有午夜,如果您在其中一天传递日期,
    date
    命令将失败,除非您同时传递时间和UTC的偏移量。这主要发生在春季提前过渡日。例如:

    $ TZ=America/Sao_Paulo date -d '2016-10-16'
    date: invalid date '2016-10-16'
    
  • 并非每个DST转换都是1小时<代码>美国/洛德豪切换30分钟。Bash只执行整数除法,所以如果需要小数,就必须使用

以下是一个函数,用于解释以下内容:

seconds_in_day() {
  # Copy input date to local variable
  date=$1

  # Start with the offset at noon on the given date.
  # Noon will almost always exist (except Samoa on 2011-12-30)
  offset1=$(date -d "$date 12:00" +%z)

  # Next get the offset for midnight.  If it doesn't exist, the time will jump back to 23:00 and we'll get a different offset.
  offset1=$(date -d "$date 00:00 $offset1" +%z)

  # Next get the offset for the next day at midnight.  Again, if it doesn't exist, it will jump back an hour.
  offset2=$(date -d "$date 00:00 $offset1 + 1 day" +%z)

  # Get the unix timestamps for both the current date and the next one, at midnight with their respective offsets.
  unixtime1=$(date -d "$date 00:00 $offset1" +%s)
  unixtime2=$(date -d "$date 00:00 $offset2 + 1 day" +%s)

  # Calculate the difference in seconds and hours.  Use awk for decimal math.
  seconds=$((unixtime2 - unixtime1))
  hours=$(awk -v seconds=$seconds 'BEGIN { print seconds / 3600 }')

  # Print the output
  echo "$date had $seconds secs in $TZ, or $hours hours."
}
示例:

$ TZ=America/Los_Angeles seconds_in_day 2016-03-12
2016-03-12 had 86400 secs in America/Los_Angeles, or 24 hours.
$ TZ=America/Los_Angeles seconds_in_day 2016-03-13
2016-03-13 had 82800 secs in America/Los_Angeles, or 23 hours.
$ TZ=America/Los_Angeles seconds_in_day 2016-03-14
2016-03-14 had 86400 secs in America/Los_Angeles, or 24 hours.

$ TZ=America/Los_Angeles seconds_in_day 2016-11-05
2016-11-05 had 86400 secs in America/Los_Angeles, or 24 hours.
$ TZ=America/Los_Angeles seconds_in_day 2016-11-06
2016-11-06 had 90000 secs in America/Los_Angeles, or 25 hours.
$ TZ=America/Los_Angeles seconds_in_day 2016-11-07
2016-11-07 had 86400 secs in America/Los_Angeles, or 24 hours.

$ TZ=America/Sao_Paulo seconds_in_day 2016-02-19
2016-02-19 had 86400 secs in America/Sao_Paulo, or 24 hours.
$ TZ=America/Sao_Paulo seconds_in_day 2016-02-20
2016-02-20 had 90000 secs in America/Sao_Paulo, or 25 hours.
$ TZ=America/Sao_Paulo seconds_in_day 2016-02-21
2016-02-21 had 86400 secs in America/Sao_Paulo, or 24 hours.

$ TZ=America/Sao_Paulo seconds_in_day 2016-10-15
2016-10-15 had 86400 secs in America/Sao_Paulo, or 24 hours.
$ TZ=America/Sao_Paulo seconds_in_day 2016-10-16
2016-10-16 had 82800 secs in America/Sao_Paulo, or 23 hours.
$ TZ=America/Sao_Paulo seconds_in_day 2016-10-17
2016-10-17 had 86400 secs in America/Sao_Paulo, or 24 hours.

$ TZ=Australia/Lord_Howe seconds_in_day 2016-04-02
2016-04-02 had 86400 secs in Australia/Lord_Howe, or 24 hours.
$ TZ=Australia/Lord_Howe seconds_in_day 2016-04-03
2016-04-03 had 88200 secs in Australia/Lord_Howe, or 24.5 hours.
$ TZ=Australia/Lord_Howe seconds_in_day 2016-04-04
2016-04-04 had 86400 secs in Australia/Lord_Howe, or 24 hours.

$ TZ=Australia/Lord_Howe seconds_in_day 2016-10-01
2016-10-01 had 86400 secs in Australia/Lord_Howe, or 24 hours.
$ TZ=Australia/Lord_Howe seconds_in_day 2016-10-02
2016-10-02 had 84600 secs in Australia/Lord_Howe, or 23.5 hours.
$ TZ=Australia/Lord_Howe seconds_in_day 2016-10-03
2016-10-03 had 86400 secs in Australia/Lord_Howe, or 24 hours.

你需要知道现在几点吗?就像
date
命令一样?你在问什么?如果一天有23小时、24小时或25小时?@Jdamian:事实上,我需要知道是23小时、24小时还是25小时,还是澳大利亚/洛德·豪的23.5小时或24.5小时。