#! /usr/bin/env ruby # coding: utf-8 require "date" require "pp" require "optparse" ## option analysis OPTIONS = {} op = OptionParser.new op.on("-t time", "--time=val", "Time indication."){|v| OPTIONS[:time] = v} op.on("-s string", "--string=val", "String just after DateTime."){|v| OPTIONS[:string] = v} op.on("-i num", "--indent=num", "Indicate number of spaces at the beginning."){|v| OPTIONS[:indent] = v.to_i} op.parse!(ARGV) OPTIONS[:time] ||= "" OPTIONS[:string] ||= "" OPTIONS[:indent] ||= 0 WEEKDAYS = %w(Sun Mon Tue Wed Thu Fri Sat) TODAY = DateTime.now unless ARGV.size == 2 puts "USAGE: dates from_date to_date" puts " e.g., dates 2012-01-26 2012-02-29" puts " e.g., dates 1-26 2-29" puts " e.g., dates '' 29" puts " e.g., dates 2012-01-26 2012-02-29 -t 12:30" puts " e.g., dates 2012-01-26 2012-02-29 -s '@ string'" puts " e.g., dates 2012-01-26 2012-02-29 -i 2" exit end def analyze_date(str) nums = str.split(/[\-\/]/).map{|i| i.to_i} nums = [TODAY.day ] if nums.size == 0 nums = [TODAY.month, *nums] if nums.size == 1 nums = [TODAY.year, *nums] if nums.size == 2 Date.new(* nums) end from_date = analyze_date(ARGV[0]) to_date = analyze_date(ARGV[1]) if to_date < from_date puts "Error: from_date must be earlier than to_date" exit end #p from_date, to_date current = from_date while (current <= to_date) str = " " * OPTIONS[:indent] str += current.strftime("[%Y-%m-%d #{OPTIONS[:time]} (#{WEEKDAYS[current.wday]})]#{OPTIONS[:string]}").sub(" ", " ") puts str current += 1 end