转换时区和减去x的时间量

时间:2015-01-17 05:26:28

标签: linux bash shell time

我一直试图让以下工作起作用,但我似乎总是遇到某种错误..

  1. 从用户输入中获取时间
  2. 将所述时间用作变量
  3. 将所述时间转换为JST
  4. 从时间上拿走$ duration(分钟)给予新的时间。
  5. 这将是下面的代码行。

    #!/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"
    

1 个答案:

答案 0 :(得分:0)

我使用具有良好日期时间库的语言。例如,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