summaryrefslogtreecommitdiff
path: root/sample/cal.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
commit65a5162550f58047974793cdc8067a970b2435c0 (patch)
tree082bb7d5568f3b2e36e3fe166e9f3039394fcf44 /sample/cal.rb
parentfcd020c83028f5610d382e85a2df00223e12bd7e (diff)
1.4.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'sample/cal.rb')
-rw-r--r--sample/cal.rb119
1 files changed, 119 insertions, 0 deletions
diff --git a/sample/cal.rb b/sample/cal.rb
new file mode 100644
index 0000000..cf82c64
--- /dev/null
+++ b/sample/cal.rb
@@ -0,0 +1,119 @@
+#! /usr/local/bin/ruby
+
+# cal.rb: Written by Tadayoshi Funaba 1998, 1999
+# $Id: cal.rb,v 1.5 1999/08/04 14:54:18 tadf Exp $
+
+require 'date2'
+
+$tab =
+{
+ 'cn' => true, # China
+ 'de' => 2342032, # Germany (protestant states)
+ 'dk' => 2342032, # Denmark
+ 'es' => 2299161, # Spain
+ 'fi' => 2361390, # Finland
+ 'fr' => 2299227, # France
+ 'gb' => 2361222, # United Kingdom
+ 'gr' => 2423868, # Greece
+ 'hu' => 2301004, # Hungary
+ 'it' => 2299161, # Italy
+ 'jp' => true, # Japan
+ 'no' => 2342032, # Norway
+ 'pl' => 2299161, # Poland
+ 'pt' => 2299161, # Portugal
+ 'ru' => 2421639, # Russia
+ 'se' => 2361390, # Sweden
+ 'us' => 2361222, # United States
+ 'os' => false, # (old style)
+ 'ns' => true # (new style)
+}
+
+$cc = 'gb'
+
+def usage
+ $stderr.puts 'usage: cal [-c iso3166] [-jy] [[month] year]'
+ exit 1
+end
+
+def cal(m, y, sg)
+ for d in 1..31
+ break if jd = Date.exist?(y, m, d, sg)
+ end
+ fst = cur = Date.new(jd, sg)
+ ti = Date::MONTHNAMES[m]
+ ti << ' ' << y.to_s unless $yr
+ mo = ti.center((($w + 1) * 7) - 1) << "\n"
+ mo << ['S', 'M', 'Tu', 'W', 'Th', 'F', 'S'].
+ collect{|x| x.rjust($w)}.join(' ') << "\n"
+ mo << ' ' * (($w + 1) * fst.wday)
+ while cur.mon == fst.mon
+ mo << (if $jd then cur.yday else cur.mday end).to_s.rjust($w)
+ mo << (if (cur += 1).wday != 0 then "\s" else "\n" end)
+ end
+ mo << "\n" * (6 - ((fst.wday + (cur - fst)) / 7))
+ mo
+end
+
+def zip(xs)
+ yr = ''
+ until xs.empty?
+ ln = (if $jd then l, r, *xs = xs; [l, r]
+ else l, c, r, *xs = xs; [l, c, r] end).
+ collect{|x| x.split(/\n/no, -1)}
+ 8.times do
+ yr << ln.collect{|x|
+ x.shift.ljust((($w + 1) * 7) - 1)}.join(' ') << "\n"
+ end
+ end
+ yr
+end
+
+while /^-([^-].*)$/no =~ $*[0]
+ a = $1
+ if /^c(.+)?$/no =~ a
+ if $1
+ $cc = $1.downcase
+ elsif $*.length >= 2
+ $cc = $*[1].downcase
+ $*.shift
+ else
+ usage
+ end
+ else
+ a.scan(/./no) do |c|
+ case c
+ when 'j'; $jd = true
+ when 'y'; $yr = true
+ else usage
+ end
+ end
+ end
+ $*.shift
+end
+$*.shift if /^--/no =~ $*[0]
+usage if (sg = $tab[$cc]).nil?
+case $*.length
+when 0
+ td = Date.today
+ m = td.mon
+ y = td.year
+when 1
+ y = $*[0].to_i
+ $yr = true
+when 2
+ m = $*[0].to_i
+ y = $*[1].to_i
+else
+ usage
+end
+usage unless m.nil? or (1..12) === m
+usage unless y >= -4712
+$w = if $jd then 3 else 2 end
+unless $yr
+ print cal(m, y, sg)
+else
+ print y.to_s.center(((($w + 1) * 7) - 1) *
+ (if $jd then 2 else 3 end) +
+ (if $jd then 2 else 4 end)), "\n\n",
+ zip((1..12).collect{|m| cal(m, y, sg)}), "\n"
+end