From 9e3d9a2a009d2a0281802a84e1c5cc1c887edc71 Mon Sep 17 00:00:00 2001 From: matz Date: Thu, 16 Sep 1999 09:40:33 +0000 Subject: 1.4.1 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/date2.rb | 274 +++++++++++++++++++++++++++++++++++-------------------- lib/parsedate.rb | 1 - lib/telnet.rb | 58 +++++++++--- 3 files changed, 221 insertions(+), 112 deletions(-) (limited to 'lib') diff --git a/lib/date2.rb b/lib/date2.rb index cc5e7f4238..38d477b78d 100644 --- a/lib/date2.rb +++ b/lib/date2.rb @@ -1,10 +1,12 @@ # date2.rb: Written by Tadayoshi Funaba 1998, 1999 -# $Id: date2.rb,v 1.15 1999/08/29 15:23:52 tadf Exp $ +# $Id: date2.rb,v 1.17 1999/09/15 05:34:07 tadf Exp $ class Date include Comparable + IDENT = 2 + MONTHNAMES = [ nil, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ] @@ -12,8 +14,10 @@ class Date DAYNAMES = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' ] - ITALY = 2299161 # Oct 15, 1582 - ENGLAND = 2361222 # Sept 14, 1752 + ITALY = 2299161 # 1582-10-15 + ENGLAND = 2361222 # 1752-09-14 + JULIAN = false + GREGORIAN = true class << self @@ -26,7 +30,7 @@ class Date def ns? (jd, sg) not os?(jd, sg) end - def civil_to_jd(y, m, d, sg=true) + def civil_to_jd(y, m, d, sg=GREGORIAN) if m <= 2 y -= 1 m += 12 @@ -42,7 +46,7 @@ class Date jd end - def jd_to_civil(jd, sg=true) + def jd_to_civil(jd, sg=GREGORIAN) if os?(jd, sg) a = jd else @@ -64,16 +68,44 @@ class Date return y, m, dom end - def ordinal_to_jd(y, d, sg=true) + def ordinal_to_jd(y, d, sg=GREGORIAN) civil_to_jd(y, 1, d, sg) end - def jd_to_ordinal(jd, sg=true) + def jd_to_ordinal(jd, sg=GREGORIAN) y = jd_to_civil(jd, sg)[0] doy = jd - civil_to_jd(y - 1, 12, 31, ns?(jd, sg)) return y, doy end + def jd_to_commercial(jd, sg=GREGORIAN) + ns = ns?(jd, sg) + a = jd_to_civil(jd - 3, ns)[0] + y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end + w = 1 + (jd - commercial_to_jd(y, 1, 1, ns)) / 7 + d = (jd + 1) % 7 + if d.zero? then d = 7 end + return y, w, d + end + + def commercial_to_jd(y, w, d, ns=GREGORIAN) + jd = civil_to_jd(y, 1, 4, ns) + (jd - (((jd - 1) + 1) % 7)) + + 7 * (w - 1) + + (d - 1) + end + + def clfloor(x, y=1) + q = (x / y).to_i + z = (q * y) + q -= 1 if (y > 0 and x < z) or (y < 0 and x > z) + r = x - q * y + return q, r + end + + def rjd_to_jd(rjd) clfloor(rjd + 0.5) end + def jd_to_rjd(jd, fr) jd + fr - 0.5 end + def mjd_to_jd(mjd) mjd + 2400000.5 end def jd_to_mjd(jd) jd - 2400000.5 end def tjd_to_jd(tjd) tjd + 2440000.5 end @@ -84,25 +116,25 @@ class Date alias_method :leap?, :gregorian_leap? + def new1(jd=0, sg=ITALY) new(jd, sg) end + def exist3? (y, m, d, sg=ITALY) if m < 0 m += 13 end if d < 0 - ljd = nil - 31.downto 1 do |ld| - break if ljd = exist3?(y, m, ld, sg) + ny, nm = Date.clfloor(y * 12 + m, 12) + nm, = Date.clfloor(m + 1, 1) + la = nil + 31.downto 1 do |z| + break if la = exist3?(y, m, z, sg) end - x = y * 12 + m - ny = x / 12 - nm = x % 12 + 1 - d = jd_to_civil(civil_to_jd(ny, nm, 1, ns?(ljd, sg)) + d, - ns?(ljd, sg))[-1] + ns = ns?(la, sg) + d = jd_to_civil(civil_to_jd(ny, nm, 1, ns) + d, ns)[-1] end jd = civil_to_jd(y, m, d, sg) - if [y, m, d] == jd_to_civil(jd, sg) - jd - end + return unless [y, m, d] == jd_to_civil(jd, sg) + jd end alias_method :exist?, :exist3? @@ -116,18 +148,17 @@ class Date def exist2? (y, d, sg=ITALY) if d < 0 - ljd = nil - 366.downto 1 do |ld| - break if ljd = exist2?(y, ld, sg) - end ny = y + 1 - d = jd_to_ordinal(ordinal_to_jd(ny, 1, ns?(ljd, sg)) + d, - ns?(ljd, sg))[-1] + la = nil + 366.downto 1 do |z| + break if la = exist2?(y, z, sg) + end + ns = ns?(la, sg) + d = jd_to_ordinal(ordinal_to_jd(ny, 1, ns) + d, ns)[-1] end jd = ordinal_to_jd(y, d, sg) - if [y, d] == jd_to_ordinal(jd, sg) - jd - end + return unless [y, d] == jd_to_ordinal(jd, sg) + jd end def new2(y=-4712, d=1, sg=ITALY) @@ -137,130 +168,179 @@ class Date new(jd, sg) end + def existw? (y, w, d, sg=ITALY) + if d < 0 + d += 8 + end + if w < 0 + w = jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)[1] + end + jd = commercial_to_jd(y, w, d) + return unless ns?(jd, sg) + return unless [y, w, d] == jd_to_commercial(jd) + jd + end + + def neww(y=1582, w=41, d=5, sg=ITALY) + unless jd = existw?(y, w, d, sg) + fail ArgumentError, 'invalid date' + end + new(jd, sg) + end + def today(sg=ITALY) new(civil_to_jd(*(Time.now.to_a[3..5].reverse << sg)), sg) end - end - - def initialize(jd=0, sg=ITALY) @jd, @sg = jd, sg end + def once(*ids) + for id in ids + module_eval <<-"end;" + alias_method :__#{id}__, #{id} + def #{id.id2name}(*args, &block) + def self.#{id.id2name}(*args, &block); @__#{id}__ end + @__#{id}__ = __#{id}__(*args, &block) + end + end; + end + end - def jd() @jd end + private :once - def mjd - def self.mjd() @mjd end - @mjd = Date.jd_to_mjd(@jd) end - def tjd - def self.tjd() @tjd end - @tjd = Date.jd_to_tjd(@jd) - end + def initialize(rjd=0, sg=ITALY) @rjd, @sg = rjd, sg end - def civil - def self.year() @year end - def self.mon() @mon end - def self.month() @mon end - def self.mday() @mday end - def self.day() @mday end - @year, @mon, @mday = Date.jd_to_civil(@jd, @sg) - end + def rjd() @rjd end + def rmjd() Date.jd_to_mjd(@rjd) end + def rtjd() Date.jd_to_tjd(@rjd) end - private :civil + once :rmjd, :rtjd - def year - civil - @year - end + def jd() Date.rjd_to_jd(@rjd)[0] end + def fr1() Date.rjd_to_jd(@rjd)[1] end + def mjd() Date.jd_to_mjd(jd) end + def tjd() Date.jd_to_tjd(jd) end - def yday - def self.yday() @yday end - @yday = Date.jd_to_ordinal(@jd, @sg)[-1] - @yday - end + once :jd, :fr1, :mjd, :tjd - def mon - civil - @mon - end + def civil() Date.jd_to_civil(jd, @sg) end + def ordinal() Date.jd_to_ordinal(jd, @sg) end + def commercial() Date.jd_to_commercial(jd, @sg) end + + once :civil, :ordinal, :commercial + + def year() civil[0] end + def yday() ordinal[1] end + def mon() civil[1] end alias_method :month, :mon + once :year, :yday, :mon, :month - def mday - civil - @mday - end + def mday() civil[2] end alias_method :day, :mday + once :mday, :day - def wday - def self.wday() @wday end - @wday = (@jd + 1) % 7 - end + def cwyear() commercial[0] end + def cweek() commercial[1] end + def cwday() commercial[2] end + + once :cwyear, :cweek, :cwday + + def wday() (jd + 1) % 7 end + + once :wday + + def os? () Date.os?(jd, @sg) end + def ns? () Date.ns?(jd, @sg) end - def os? () Date.os?(@jd, @sg) end - def ns? () Date.ns?(@jd, @sg) end + once :os?, :ns? def leap? - def self.leap?() @leap_p end - @leap_p = Date.jd_to_civil(Date.civil_to_jd(year, 3, 1, ns?) - 1, - ns?)[-1] == 29 + Date.jd_to_civil(Date.civil_to_jd(year, 3, 1, ns?) - 1, + ns?)[-1] == 29 end - def + (other) - case other - when Numeric; return Date.new(@jd + other, @sg) + once :leap? + + def sg() @sg end + def newsg(sg=Date::ITALY) Date.new(@rjd, sg) end + + def italy() newsg(Date::ITALY) end + def england() newsg(Date::ENGLAND) end + def julian() newsg(Date::JULIAN) end + def gregorian() newsg(Date::GREGORIAN) end + + def + (n) + case n + when Numeric; return Date.new(@rjd + n, @sg) end fail TypeError, 'expected numeric' end - def - (other) - case other - when Numeric; return Date.new(@jd - other, @sg) - when Date; return @jd - other.jd + def - (x) + case x + when Numeric; return Date.new(@rjd - x, @sg) + when Date; return @rjd - x.rjd end fail TypeError, 'expected numeric or date' end def <=> (other) case other - when Numeric; return @jd <=> other - when Date; return @jd <=> other.jd + when Numeric; return @rjd <=> other + when Date; return @rjd <=> other.rjd end fail TypeError, 'expected numeric or date' end - def downto(min) - @jd.downto(min.jd) do |jd| - yield Date.new(jd, @sg) + def === (other) + case other + when Numeric; return jd == other + when Date; return jd == other.jd end - self + fail TypeError, 'expected numeric or date' end - def upto(max) - @jd.upto(max.jd) do |jd| - yield Date.new(jd, @sg) - end - self + def >> (n) + y, m = Date.clfloor(year * 12 + (mon - 1) + n, 12) + m, = Date.clfloor(m + 1, 1) + d = mday + d -= 1 until jd2 = Date.exist3?(y, m, d, ns?) + self + (jd2 - jd) end + def << (n) self >> -n end + def step(limit, step) - @jd.step(limit.jd, step) do |jd| - yield Date.new(jd, @sg) + rjd = @rjd + if (step > 0) + while rjd <= limit.rjd + yield Date.new(rjd, @sg) + rjd += step + end + else + while rjd >= limit.rjd + yield Date.new(rjd, @sg) + rjd += step + end end self end + def upto(max, &block) step(max, +1, &block) end + def downto(min, &block) step(min, -1, &block) end + def succ() self + 1 end alias_method :next, :succ - def eql? (other) self == other end - def hash() @jd end - def inspect() format('#', @jd, @sg) end + def eql? (other) Date === other and self == other end + def hash() Date.clfloor(@rjd)[0] end + def inspect() format('#', @rjd, @sg) end def to_s() format('%.4d-%02d-%02d', year, mon, mday) end - def _dump(limit) Marshal.dump([@jd, @sg], -1) end + def _dump(limit) Marshal.dump([@rjd, @sg], -1) end def Date._load(str) Date.new(*Marshal.load(str)) end end diff --git a/lib/parsedate.rb b/lib/parsedate.rb index f49266d2e5..29b3fa43b9 100644 --- a/lib/parsedate.rb +++ b/lib/parsedate.rb @@ -67,7 +67,6 @@ module ParseDate mon = MONTHS[$2.downcase] year = $3.to_i end - p date if date.sub!(/\d{4}/i, ' ') year = $&.to_i elsif date.sub!(/\d\d/i, ' ') diff --git a/lib/telnet.rb b/lib/telnet.rb index 96e2cac100..9300bf6e18 100644 --- a/lib/telnet.rb +++ b/lib/telnet.rb @@ -1,11 +1,11 @@ =begin -$Date: 1999/08/10 05:20:21 $ +$Date: 1999/09/14 23:09:05 $ == SIMPLE TELNET CLIANT LIBRARY telnet.rb -Version 0.232 +Version 0.30 Wakou Aoyama @@ -34,7 +34,7 @@ the network or the host is very heavy, the value is enlarged. === STATUS OUTPUT - host = Telnet.new({"Host" => "localhost"){|c| print c } + host = Telnet.new({"Host" => "localhost"}){|c| print c } connection status output. @@ -110,6 +110,13 @@ of cource, set sync=true or flush is necessary. "Prompt" => /[$%#>] \z/n, "Timeout" => 10}) +if no password prompt. + + host.login("username") + host.login({"Name" => "username", + "Prompt" => /[$%#>] \z/n, + "Timeout" => 10}) + ==== REALTIME OUTPUT @@ -147,13 +154,28 @@ of cource, set sync=true or flush is necessary. == HISTORY +=== Version 0.30 + +1999/09/14 23:09:05 + +- change prompt check order. + not IO::select([@sock], nil, nil, waittime) and prompt === line + --> prompt === line and not IO::select([@sock], nil, nil, waittime) + +=== Version 0.24 + +1999/09/13 22:28:33 + +- Telnet#login +if ommit password, then not require password prompt. + === Version 0.232 1999/08/10 05:20:21 - STATUS OUTPUT sample code typo. thanks to Tadayoshi Funaba host = Telnet.new({"Hosh" => "localhost"){|c| print c } - host = Telnet.new({"Host" => "localhost"){|c| print c } + --> host = Telnet.new({"Host" => "localhost"){|c| print c } === Version 0.231 @@ -370,8 +392,8 @@ class Telnet < SimpleDelegator EOL = CR + LF v = $-v $-v = false - VERSION = "0.232" - RELEASE_DATE = "$Date: 1999/08/10 05:20:21 $" + VERSION = "0.30" + RELEASE_DATE = "$Date: 1999/09/14 23:09:05 $" $-v = v def initialize(options) @@ -540,7 +562,7 @@ $-v = v line = '' buf = '' - until(not IO::select([@sock], nil, nil, waittime) and prompt === line) + until(prompt === line and not IO::select([@sock], nil, nil, waittime)) unless IO::select([@sock], nil, nil, time_out) raise TimeOut, "timed-out; wait for the next data" end @@ -611,7 +633,7 @@ $-v = v end end - def login(options, password = '') + def login(options, password = nil) if options.kind_of?(Hash) username = options["Name"] password = options["Password"] @@ -621,14 +643,22 @@ $-v = v if iterator? line = waitfor(/login[: ]*\z/n){|c| yield c } - line.concat( cmd({"String" => username, - "Match" => /Password[: ]*\z/n}){|c| yield c } ) - line.concat( cmd(password){|c| yield c } ) + if password + line.concat( cmd({"String" => username, + "Match" => /Password[: ]*\z/n}){|c| yield c } ) + line.concat( cmd(password){|c| yield c } ) + else + line.concat( cmd(username){|c| yield c } ) + end else line = waitfor(/login[: ]*\z/n) - line.concat( cmd({"String" => username, - "Match" => /Password[: ]*\z/n}) ) - line.concat( cmd(password) ) + if password + line.concat( cmd({"String" => username, + "Match" => /Password[: ]*\z/n}) ) + line.concat( cmd(password) ) + else + line.concat( cmd(username) ) + end end line end -- cgit v1.2.3