home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / rbemx144.zip / ruby-1.4.4 / sample / cal.rb < prev    next >
Text File  |  1999-09-16  |  3KB  |  120 lines

  1. #! /usr/local/bin/ruby
  2.  
  3. # cal.rb: Written by Tadayoshi Funaba 1998, 1999
  4. # $Id: cal.rb,v 1.6 1999/09/15 05:35:25 tadf Exp $
  5.  
  6. require 'date2'
  7.  
  8. $tab =
  9. {
  10.   'cn' => true,    # China
  11.   'de' => 2342032, # Germany (protestant states)
  12.   'dk' => 2342032, # Denmark
  13.   'es' => 2299161, # Spain
  14.   'fi' => 2361390, # Finland
  15.   'fr' => 2299227, # France
  16.   'gb' => 2361222, # United Kingdom
  17.   'gr' => 2423868, # Greece
  18.   'hu' => 2301004, # Hungary
  19.   'it' => 2299161, # Italy
  20.   'jp' => true,    # Japan
  21.   'no' => 2342032, # Norway
  22.   'pl' => 2299161, # Poland
  23.   'pt' => 2299161, # Portugal
  24.   'ru' => 2421639, # Russia
  25.   'se' => 2361390, # Sweden
  26.   'us' => 2361222, # United States
  27.   'os' => false,   # (old style)
  28.   'ns' => true     # (new style)
  29. }
  30.  
  31. $cc = 'gb'
  32.  
  33. def usage
  34.   $stderr.puts 'usage: cal [-c iso3166] [-jy] [[month] year]'
  35.   exit 1
  36. end
  37.  
  38. def cal(m, y, sg)
  39.   for d in 1..31
  40.     break if jd = Date.exist?(y, m, d, sg)
  41.   end
  42.   fst = cur = Date.new1(jd, sg)
  43.   ti = Date::MONTHNAMES[m]
  44.   ti << ' ' << y.to_s unless $yr
  45.   mo = ti.center((($w + 1) * 7) - 1) << "\n"
  46.   mo << ['S', 'M', 'Tu', 'W', 'Th', 'F', 'S'].
  47.     collect{|x| x.rjust($w)}.join(' ') << "\n"
  48.   mo << ' ' * (($w + 1) * fst.wday)
  49.   while cur.mon == fst.mon
  50.     mo << (if $jd then cur.yday else cur.mday end).to_s.rjust($w)
  51.     mo << (if (cur += 1).wday != 0 then "\s" else "\n" end)
  52.   end
  53.   mo << "\n" * (6 - ((fst.wday + (cur - fst)) / 7))
  54.   mo
  55. end
  56.  
  57. def zip(xs)
  58.   yr = ''
  59.   until xs.empty?
  60.     ln = (if $jd then l,    r, *xs = xs; [l,    r]
  61.          else l, c, r, *xs = xs; [l, c, r] end).
  62.       collect{|x| x.split(/\n/no, -1)}
  63.     8.times do
  64.       yr << ln.collect{|x|
  65.     x.shift.ljust((($w + 1) * 7) - 1)}.join('  ') << "\n"
  66.     end
  67.   end
  68.   yr
  69. end
  70.  
  71. while /^-([^-].*)$/no =~ $*[0]
  72.   a = $1
  73.   if /^c(.+)?$/no =~ a
  74.     if $1
  75.       $cc = $1.downcase
  76.     elsif $*.length >= 2
  77.       $cc = $*[1].downcase
  78.       $*.shift
  79.     else
  80.       usage
  81.     end
  82.   else
  83.     a.scan(/./no) do |c|
  84.       case c
  85.       when 'j'; $jd = true
  86.       when 'y'; $yr = true
  87.       else usage
  88.       end
  89.     end
  90.   end
  91.   $*.shift
  92. end
  93. $*.shift if /^--/no =~ $*[0]
  94. usage if (sg = $tab[$cc]).nil?
  95. case $*.length
  96. when 0
  97.   td = Date.today
  98.   m = td.mon
  99.   y = td.year
  100. when 1
  101.   y = $*[0].to_i
  102.   $yr = true
  103. when 2
  104.   m = $*[0].to_i
  105.   y = $*[1].to_i
  106. else
  107.   usage
  108. end
  109. usage unless m.nil? or (1..12) === m
  110. usage unless y >= -4712
  111. $w = if $jd then 3 else 2 end
  112. unless $yr
  113.   print cal(m, y, sg)
  114. else
  115.   print y.to_s.center(((($w + 1) * 7) - 1) *
  116.               (if $jd then 2 else 3 end) +
  117.               (if $jd then 2 else 4 end)), "\n\n",
  118.     zip((1..12).collect{|m| cal(m, y, sg)}), "\n"
  119. end
  120.