summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-25 23:10:36 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2004-09-25 23:10:36 +0000
commit57ef222d3a3c54336ba77630eb1031ab32883479 (patch)
tree96db636f6bed5ce3bd4859bd0f8c15c375d8a413
parent054dfd9c96fb54a846f567205e4fe24a336c2db5 (diff)
downloadruby-57ef222d3a3c54336ba77630eb1031ab32883479.tar.gz
ruby-57ef222d3a3c54336ba77630eb1031ab32883479.tar.xz
ruby-57ef222d3a3c54336ba77630eb1031ab32883479.zip
synchronized with date2 3.6.1
git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@6961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--lib/date.rb71
-rw-r--r--sample/cal.rb36
3 files changed, 81 insertions, 32 deletions
diff --git a/ChangeLog b/ChangeLog
index 9dd166cd2..35ca51cb2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Sun Sep 26 08:05:10 2004 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * lib/date.rb: provides {Time,Date,DateTime}#to_{time,date,datetime}.
+
+ * sample/cal.rb: uses getoptlong instead of getopts.
+
Sat Sep 25 04:04:52 2004 Nobuyoshi Nakada <nobu@ruby-lang.org>
* hash.c: iterator functions for hash_foreach() should return enum
diff --git a/lib/date.rb b/lib/date.rb
index 3e3e33de7..d67b1bab7 100644
--- a/lib/date.rb
+++ b/lib/date.rb
@@ -6,7 +6,7 @@
# Documentation: William Webber <william@williamwebber.com>
#
#--
-# $Id: date.rb,v 2.12 2004-03-20 08:05:13+09 tadf Exp $
+# $Id: date.rb,v 2.14 2004-09-25 09:51:25+09 tadf Exp $
#++
#
# == Overview
@@ -695,14 +695,6 @@ class Date
new_with_hash(elem, sg)
end
- # Create a new Date object representing today.
- #
- # +sg+ specifies the Day of Calendar Reform.
- def self.today(sg=ITALY)
- jd = civil_to_jd(*(Time.now.to_a[3..5].reverse << sg))
- new0(jd_to_ajd(jd, 0, 0), 0, sg)
- end
-
class << self
def once(*ids) # :nodoc:
@@ -1258,21 +1250,62 @@ class DateTime < Date
new_with_hash(elem, sg)
end
- class << self; undef_method :today end
+ public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset
- # Create a new DateTime object representing the current time.
+end
+
+class Time
+
+ def to_time() getlocal end
+
+ def to_date
+ jd = Date.civil_to_jd(year, mon, mday, Date::ITALY)
+ Date.new0(Date.jd_to_ajd(jd, 0, 0), 0, Date::ITALY)
+ end
+
+ def to_datetime
+ jd = DateTime.civil_to_jd(year, mon, mday, DateTime::ITALY)
+ fr = DateTime.time_to_day_fraction(hour, min, [sec, 59].min) +
+ usec.to_r/86400000000
+ of = utc_offset.to_r/86400
+ DateTime.new0(DateTime.jd_to_ajd(jd, fr, of), of, DateTime::ITALY)
+ end
+
+end
+
+class Date
+
+ def to_time() Time.local(year, mon, mday) end
+ def to_date() self end
+ def to_datetime() DateTime.new0(self.class.jd_to_ajd(jd, 0, 0), @of, @sg) end
+
+ # Create a new Date object representing today.
#
# +sg+ specifies the Day of Calendar Reform.
- def self.now(sg=ITALY)
- i = Time.now
- a = i.to_a[0..5].reverse
- jd = civil_to_jd(*(a[0,3] << sg))
- fr = time_to_day_fraction(*(a[3,3])) + i.usec.to_r/86400000000
- of = i.utc_offset.to_r/86400
- new0(jd_to_ajd(jd, fr, of), of, sg)
+ def self.today(sg=ITALY) Time.now.to_date.new_start(sg) end
+
+end
+
+class DateTime < Date
+
+ def to_time
+ d = new_offset(0)
+ d.instance_eval do
+ Time.utc(year, mon, mday, hour, min, sec,
+ (sec_fraction * 86400000000).to_i)
+ end.
+ getlocal
end
- public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset
+ def to_date() Date.new0(self.class.jd_to_ajd(jd, 0, 0), 0, @sg) end
+ def to_datetime() self end
+
+ class << self; undef_method :today end
+
+ # Create a new DateTime object representing the current time.
+ #
+ # +sg+ specifies the Day of Calendar Reform.
+ def self.now(sg=ITALY) Time.now.to_datetime.new_start(sg) end
end
diff --git a/sample/cal.rb b/sample/cal.rb
index fa20352f7..197cdfe3a 100644
--- a/sample/cal.rb
+++ b/sample/cal.rb
@@ -1,7 +1,7 @@
#! /usr/bin/env ruby
# cal.rb: Written by Tadayoshi Funaba 1998-2004
-# $Id: cal.rb,v 2.7 2004-01-10 23:52:51+09 tadf Exp $
+# $Id: cal.rb,v 2.8 2004-09-25 12:50:10+09 tadf Exp $
require 'date'
@@ -121,17 +121,36 @@ end
if __FILE__ == $0
- require 'getopts'
+ require 'getoptlong'
def usage
warn 'usage: cal [-c iso3166] [-jmty] [[month] year]'
exit 1
end
- usage unless getopts('jmty', "c:#{Cal::DEFAULT_START}")
+ cal = Cal.new
+
+ begin
+ GetoptLong.new(['-c', GetoptLong::REQUIRED_ARGUMENT],
+ ['-j', GetoptLong::NO_ARGUMENT],
+ ['-m', GetoptLong::NO_ARGUMENT],
+ ['-t', GetoptLong::NO_ARGUMENT],
+ ['-y', GetoptLong::NO_ARGUMENT]).
+ each do |opt, arg|
+ case opt
+ when '-c'; cal.opt_c(arg) || raise
+ when '-j'; cal.opt_j(true)
+ when '-m'; cal.opt_m(true)
+ when '-t'; cal.opt_t(true)
+ when '-y'; cal.opt_y(true)
+ end
+ end
+ rescue
+ usage
+ end
y, m = ARGV.values_at(1, 0).compact.collect{|x| x.to_i}
- $OPT_y ||= (y and not m)
+ cal.opt_y(true) if y and not m
to = Date.today
y ||= to.year
@@ -139,15 +158,6 @@ if __FILE__ == $0
usage unless m >= 1 and m <= 12
usage unless y >= -4712
- usage if Cal::START[$OPT_c].nil?
-
- cal = Cal.new
-
- cal.opt_j($OPT_j)
- cal.opt_m($OPT_m)
- cal.opt_t($OPT_t)
- cal.opt_y($OPT_y)
- cal.opt_c($OPT_c)
print cal.print(y, m)