synchronized with date2 3.5.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@5494 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
91c798b670
commit
720a23df0a
16
lib/date.rb
16
lib/date.rb
@ -1,12 +1,12 @@
|
|||||||
#
|
#
|
||||||
# date.rb - date and time library
|
# date.rb - date and time library
|
||||||
#
|
#
|
||||||
# Author: Tadayoshi Funaba 1998-2002
|
# Author: Tadayoshi Funaba 1998-2004
|
||||||
#
|
#
|
||||||
# Documentation: William Webber <william@williamwebber.com>
|
# Documentation: William Webber <william@williamwebber.com>
|
||||||
#
|
#
|
||||||
#--
|
#--
|
||||||
# $Id: date.rb,v 2.8 2002-06-08 00:39:51+09 tadf Exp $
|
# $Id: date.rb,v 2.11 2004-01-19 04:56:12+09 tadf Exp $
|
||||||
#++
|
#++
|
||||||
#
|
#
|
||||||
# == Overview
|
# == Overview
|
||||||
@ -817,13 +817,7 @@ class Date
|
|||||||
|
|
||||||
private :hour, :min, :sec, :sec_fraction
|
private :hour, :min, :sec, :sec_fraction
|
||||||
|
|
||||||
def zone
|
def zone() strftime('%Z') end
|
||||||
['Z',
|
|
||||||
format('%+.2d%02d',
|
|
||||||
(@of / (1.to_r/24)).to_i,
|
|
||||||
(@of.abs % (1.to_r/24) / (1.to_r/1440)).to_i)
|
|
||||||
][@of<=>0]
|
|
||||||
end
|
|
||||||
|
|
||||||
private :zone
|
private :zone
|
||||||
|
|
||||||
@ -1266,9 +1260,7 @@ class DateTime < Date
|
|||||||
a = i.to_a[0..5].reverse
|
a = i.to_a[0..5].reverse
|
||||||
jd = civil_to_jd(*(a[0,3] << sg))
|
jd = civil_to_jd(*(a[0,3] << sg))
|
||||||
fr = time_to_day_fraction(*(a[3,3])) + i.usec.to_r/86400000000
|
fr = time_to_day_fraction(*(a[3,3])) + i.usec.to_r/86400000000
|
||||||
d = Time.gm(*i.to_a).to_i - i.to_i
|
of = i.utc_offset.to_r/86400
|
||||||
d += d / d.abs if d.nonzero?
|
|
||||||
of = (d / 60).to_r/1440
|
|
||||||
new0(jd_to_ajd(jd, fr, of), of, sg)
|
new0(jd_to_ajd(jd, fr, of), of, sg)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# format.rb: Written by Tadayoshi Funaba 1999-2003
|
# format.rb: Written by Tadayoshi Funaba 1999-2004
|
||||||
# $Id: format.rb,v 2.9 2003-04-19 19:19:35+09 tadf Exp $
|
# $Id: format.rb,v 2.12 2004-01-19 05:43:28+09 tadf Exp $
|
||||||
|
|
||||||
class Date
|
class Date
|
||||||
|
|
||||||
@ -501,8 +501,14 @@ class Date
|
|||||||
when '%x'; o << strftime('%m/%d/%y')
|
when '%x'; o << strftime('%m/%d/%y')
|
||||||
when '%Y'; o << '%.4d' % year
|
when '%Y'; o << '%.4d' % year
|
||||||
when '%y'; o << '%02d' % (year % 100)
|
when '%y'; o << '%02d' % (year % 100)
|
||||||
when '%Z'; o << zone
|
when '%Z'; o << (if offset.zero? then 'Z' else strftime('%z') end)
|
||||||
when '%z'; o << zone # ID
|
when '%z' # ID
|
||||||
|
o << if offset < 0 then '-' else '+' end
|
||||||
|
of = offset.abs
|
||||||
|
hh, fr = of.divmod(1.to_r/24)
|
||||||
|
mm = fr / (1.to_r/1440)
|
||||||
|
o << '%02d' % hh
|
||||||
|
o << '%02d' % mm
|
||||||
when '%%'; o << '%'
|
when '%%'; o << '%'
|
||||||
when '%+'; o << strftime('%a %b %e %H:%M:%S %Z %Y') # TZ
|
when '%+'; o << strftime('%a %b %e %H:%M:%S %Z %Y') # TZ
|
||||||
when '%1'; o << '%d' % jd
|
when '%1'; o << '%d' % jd
|
||||||
|
111
sample/cal.rb
111
sample/cal.rb
@ -1,12 +1,13 @@
|
|||||||
#! /usr/bin/env ruby
|
#! /usr/bin/env ruby
|
||||||
|
|
||||||
# cal.rb: Written by Tadayoshi Funaba 1998-2002
|
# cal.rb: Written by Tadayoshi Funaba 1998-2004
|
||||||
# $Id: cal.rb,v 2.4 2002-06-08 00:40:29+09 tadf Exp $
|
# $Id: cal.rb,v 2.7 2004-01-10 23:52:51+09 tadf Exp $
|
||||||
|
|
||||||
require 'date'
|
require 'date'
|
||||||
require 'getopts'
|
|
||||||
|
|
||||||
$tab =
|
class Cal
|
||||||
|
|
||||||
|
START =
|
||||||
{
|
{
|
||||||
'cn' => true, # China
|
'cn' => true, # China
|
||||||
'de' => 2342032, # Germany (protestant states)
|
'de' => 2342032, # Germany (protestant states)
|
||||||
@ -29,35 +30,50 @@ $tab =
|
|||||||
'ns' => true # (new style)
|
'ns' => true # (new style)
|
||||||
}
|
}
|
||||||
|
|
||||||
$cc = 'gb'
|
DEFAULT_START = 'gb'
|
||||||
|
|
||||||
def usage
|
def initialize
|
||||||
warn 'usage: cal [-c iso3166] [-jmty] [[month] year]'
|
opt_j; opt_m; opt_t; opt_y; opt_c
|
||||||
exit 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def pict(y, m, sg)
|
def opt_j(flag=false) @opt_j = flag end
|
||||||
d = (1..31).detect{|d| Date.valid_date?(y, m, d, sg)}
|
def opt_m(flag=false) @opt_m = flag end
|
||||||
fi = Date.new(y, m, d, sg)
|
def opt_t(flag=false) @opt_t = flag end
|
||||||
fi -= (fi.jd - $k + 1) % 7
|
def opt_y(flag=false) @opt_y = flag end
|
||||||
|
|
||||||
|
def opt_c(arg=DEFAULT_START) @start = START[arg] end
|
||||||
|
|
||||||
|
def set_params
|
||||||
|
@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
|
||||||
|
end
|
||||||
|
|
||||||
|
def pict(y, m)
|
||||||
|
d = (1..31).detect{|d| Date.valid_date?(y, m, d, @start)}
|
||||||
|
fi = Date.new(y, m, d, @start)
|
||||||
|
fi -= (fi.jd - @k + 1) % 7
|
||||||
|
|
||||||
ve = (fi..fi + 6).collect{|cu|
|
ve = (fi..fi + 6).collect{|cu|
|
||||||
%w(S M Tu W Th F S)[cu.wday]
|
%w(S M Tu W Th F S)[cu.wday]
|
||||||
}
|
}
|
||||||
ve += (fi..fi + 41).collect{|cu|
|
ve += (fi..fi + 41).collect{|cu|
|
||||||
if cu.mon == m then cu.send($da) end.to_s
|
if cu.mon == m then cu.send(@da) end.to_s
|
||||||
}
|
}
|
||||||
|
|
||||||
ve = ve.collect{|e| e.rjust($dw)}
|
ve = ve.collect{|e| e.rjust(@dw)}
|
||||||
|
|
||||||
gr = group(ve, 7)
|
gr = group(ve, 7)
|
||||||
gr = trans(gr) if $OPT_t
|
gr = trans(gr) if @opt_t
|
||||||
ta = gr.collect{|xs| xs.join(' ')}
|
ta = gr.collect{|xs| xs.join(' ')}
|
||||||
|
|
||||||
ca = %w(January February March April May June July
|
ca = %w(January February March April May June July
|
||||||
August September October November December)[m - 1]
|
August September October November December)[m - 1]
|
||||||
ca = ca + ' ' + y.to_s if not $OPT_y
|
ca = ca + ' ' + y.to_s if not @opt_y
|
||||||
ca = ca.center($mw)
|
ca = ca.center(@mw)
|
||||||
|
|
||||||
ta.unshift(ca)
|
ta.unshift(ca)
|
||||||
end
|
end
|
||||||
@ -70,19 +86,49 @@ def trans(xs)
|
|||||||
(0..xs[0].size - 1).collect{|i| xs.collect{|x| x[i]}}
|
(0..xs[0].size - 1).collect{|i| xs.collect{|x| x[i]}}
|
||||||
end
|
end
|
||||||
|
|
||||||
def unite(xs)
|
def stack(xs)
|
||||||
if xs.empty? then [] else xs[0] + unite(xs[1..-1]) end
|
if xs.empty? then [] else xs[0] + stack(xs[1..-1]) end
|
||||||
end
|
end
|
||||||
|
|
||||||
def block(xs, n)
|
def block(xs, n)
|
||||||
unite(group(xs, n).collect{|ys| trans(ys).collect{|zs| zs.join(' ')}})
|
stack(group(xs, n).collect{|ys| trans(ys).collect{|zs| zs.join(' ')}})
|
||||||
end
|
end
|
||||||
|
|
||||||
def unlines(xs)
|
def unlines(xs)
|
||||||
xs.collect{|x| x + "\n"}.join
|
xs.collect{|x| x + "\n"}.join
|
||||||
end
|
end
|
||||||
|
|
||||||
usage unless getopts('jmty', "c:#{$cc}")
|
def monthly(y, m)
|
||||||
|
unlines(pict(y, m))
|
||||||
|
end
|
||||||
|
|
||||||
|
def addmon(y, m, n)
|
||||||
|
y, m = (y * 12 + (m - 1) + n).divmod(12)
|
||||||
|
return y, m + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def yearly(y)
|
||||||
|
y.to_s.center(@tw) + "\n\n" +
|
||||||
|
unlines(block((0..11).collect{|n| pict(*addmon(y, 1, n))}, @mn)) + "\n"
|
||||||
|
end
|
||||||
|
|
||||||
|
def print(y, m)
|
||||||
|
set_params
|
||||||
|
if @opt_y then yearly(y) else monthly(y, m) end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
if __FILE__ == $0
|
||||||
|
|
||||||
|
require 'getopts'
|
||||||
|
|
||||||
|
def usage
|
||||||
|
warn 'usage: cal [-c iso3166] [-jmty] [[month] year]'
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
usage unless getopts('jmty', "c:#{Cal::DEFAULT_START}")
|
||||||
|
|
||||||
y, m = ARGV.values_at(1, 0).compact.collect{|x| x.to_i}
|
y, m = ARGV.values_at(1, 0).compact.collect{|x| x.to_i}
|
||||||
$OPT_y ||= (y and not m)
|
$OPT_y ||= (y and not m)
|
||||||
@ -93,19 +139,16 @@ m ||= to.mon
|
|||||||
|
|
||||||
usage unless m >= 1 and m <= 12
|
usage unless m >= 1 and m <= 12
|
||||||
usage unless y >= -4712
|
usage unless y >= -4712
|
||||||
usage if (sg = $tab[$OPT_c]).nil?
|
usage if Cal::START[$OPT_c].nil?
|
||||||
|
|
||||||
$dw = if $OPT_j then 3 else 2 end
|
cal = Cal.new
|
||||||
$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
|
cal.opt_j($OPT_j)
|
||||||
$da = if $OPT_j then :yday else :mday end
|
cal.opt_m($OPT_m)
|
||||||
|
cal.opt_t($OPT_t)
|
||||||
|
cal.opt_y($OPT_y)
|
||||||
|
cal.opt_c($OPT_c)
|
||||||
|
|
||||||
print(if not $OPT_y
|
print cal.print(y, m)
|
||||||
unlines(pict(y, m, sg))
|
|
||||||
else
|
end
|
||||||
y.to_s.center($tw) + "\n\n" +
|
|
||||||
unlines(block((1..12).collect{|m| pict(y, m, sg)}, $mn)) + "\n"
|
|
||||||
end)
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user