git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6961 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2004-09-25 23:10:36 +00:00
Родитель 7e827e6694
Коммит c26e86fd1b
3 изменённых файлов: 81 добавлений и 32 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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
public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset
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.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
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)
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)
end
public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset
def self.now(sg=ITALY) Time.now.to_datetime.new_start(sg) end
end

Просмотреть файл

@ -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)