转换时区并减去 x 时间量

convert time zone and minus x amount of time

我一直在努力使以下内容正常工作,但我似乎总是遇到某种错误..

  1. 从用户输入中获取时间
  2. 使用上述时间作为变量
  3. 将所述时间转换为 JST
  4. 从时间中减去 $duration(分钟)以给出新的时间。

这将类似于下面的代码行。

#!/bin/sh

read -p "Please enter hour: " hour
read -p "Please enter minute: " minute
read -p "Please enter duration: " duration

jptime=$(TZ=JST date --date $hour$minute)

newtime=$(date -d "$jptime" "-$duration minutes")

echo "$newtime"

我会使用具有良好日期时间库的语言。例如,perl

perl -MDateTime -E '
  ($hour, $minute, $duration) = @ARGV; 
  $fmt = "%F %T %Z";
  $local = DateTime->now(time_zone=>"local")->set(hour=>$hour, minute=>$minute, second=>0);
  say $local->strftime($fmt);
  $jp = $local->clone->set_time_zone("Asia/Tokyo"); 
  say $jp->strftime($fmt); 
  $jp2 = $jp->subtract(minutes => $duration); 
  say $jp2->strftime($fmt);
' 8 0 45
2015-01-17 08:00:00 EST
2015-01-17 22:00:00 JST
2015-01-17 21:15:00 JST

或 Tcl

hour=8
minute=0
duration=45
export hour minute duration
tclsh <<'END'
  set fmt "%Y-%m-%d %T %Z"
  set t [clock scan "$env(hour):$env(minute) today"]
  puts [clock format $t -format $fmt]
  puts [clock format $t -format $fmt -timezone "Asia/Tokyo"]
  puts [clock format [clock add $t -$env(duration) minutes] -format $fmt -timezone "Asia/Tokyo"]
END
2015-01-17 08:00:00 EST
2015-01-17 22:00:00 JST
2015-01-17 21:15:00 JST

根据您的评论:

 perl -MDateTime -E '
    ($hour, $minute, $duration) = @ARGV;
    say uc DateTime->now(time_zone=>"local")
                   ->set(hour=>$hour, minute=>$minute, second=>0)
                   ->set_time_zone("Asia/Tokyo")
                   ->subtract(minutes => $duration)
                   ->strftime("%a %H:%M");
' 8 0 45 >| output.file