落書きノート

ふと自分が気になった事を書いてます

Ruby 練習問題 TimeクラスとDateクラス ラスト

最後の練習問題です。後、Rubyの資格の問題集も残ってるのでこれをやります。

require "time"

def jparsedate(target)
  now = Time.now
  year = now.year
  month = now.month
  day = now.day
  hour = now.hour
  min = now.min
  sec = now.sec
  target.scan(/(午前|午後)?(\d+)(|||||)/){
    case $3
    when ""; year = $2.to_i
    when ""; month = $2.to_i
    when ""; day = $2.to_i
    when ""; hour = $2.to_i
      hour += 12 if $1 == "午後"
    when ""; min = $2.to_i
    when ""; sec = $2.to_i
    end
  }
  return Time.mktime(year, month, day, hour, min, sec)
end

date = "2006年12月23日午後8時17分50秒"

puts jparsedate(date)
def ls_t(path)
  entries = Dir.entries(path)
  entries.reject!{|name| /^\./ =~ name}

  mtimes = Hash.new
  entries = entries.sort_by{|name|
    mtimes[name] = File.mtime(File.join(path, name))
  }

  entries.each{|name|
    printf("%-40s %s\n", name, mtimes[name])
  }

rescue => ex
  puts ex.message
end

ls_t(ARGV[0] || ".")
require "date"

module Calendar
  WEEK_TABLE = [
    [99, 99, 99, 99, 99, 99,  1,  2,  3,  4,  5,  6,  7],
    [ 2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14],
    [ 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
    [16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28],
    [23, 24, 25, 26, 27, 28, 29, 30, 31, 99, 99, 99, 99],
    [30, 31, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99],
  ]

  module_function

  def cal(year, month)
    first = Date.new(year, month, 1)
    end_of_month = ((first >> 1) - 1).day
    start = 6 - first.wday

    puts first.strftime("%B %Y").center(21)
    puts " Su Mo Tu We Th Fr St"
    WEEK_TABLE.each{|week|
      buf = ""
      week[start, 7].each{|day|
        if day > end_of_month
          buf << "   "
        else
          buf << sprintf("%3d", day)
        end
      }
      puts buf
    }
  end
end

t = Date.today
Calendar.cal(t.year, t.month)