weeks
weeks
は、指定した日付から特定の曜日の日付を10週間分表示するRubyスクリプトです。
使い方
./weeks YYYY-MM-DD Wday
YYYY-MM-DD
: 検索を開始する日付を年-月-日
の形式で指定します。Wday
: 検索する曜日を英語の略称(例:Sun
,Mon
,Tue
,Wed
,Thu
,Fri
,Sat
)で指定します。
例
2023年5月15日から始まる金曜日を10週間分表示するには:
./weeks 2023-05-15 Fri
その他の機能
ヘルプの表示
引数が正しくない場合や不足している場合は、使い方のメッセージと今年のカレンダーが表示されます。
./weeks
# または
./weeks 2023-05-15
スクリプトの編集
edit
引数を付けて実行すると、スクリプトをエディタで開けます。これにより、スクリプトのパスを覚えていなくても簡単に修正できます。
./weeks edit
この機能は、環境変数EDITOR
に設定されているエディタを使用します。
仕組み
このスクリプトは以下の手順で動作します:
- 引数の解析: コマンドライン引数から開始日とターゲットの曜日を取得します。
- 曜日のマッピング: 英語の曜日略称を数値(日曜日: 0, 月曜日: 1, ... 土曜日: 6)に変換します。
- 最初のターゲット日を見つける: 指定された開始日から、最初のターゲットの曜日になるまで日付を進めます。
- 10週間分の表示: 見つかった最初のターゲットの日付から、7日ずつ日付を進めながら10回日付を表示します。
必要要件
- Rubyがインストールされていること。
作成の経緯
weeks
#!/usr/bin/env ruby
# https://gist.github.com/hyuki/c45e62afd5e11f4224fb4be535fdcc16/
require 'pp'
require 'date'
APPNAME = 'weeks'
if ARGV.length == 1 && ARGV[0] == 'edit'
system("#{ENV['EDITOR']} ~/bin/weeks")
exit
end
if ARGV.length != 2
puts "Usage: #{APPNAME} YYYY-MM-DD Wday"
puts "Example: #{APPNAME} 2023-05-15 Fri"
current_year = Time.now.year.to_s
system("cal #{current_year}")
abort
end
start_date = Date.parse(ARGV[0]) # start date from command line argument
target_day = ARGV[1] # target day from command line argument
# Map the days of the week to corresponding wday values
day_to_num = {
"Sun" => 0,
"Mon" => 1,
"Tue" => 2,
"Wed" => 3,
"Thu" => 4,
"Fri" => 5,
"Sat" => 6
}
target_num = day_to_num[target_day]
# Find the first target day
until start_date.wday == target_num
start_date = start_date.next_day
end
# Print the next 10 weeks of the target day
10.times do
puts start_date
start_date = start_date.next_day(7) # Jump to the next week
end
(2025年5月24日)