summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-02-03 10:02:57 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1998-02-03 10:02:57 +0000
commit01bb9da7acfeb868eeec4fc20061c57d85b26cf8 (patch)
tree9b0ef258786fb82863ed752fc410405f86ac2068 /lib
parentde0e5e35ae39a03879d8a954dd6b75654c173cd7 (diff)
*** empty log message ***
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/v1_1r@63 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/date.rb50
-rw-r--r--lib/parsedate.rb79
-rw-r--r--lib/tracer.rb107
3 files changed, 165 insertions, 71 deletions
diff --git a/lib/date.rb b/lib/date.rb
index 0f14d59ff5..2d5090b62b 100644
--- a/lib/date.rb
+++ b/lib/date.rb
@@ -1,8 +1,8 @@
#
# Date.rb -
# $Release Version: $
-# $Revision: 1.1.1.1.4.1 $
-# $Date: 1998/01/16 12:36:04 $
+# $Revision: 1.1.1.1.4.2 $
+# $Date: 1998/02/02 04:49:13 $
# by Yasuo OHBA(SHL Japan Inc. Technology Dept.)
#
# --
@@ -32,10 +32,16 @@ class Date
}
def initialize(y = 1, m = 1, d = 1)
- if y.kind_of?(String) && y.size == 8
- @year = y[0,4].to_i
- @month = y[4,2].to_i
- @day = y[6,2].to_i
+ if y.kind_of?(String)
+ case y
+ when /(\d\d\d\d)-?(?:(\d\d)-?(\d\d)?)?/
+ @year = $1.to_i
+ @month = if $2 then $2.to_i else 1 end
+ @day = if $3 then $3.to_i else 1 end
+ else
+ require 'parsedate'
+ @year, @month, @day = ParseDate.parsedate(y)
+ end
else
if m.kind_of?(String)
m = Monthtab[m.downcase]
@@ -66,18 +72,25 @@ class Date
def period
return Date.period!(@year, @month, @day)
end
-
+
+ def jd
+ return period + 1721423
+ end
+
+ def mjd
+ return jd - 2400000.5
+ end
+
+ def to_s
+ format("%.3s, %.3s %2d %4d", name_of_week, name_of_month, @day, @year)
+ end
+
+ def inspect
+ to_s
+ end
+
def day_of_week
- dl = Date.daylist(@year)
- d = Date.jan1!(@year)
- for m in 1..(@month - 1)
- d += dl[m]
- end
- d += @day - 1
- if @year == 1752 && @month == 9 && @day >= 14
- d -= (14 - 3)
- end
- return (d % 7)
+ return (period + 5) % 7
end
def name_of_week
@@ -141,6 +154,9 @@ class Date
end
def _check_date
+ if @year == nil or @month == nil or @day == nil
+ raise ArgumentError, "argument contains nil"
+ end
m = Date.daylist(@year)
if @month < 1 || @month > 12
raise ArgumentError, "argument(month) out of range."
diff --git a/lib/parsedate.rb b/lib/parsedate.rb
index 2ec54b0125..69b7ae98a9 100644
--- a/lib/parsedate.rb
+++ b/lib/parsedate.rb
@@ -4,39 +4,66 @@ module ParseDate
'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
'sep' => 9, 'oct' =>10, 'nov' =>11, 'dec' =>12 }
MONTHPAT = MONTHS.keys.join('|')
- DAYPAT = 'mon|tue|wed|thu|fri|sat|sun'
+ DAYS = {
+ 'sun' => 0, 'mon' => 1, 'tue' => 2, 'wed' => 3,
+ 'thu' => 4, 'fri' => 5, 'sat' => 6 }
+ DAYPAT = DAYS.keys.join('|')
def parsedate(date)
- if date.sub!(/(#{DAYPAT})/i, ' ')
- dayofweek = $1
+ # ISO 8601?
+ if date =~ /(\d\d\d\d)-?(?:(\d\d)-?(\d\d)?)? *(?:(\d\d):(\d\d)(?::(\d\d))?)?/
+ return $1.to_i,
+ if $2 then $2.to_i else 1 end,
+ if $3 then $3.to_i else 1 end,
+ nil,
+ if $4 then $4.to_i end,
+ if $5 then $5.to_i end,
+ if $6 then $6.to_i end,
+ nil
end
- if date.sub!(/\s+(\d+:\d+(:\d+)?)/, ' ')
- time = $1
+ date = date.dup
+ if date.sub!(/(#{DAYPAT})[a-z]*,?/i, ' ')
+ wday = DAYS[$1.downcase]
end
- if date =~ /(19|20)(\d\d)/
- year = Integer($2)
+ if date.sub!(/(\d+):(\d+)(?::(\d+))?\s*(am|pm)?\s*(?:\s+([a-z]{1,4}(?:\s+[a-z]{1,4})|[-+]\d{4}))?/i, ' ')
+ hour = $1.to_i
+ min = $2.to_i
+ if $3
+ sec = $3.to_i
+ end
+ if $4 == 'pm'
+ hour += 12
+ end
+ if $5
+ zone = $5
+ end
end
- if date.sub!(/\s*(\d+)\s+(#{MONTHPAT})\S*\s+/i, ' ')
- dayofmonth = $1.to_i
- monthname = $2
- elsif date.sub!(/\s*(#{MONTHPAT})\S*\s+(\d+)\s+/i, ' ')
- monthname = $1
- dayofmonth = $2.to_i
- elsif date.sub!(/\s*(#{MONTHPAT})\S*\s+(\d+)\D+/i, ' ')
- monthname = $1
- dayofmonth = $2.to_i
- elsif date.sub!(/\s*(\d\d?)\/(\d\d?)/, ' ')
- month = $1
- dayofmonth = $2.to_i
+ if date.sub!(/(\d+)\S*\s+(#{MONTHPAT})\S*(?:\s+(\d+))?/i, ' ')
+ mday = $1.to_i
+ mon = MONTHS[$2.downcase]
+ if $3
+ year = $3.to_i
+ end
+ elsif date.sub!(/(#{MONTHPAT})\S*\s+(\d+)\S*\s*,?(?:\s+(\d+))?/i, ' ')
+ mon = MONTHS[$1.downcase]
+ mday = $2.to_i
+ if $3
+ year = $3.to_i
+ end
+ elsif date.sub!(/(\d+)\/(\d+)(?:\/(\d+))/, ' ')
+ mon = $1.to_i
+ mday = $2.to_i
+ if $3
+ year = $3.to_i
+ end
end
- if monthname
- month = MONTHS[monthname.downcase]
- end
- if ! year && date =~ /\d\d/
- year = Integer($&)
- end
- return year, month, dayofmonth
+ return year, mon, mday, wday, hour, min, sec, zone
end
module_function :parsedate
end
+
+if __FILE__ == $0
+ p Time.now.asctime
+ p ParseDate.parsedate(Time.now.asctime)
+end
diff --git a/lib/tracer.rb b/lib/tracer.rb
index d37339fd62..ef03fe09c9 100644
--- a/lib/tracer.rb
+++ b/lib/tracer.rb
@@ -1,7 +1,23 @@
+#!/usr/local/bin/ruby
+#
+# tracer.rb -
+# $Release Version: 0.2$
+# $Revision: 1.6 $
+# $Date: 1998/02/02 08:12:02 $
+# by Keiju ISHITSUKA(Nippon Rational Inc.)
+#
+# --
+#
+#
+#
+
+#
+# tracer main class
+#
class Tracer
- MY_FILE_NAME_PATTERN = /^tracer\.(rb)?/
- Threads = Hash.new
- Sources = Hash.new
+ RCS_ID='-$Id: tracer.rb,v 1.6 1998/02/02 08:12:02 keiju Exp keiju $-'
+
+ MY_FILE_NAME = caller(0)[0].scan(/^(.*):[0-9]+$/)[0]
EVENT_SYMBOL = {
"line" => "-",
@@ -10,11 +26,31 @@ class Tracer
"class" => "C",
"end" => "E"}
+ def initialize
+ @threads = Hash.new
+ if defined? Thread.main
+ @threads[Thread.main.id] = 0
+ else
+ @threads[Thread.current.id] = 0
+ end
+
+ @sources = Hash.new
+ end
+
def on
- set_trace_func proc{|event, file, line, id, binding|
- trace_func event, file, line, id, binding
- }
- print "Trace on\n"
+ if iterator?
+ on
+ begin
+ yield
+ ensure
+ off
+ end
+ else
+ set_trace_func proc{|event, file, line, id, binding|
+ trace_func event, file, line, id, binding
+ }
+ print "Trace on\n"
+ end
end
def off
@@ -22,27 +58,38 @@ class Tracer
print "Trace off\n"
end
- def get_thread_no
- unless no = Threads[Thread.current.id]
- Threads[Thread.current.id] = no = Threads.size
+ def get_line(file, line)
+ unless list = @sources[file]
+# print file if $DEBUG
+ begin
+ f = open(file)
+ begin
+ @sources[file] = list = f.readlines
+ ensure
+ f.close
+ end
+ rescue
+ @sources[file] = list = []
+ end
+ end
+ if l = list[line - 1]
+ l
+ else
+ "-\n"
end
- no
end
- def get_line(file, line)
- unless list = Sources[file]
- f =open(file)
- begin
- Sources[file] = list = f.readlines
- ensure
- f.close
- end
+ def get_thread_no
+ if no = @threads[Thread.current.id]
+ no
+ else
+ @threads[Thread.current.id] = @threads.size
end
- list[line - 1]
end
def trace_func(event, file, line, id, binding)
- return if File.basename(file) =~ MY_FILE_NAME_PATTERN
+ return if file == MY_FILE_NAME
+ #printf "Th: %s\n", Thread.current.inspect
Thread.critical = TRUE
printf("#%d:%s:%d:%s: %s",
@@ -65,11 +112,15 @@ class Tracer
end
-if File.basename($0) =~ Tracer::MY_FILE_NAME_PATTERN
- $0 = ARGV.shift
-
- Tracer.on
- load $0
-else
- Tracer.on
+if caller(0).size == 1
+ if $0 == Tracer::MY_FILE_NAME
+ # direct call
+
+ $0 = ARGV[0]
+ ARGV.shift
+ Tracer.on
+ require $0
+ else
+ Tracer.on
+ end
end