Newer
Older
Ruby / time-signal.rb
#!/usr/bin/env ruby
# coding: utf-8

# RubyではTime.nowで現在時刻を得ることができる。
# 得た値に秒数を足したり引いたりするとその時間だけずらした時刻が得られる。
# 参考:
# https://qiita.com/prgseek/items/c0fc2ffc8e1736348486
# https://docs.ruby-lang.org/ja/latest/class/Time.html

y = Time.now.year

#def play(sound)
#spawn "ogg123 -q "+sound
#end
def next1sec(sec)
  while sec == Time.now.sec
    sleep 0.03		# 秒が変わるまで待機する
  end
  Time.now.sec		# 新しい秒を返しておく(次に使う)
end

while y == Time.now.year # 年が同じ間繰り返す
  now = Time.now
  sec = now.sec
  just = now+60		# 1分後
  jh = just.hour	# 1分後の時
  jm = just.min		# 1分後の分
  #while Time.now.min != jm
    sec = next1sec(sec)	# 秒の区切りまで待つ
    printf("%s\r", Time.now)	# \r は行頭に戻る
  #  if sec%30 == 0	# 30で割り切れる0秒か30秒なら
      #play("po.ogg")	# 「ポーン♪」
   # elsif sec%30 == 20	# 余りが20なら30秒区切りの10秒前
      #play("kotsu.ogg")	
    #  if sec < 30	# 何時何分30秒 の予告
        #printf("只今から%d時%d分30秒をお知らせします\n", now.hour, now.min)
     # else		# 次の分の予告
       # printf("只今から%d時%d分ちょうどをお知らせします\n", jh, jm)
        #end
    #elsif sec%30 > 26	# 3秒前の「プ」
      #play("pu.ogg")
    #else
     # play("kotsu.ogg")	# 特に何もないときは「コツ」
   # end
 # end
end

puts("おめでとう!")