2000-07-10 08:49:24 +04:00
|
|
|
#! /usr/bin/env ruby
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-05-18 16:41:51 +04:00
|
|
|
# cal.rb: Written by Tadayoshi Funaba 1998-2002
|
2002-06-22 19:26:58 +04:00
|
|
|
# $Id: cal.rb,v 2.4 2002-06-08 00:40:29+09 tadf Exp $
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-05-18 16:41:51 +04:00
|
|
|
require 'date'
|
2000-05-24 08:34:26 +04:00
|
|
|
require 'getopts'
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
$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
|
2000-05-24 08:34:26 +04:00
|
|
|
$stderr.puts 'usage: cal [-c iso3166] [-jmty] [[month] year]'
|
1999-08-13 09:45:20 +04:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
def pict(y, m, sg)
|
2002-06-22 19:26:58 +04:00
|
|
|
d = (1..31).detect{|d| Date.valid_date?(y, m, d, sg)}
|
2000-07-18 10:00:45 +04:00
|
|
|
fi = Date.new(y, m, d, sg)
|
2000-05-24 08:34:26 +04:00
|
|
|
fi -= (fi.jd - $k + 1) % 7
|
|
|
|
|
|
|
|
ve = (fi..fi + 6).collect{|cu|
|
|
|
|
%w(S M Tu W Th F S)[cu.wday]
|
|
|
|
}
|
|
|
|
ve += (fi..fi + 41).collect{|cu|
|
|
|
|
if cu.mon == m then cu.send($da) end.to_s
|
|
|
|
}
|
|
|
|
|
|
|
|
ve = ve.collect{|e| e.rjust($dw)}
|
|
|
|
|
|
|
|
gr = group(ve, 7)
|
|
|
|
gr = trans(gr) if $OPT_t
|
|
|
|
ta = gr.collect{|xs| xs.join(' ')}
|
|
|
|
|
2000-07-18 10:00:45 +04:00
|
|
|
ca = %w(January February March April May June July
|
|
|
|
August September October November December)[m - 1]
|
2000-05-24 08:34:26 +04:00
|
|
|
ca = ca + ' ' + y.to_s if not $OPT_y
|
|
|
|
ca = ca.center($mw)
|
|
|
|
|
|
|
|
ta.unshift(ca)
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
def group(xs, n)
|
|
|
|
(0..xs.size / n - 1).collect{|i| xs[i * n, n]}
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
|
|
|
|
2000-05-24 08:34:26 +04:00
|
|
|
def trans(xs)
|
|
|
|
(0..xs[0].size - 1).collect{|i| xs.collect{|x| x[i]}}
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2000-05-24 08:34:26 +04:00
|
|
|
|
|
|
|
def unite(xs)
|
|
|
|
if xs.empty? then [] else xs[0] + unite(xs[1..-1]) end
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2000-05-24 08:34:26 +04:00
|
|
|
|
|
|
|
def block(xs, n)
|
|
|
|
unite(group(xs, n).collect{|ys| trans(ys).collect{|zs| zs.join(' ')}})
|
|
|
|
end
|
|
|
|
|
|
|
|
def unlines(xs)
|
|
|
|
xs.collect{|x| x + "\n"}.join
|
1999-08-13 09:45:20 +04:00
|
|
|
end
|
2000-05-24 08:34:26 +04:00
|
|
|
|
|
|
|
usage unless getopts('jmty', "c:#{$cc}")
|
|
|
|
|
2002-05-18 16:41:51 +04:00
|
|
|
y, m = ARGV.select(1, 0).compact.collect{|x| x.to_i}
|
2000-05-24 08:34:26 +04:00
|
|
|
$OPT_y ||= (y and not m)
|
|
|
|
|
|
|
|
to = Date.today
|
|
|
|
y ||= to.year
|
|
|
|
m ||= to.mon
|
|
|
|
|
|
|
|
usage unless m >= 1 and m <= 12
|
|
|
|
usage unless y >= -4712
|
2002-06-22 19:26:58 +04:00
|
|
|
usage if (sg = $tab[$OPT_c]).nil?
|
2000-05-24 08:34:26 +04:00
|
|
|
|
|
|
|
$dw = if $OPT_j then 3 else 2 end
|
|
|
|
$mw = ($dw + 1) * 7 - 1
|
|
|
|
$mn = if $OPT_j then 2 else 3 end
|
|
|
|
$tw = ($mw + 2) * $mn - 2
|
|
|
|
|
|
|
|
$k = if $OPT_m then 1 else 0 end
|
|
|
|
$da = if $OPT_j then :yday else :mday end
|
|
|
|
|
2002-06-22 19:26:58 +04:00
|
|
|
print(if not $OPT_y
|
|
|
|
unlines(pict(y, m, sg))
|
|
|
|
else
|
|
|
|
y.to_s.center($tw) + "\n\n" +
|
|
|
|
unlines(block((1..12).collect{|m| pict(y, m, sg)}, $mn)) + "\n"
|
|
|
|
end)
|