2004-01-18 17:01:12 -05:00
|
|
|
#
|
2003-08-23 08:00:21 -04:00
|
|
|
# date.rb - date and time library
|
|
|
|
#
|
2010-03-22 06:32:10 -04:00
|
|
|
# Author: Tadayoshi Funaba 1998-2010
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# Documentation: William Webber <william@williamwebber.com>
|
|
|
|
#
|
|
|
|
#--
|
2008-01-17 07:05:43 -05:00
|
|
|
# $Id: date.rb,v 2.37 2008-01-17 20:16:31+09 tadf Exp $
|
2003-08-23 08:00:21 -04:00
|
|
|
#++
|
|
|
|
#
|
|
|
|
# == Overview
|
|
|
|
#
|
|
|
|
# This file provides two classes for working with
|
|
|
|
# dates and times.
|
2004-01-18 17:01:12 -05:00
|
|
|
#
|
2006-12-30 10:27:01 -05:00
|
|
|
# The first class, Date, represents dates.
|
2003-08-23 08:00:21 -04:00
|
|
|
# It works with years, months, weeks, and days.
|
|
|
|
# See the Date class documentation for more details.
|
|
|
|
#
|
|
|
|
# The second, DateTime, extends Date to include hours,
|
|
|
|
# minutes, seconds, and fractions of a second. It
|
|
|
|
# provides basic support for time zones. See the
|
|
|
|
# DateTime class documentation for more details.
|
|
|
|
#
|
|
|
|
# === Ways of calculating the date.
|
|
|
|
#
|
|
|
|
# In common usage, the date is reckoned in years since or
|
|
|
|
# before the Common Era (CE/BCE, also known as AD/BC), then
|
|
|
|
# as a month and day-of-the-month within the current year.
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# This is known as the *Civil* *Date*, and abbreviated
|
2003-08-23 08:00:21 -04:00
|
|
|
# as +civil+ in the Date class.
|
|
|
|
#
|
2004-01-18 17:01:12 -05:00
|
|
|
# Instead of year, month-of-the-year, and day-of-the-month,
|
|
|
|
# the date can also be reckoned in terms of year and
|
2003-08-23 08:00:21 -04:00
|
|
|
# day-of-the-year. This is known as the *Ordinal* *Date*,
|
|
|
|
# and is abbreviated as +ordinal+ in the Date class. (Note
|
|
|
|
# that referring to this as the Julian date is incorrect.)
|
|
|
|
#
|
|
|
|
# The date can also be reckoned in terms of year, week-of-the-year,
|
|
|
|
# and day-of-the-week. This is known as the *Commercial*
|
|
|
|
# *Date*, and is abbreviated as +commercial+ in the
|
|
|
|
# Date class. The commercial week runs Monday (day-of-the-week
|
|
|
|
# 1) to Sunday (day-of-the-week 7), in contrast to the civil
|
|
|
|
# week which runs Sunday (day-of-the-week 0) to Saturday
|
|
|
|
# (day-of-the-week 6). The first week of the commercial year
|
|
|
|
# starts on the Monday on or before January 1, and the commercial
|
|
|
|
# year itself starts on this Monday, not January 1.
|
|
|
|
#
|
|
|
|
# For scientific purposes, it is convenient to refer to a date
|
|
|
|
# simply as a day count, counting from an arbitrary initial
|
|
|
|
# day. The date first chosen for this was January 1, 4713 BCE.
|
|
|
|
# A count of days from this date is the *Julian* *Day* *Number*
|
|
|
|
# or *Julian* *Date*, which is abbreviated as +jd+ in the
|
|
|
|
# Date class. This is in local time, and counts from midnight
|
|
|
|
# on the initial day. The stricter usage is in UTC, and counts
|
|
|
|
# from midday on the initial day. This is referred to in the
|
|
|
|
# Date class as the *Astronomical* *Julian* *Day* *Number*, and
|
|
|
|
# abbreviated as +ajd+. In the Date class, the Astronomical
|
|
|
|
# Julian Day Number includes fractional days.
|
2004-01-18 17:01:12 -05:00
|
|
|
#
|
2003-08-23 08:00:21 -04:00
|
|
|
# Another absolute day count is the *Modified* *Julian* *Day*
|
|
|
|
# *Number*, which takes November 17, 1858 as its initial day.
|
|
|
|
# This is abbreviated as +mjd+ in the Date class. There
|
|
|
|
# is also an *Astronomical* *Modified* *Julian* *Day* *Number*,
|
|
|
|
# which is in UTC and includes fractional days. This is
|
|
|
|
# abbreviated as +amjd+ in the Date class. Like the Modified
|
|
|
|
# Julian Day Number (and unlike the Astronomical Julian
|
|
|
|
# Day Number), it counts from midnight.
|
|
|
|
#
|
|
|
|
# Alternative calendars such as the Chinese Lunar Calendar,
|
|
|
|
# the Islamic Calendar, or the French Revolutionary Calendar
|
|
|
|
# are not supported by the Date class; nor are calendars that
|
|
|
|
# are based on an Era different from the Common Era, such as
|
|
|
|
# the Japanese Imperial Calendar or the Republic of China
|
|
|
|
# Calendar.
|
|
|
|
#
|
|
|
|
# === Calendar Reform
|
|
|
|
#
|
|
|
|
# The standard civil year is 365 days long. However, the
|
|
|
|
# solar year is fractionally longer than this. To account
|
|
|
|
# for this, a *leap* *year* is occasionally inserted. This
|
2006-12-30 10:27:01 -05:00
|
|
|
# is a year with 366 days, the extra day falling on February 29.
|
2003-08-23 08:00:21 -04:00
|
|
|
# In the early days of the civil calendar, every fourth
|
|
|
|
# year without exception was a leap year. This way of
|
|
|
|
# reckoning leap years is the *Julian* *Calendar*.
|
|
|
|
#
|
|
|
|
# However, the solar year is marginally shorter than 365 1/4
|
|
|
|
# days, and so the *Julian* *Calendar* gradually ran slow
|
|
|
|
# over the centuries. To correct this, every 100th year
|
|
|
|
# (but not every 400th year) was excluded as a leap year.
|
|
|
|
# This way of reckoning leap years, which we use today, is
|
|
|
|
# the *Gregorian* *Calendar*.
|
|
|
|
#
|
|
|
|
# The Gregorian Calendar was introduced at different times
|
|
|
|
# in different regions. The day on which it was introduced
|
2004-01-18 17:01:12 -05:00
|
|
|
# for a particular region is the *Day* *of* *Calendar*
|
2003-08-23 08:00:21 -04:00
|
|
|
# *Reform* for that region. This is abbreviated as +sg+
|
|
|
|
# (for Start of Gregorian calendar) in the Date class.
|
|
|
|
#
|
|
|
|
# Two such days are of particular
|
|
|
|
# significance. The first is October 15, 1582, which was
|
|
|
|
# the Day of Calendar Reform for Italy and most Catholic
|
|
|
|
# countries. The second is September 14, 1752, which was
|
|
|
|
# the Day of Calendar Reform for England and its colonies
|
2004-01-18 17:01:12 -05:00
|
|
|
# (including what is now the United States). These two
|
2003-08-23 08:00:21 -04:00
|
|
|
# dates are available as the constants Date::ITALY and
|
|
|
|
# Date::ENGLAND, respectively. (By comparison, Germany and
|
|
|
|
# Holland, less Catholic than Italy but less stubborn than
|
|
|
|
# England, changed over in 1698; Sweden in 1753; Russia not
|
|
|
|
# till 1918, after the Revolution; and Greece in 1923. Many
|
|
|
|
# Orthodox churches still use the Julian Calendar. A complete
|
2004-01-18 17:01:12 -05:00
|
|
|
# list of Days of Calendar Reform can be found at
|
2003-08-23 08:00:21 -04:00
|
|
|
# http://www.polysyllabic.com/GregConv.html.)
|
|
|
|
#
|
|
|
|
# Switching from the Julian to the Gregorian calendar
|
|
|
|
# involved skipping a number of days to make up for the
|
|
|
|
# accumulated lag, and the later the switch was (or is)
|
|
|
|
# done, the more days need to be skipped. So in 1582 in Italy,
|
2004-01-18 17:01:12 -05:00
|
|
|
# 4th October was followed by 15th October, skipping 10 days; in 1752
|
2003-08-23 08:00:21 -04:00
|
|
|
# in England, 2nd September was followed by 14th September, skipping
|
2004-01-18 17:01:12 -05:00
|
|
|
# 11 days; and if I decided to switch from Julian to Gregorian
|
|
|
|
# Calendar this midnight, I would go from 27th July 2003 (Julian)
|
2003-08-23 08:00:21 -04:00
|
|
|
# today to 10th August 2003 (Gregorian) tomorrow, skipping
|
|
|
|
# 13 days. The Date class is aware of this gap, and a supposed
|
|
|
|
# date that would fall in the middle of it is regarded as invalid.
|
|
|
|
#
|
|
|
|
# The Day of Calendar Reform is relevant to all date representations
|
|
|
|
# involving years. It is not relevant to the Julian Day Numbers,
|
|
|
|
# except for converting between them and year-based representations.
|
|
|
|
#
|
|
|
|
# In the Date and DateTime classes, the Day of Calendar Reform or
|
|
|
|
# +sg+ can be specified a number of ways. First, it can be as
|
|
|
|
# the Julian Day Number of the Day of Calendar Reform. Second,
|
|
|
|
# it can be using the constants Date::ITALY or Date::ENGLAND; these
|
|
|
|
# are in fact the Julian Day Numbers of the Day of Calendar Reform
|
|
|
|
# of the respective regions. Third, it can be as the constant
|
|
|
|
# Date::JULIAN, which means to always use the Julian Calendar.
|
|
|
|
# Finally, it can be as the constant Date::GREGORIAN, which means
|
|
|
|
# to always use the Gregorian Calendar.
|
|
|
|
#
|
|
|
|
# Note: in the Julian Calendar, New Years Day was March 25. The
|
|
|
|
# Date class does not follow this convention.
|
|
|
|
#
|
|
|
|
# === Time Zones
|
|
|
|
#
|
|
|
|
# DateTime objects support a simple representation
|
|
|
|
# of time zones. Time zones are represented as an offset
|
|
|
|
# from UTC, as a fraction of a day. This offset is the
|
|
|
|
# how much local time is later (or earlier) than UTC.
|
2006-12-30 10:27:01 -05:00
|
|
|
# UTC offset 0 is centred on England (also known as GMT).
|
2003-08-23 08:00:21 -04:00
|
|
|
# As you travel east, the offset increases until you
|
|
|
|
# reach the dateline in the middle of the Pacific Ocean;
|
|
|
|
# as you travel west, the offset decreases. This offset
|
|
|
|
# is abbreviated as +of+ in the Date class.
|
|
|
|
#
|
2004-01-18 17:01:12 -05:00
|
|
|
# This simple representation of time zones does not take
|
2003-08-23 08:00:21 -04:00
|
|
|
# into account the common practice of Daylight Savings
|
|
|
|
# Time or Summer Time.
|
|
|
|
#
|
|
|
|
# Most DateTime methods return the date and the
|
|
|
|
# time in local time. The two exceptions are
|
|
|
|
# #ajd() and #amjd(), which return the date and time
|
|
|
|
# in UTC time, including fractional days.
|
|
|
|
#
|
|
|
|
# The Date class does not support time zone offsets, in that
|
|
|
|
# there is no way to create a Date object with a time zone.
|
|
|
|
# However, methods of the Date class when used by a
|
|
|
|
# DateTime instance will use the time zone offset of this
|
|
|
|
# instance.
|
|
|
|
#
|
|
|
|
# == Examples of use
|
|
|
|
#
|
|
|
|
# === Print out the date of every Sunday between two dates.
|
|
|
|
#
|
|
|
|
# def print_sundays(d1, d2)
|
|
|
|
# d1 +=1 while (d1.wday != 0)
|
|
|
|
# d1.step(d2, 7) do |date|
|
|
|
|
# puts "#{Date::MONTHNAMES[date.mon]} #{date.day}"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# print_sundays(Date::civil(2003, 4, 8), Date::civil(2003, 5, 23))
|
|
|
|
#
|
|
|
|
# === Calculate how many seconds to go till midnight on New Year's Day.
|
|
|
|
#
|
|
|
|
# def secs_to_new_year(now = DateTime::now())
|
|
|
|
# new_year = DateTime.new(now.year + 1, 1, 1)
|
|
|
|
# dif = new_year - now
|
|
|
|
# hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(dif)
|
|
|
|
# return hours * 60 * 60 + mins * 60 + secs
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# puts secs_to_new_year()
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
require 'date/format'
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Class representing a date.
|
|
|
|
#
|
|
|
|
# See the documentation to the file date.rb for an overview.
|
|
|
|
#
|
|
|
|
# Internally, the date is represented as an Astronomical
|
2004-01-18 17:01:12 -05:00
|
|
|
# Julian Day Number, +ajd+. The Day of Calendar Reform, +sg+, is
|
2003-08-23 08:00:21 -04:00
|
|
|
# also stored, for conversions to other date formats. (There
|
|
|
|
# is also an +of+ field for a time zone offset, but this
|
|
|
|
# is only for the use of the DateTime subclass.)
|
|
|
|
#
|
|
|
|
# A new Date object is created using one of the object creation
|
|
|
|
# class methods named after the corresponding date format, and the
|
|
|
|
# arguments appropriate to that date format; for instance,
|
|
|
|
# Date::civil() (aliased to Date::new()) with year, month,
|
|
|
|
# and day-of-month, or Date::ordinal() with year and day-of-year.
|
|
|
|
# All of these object creation class methods also take the
|
|
|
|
# Day of Calendar Reform as an optional argument.
|
|
|
|
#
|
|
|
|
# Date objects are immutable once created.
|
|
|
|
#
|
|
|
|
# Once a Date has been created, date values
|
2004-01-18 17:01:12 -05:00
|
|
|
# can be retrieved for the different date formats supported
|
2003-08-23 08:00:21 -04:00
|
|
|
# using instance methods. For instance, #mon() gives the
|
|
|
|
# Civil month, #cwday() gives the Commercial day of the week,
|
|
|
|
# and #yday() gives the Ordinal day of the year. Date values
|
|
|
|
# can be retrieved in any format, regardless of what format
|
|
|
|
# was used to create the Date instance.
|
|
|
|
#
|
|
|
|
# The Date class includes the Comparable module, allowing
|
|
|
|
# date objects to be compared and sorted, ranges of dates
|
|
|
|
# to be created, and so forth.
|
1998-01-16 07:13:05 -05:00
|
|
|
class Date
|
2000-07-18 03:37:02 -04:00
|
|
|
|
1998-01-16 07:13:05 -05:00
|
|
|
include Comparable
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Full month names, in English. Months count from 1 to 12; a
|
|
|
|
# month's numerical representation indexed into this array
|
|
|
|
# gives the name of that month (hence the first element is nil).
|
2000-07-18 03:37:02 -04:00
|
|
|
MONTHNAMES = [nil] + %w(January February March April May June July
|
|
|
|
August September October November December)
|
|
|
|
|
2004-01-18 17:01:12 -05:00
|
|
|
# Full names of days of the week, in English. Days of the week
|
2003-08-23 08:00:21 -04:00
|
|
|
# count from 0 to 6 (except in the commercial week); a day's numerical
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# representation indexed into this array gives the name of that day.
|
2000-07-18 03:37:02 -04:00
|
|
|
DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Abbreviated month names, in English.
|
2002-05-18 08:41:51 -04:00
|
|
|
ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun
|
|
|
|
Jul Aug Sep Oct Nov Dec)
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Abbreviated day names, in English.
|
2002-05-18 08:41:51 -04:00
|
|
|
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
|
|
|
|
|
2006-09-22 12:05:19 -04:00
|
|
|
[MONTHNAMES, DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYNAMES].each do |xs|
|
2007-12-22 00:48:30 -05:00
|
|
|
xs.each{|x| x.freeze unless x.nil?}.freeze
|
2006-09-22 12:05:19 -04:00
|
|
|
end
|
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
class Infinity < Numeric # :nodoc:
|
|
|
|
|
|
|
|
include Comparable
|
|
|
|
|
|
|
|
def initialize(d=1) @d = d <=> 0 end
|
|
|
|
|
|
|
|
def d() @d end
|
|
|
|
|
|
|
|
protected :d
|
|
|
|
|
|
|
|
def zero? () false end
|
|
|
|
def finite? () false end
|
|
|
|
def infinite? () d.nonzero? end
|
|
|
|
def nan? () d.zero? end
|
|
|
|
|
|
|
|
def abs() self.class.new end
|
|
|
|
|
|
|
|
def -@ () self.class.new(-d) end
|
|
|
|
def +@ () self.class.new(+d) end
|
|
|
|
|
|
|
|
def <=> (other)
|
|
|
|
case other
|
2008-01-17 07:05:43 -05:00
|
|
|
when Infinity; return d <=> other.d
|
|
|
|
when Numeric; return d
|
2006-08-25 19:09:30 -04:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
l, r = other.coerce(self)
|
|
|
|
return l <=> r
|
|
|
|
rescue NoMethodError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def coerce(other)
|
|
|
|
case other
|
|
|
|
when Numeric; return -d, d
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# The Julian Day Number of the Day of Calendar Reform for Italy
|
|
|
|
# and the Catholic countries.
|
2000-07-18 03:37:02 -04:00
|
|
|
ITALY = 2299161 # 1582-10-15
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# The Julian Day Number of the Day of Calendar Reform for England
|
|
|
|
# and her Colonies.
|
2000-07-18 03:37:02 -04:00
|
|
|
ENGLAND = 2361222 # 1752-09-14
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# A constant used to indicate that a Date should always use the
|
|
|
|
# Julian calendar.
|
2006-08-25 19:09:30 -04:00
|
|
|
JULIAN = Infinity.new
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# A constant used to indicate that a Date should always use the
|
|
|
|
# Gregorian calendar.
|
2006-08-25 19:09:30 -04:00
|
|
|
GREGORIAN = -Infinity.new
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2008-01-05 19:38:22 -05:00
|
|
|
HALF_DAYS_IN_DAY = Rational(1, 2) # :nodoc:
|
|
|
|
HOURS_IN_DAY = Rational(1, 24) # :nodoc:
|
|
|
|
MINUTES_IN_DAY = Rational(1, 1440) # :nodoc:
|
|
|
|
SECONDS_IN_DAY = Rational(1, 86400) # :nodoc:
|
|
|
|
MILLISECONDS_IN_DAY = Rational(1, 86400*10**3) # :nodoc:
|
|
|
|
NANOSECONDS_IN_DAY = Rational(1, 86400*10**9) # :nodoc:
|
|
|
|
MILLISECONDS_IN_SECOND = Rational(1, 10**3) # :nodoc:
|
|
|
|
NANOSECONDS_IN_SECOND = Rational(1, 10**9) # :nodoc:
|
|
|
|
|
|
|
|
MJD_EPOCH_IN_AJD = Rational(4800001, 2) # 1858-11-17 # :nodoc:
|
|
|
|
UNIX_EPOCH_IN_AJD = Rational(4881175, 2) # 1970-01-01 # :nodoc:
|
|
|
|
MJD_EPOCH_IN_CJD = 2400001 # :nodoc:
|
|
|
|
UNIX_EPOCH_IN_CJD = 2440588 # :nodoc:
|
|
|
|
LD_EPOCH_IN_CJD = 2299160 # :nodoc:
|
2006-09-22 12:05:19 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
t = Module.new do
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_fdoy(y, sg) # :nodoc:
|
|
|
|
j = nil
|
|
|
|
1.upto(31) do |d|
|
|
|
|
break if j = _valid_civil?(y, 1, d, sg)
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
j
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def find_ldoy(y, sg) # :nodoc:
|
|
|
|
j = nil
|
|
|
|
31.downto(1) do |d|
|
|
|
|
break if j = _valid_civil?(y, 12, d, sg)
|
|
|
|
end
|
|
|
|
j
|
|
|
|
end
|
2006-08-25 19:09:30 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def find_fdom(y, m, sg) # :nodoc:
|
|
|
|
j = nil
|
|
|
|
1.upto(31) do |d|
|
|
|
|
break if j = _valid_civil?(y, m, d, sg)
|
|
|
|
end
|
|
|
|
j
|
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def find_ldom(y, m, sg) # :nodoc:
|
|
|
|
j = nil
|
|
|
|
31.downto(1) do |d|
|
|
|
|
break if j = _valid_civil?(y, m, d, sg)
|
|
|
|
end
|
|
|
|
j
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert an Ordinal Date to a Julian Day Number.
|
|
|
|
#
|
|
|
|
# +y+ and +d+ are the year and day-of-year to convert.
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# Returns the corresponding Julian Day Number.
|
|
|
|
def ordinal_to_jd(y, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
find_fdoy(y, sg) + d - 1
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert a Julian Day Number to an Ordinal Date.
|
|
|
|
#
|
|
|
|
# +jd+ is the Julian Day Number to convert.
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# Returns the corresponding Ordinal Date as
|
|
|
|
# [year, day_of_year]
|
|
|
|
def jd_to_ordinal(jd, sg=GREGORIAN) # :nodoc:
|
|
|
|
y = jd_to_civil(jd, sg)[0]
|
|
|
|
j = find_fdoy(y, sg)
|
|
|
|
doy = jd - j + 1
|
|
|
|
return y, doy
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a Civil Date to a Julian Day Number.
|
|
|
|
# +y+, +m+, and +d+ are the year, month, and day of the
|
|
|
|
# month. +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# Returns the corresponding Julian Day Number.
|
|
|
|
def civil_to_jd(y, m, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
if m <= 2
|
|
|
|
y -= 1
|
|
|
|
m += 12
|
|
|
|
end
|
|
|
|
a = (y / 100.0).floor
|
|
|
|
b = 2 - a + (a / 4.0).floor
|
|
|
|
jd = (365.25 * (y + 4716)).floor +
|
|
|
|
(30.6001 * (m + 1)).floor +
|
|
|
|
d + b - 1524
|
|
|
|
if jd < sg
|
|
|
|
jd -= b
|
|
|
|
end
|
|
|
|
jd
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert a Julian Day Number to a Civil Date. +jd+ is
|
|
|
|
# the Julian Day Number. +sg+ specifies the Day of
|
|
|
|
# Calendar Reform.
|
|
|
|
#
|
|
|
|
# Returns the corresponding [year, month, day_of_month]
|
|
|
|
# as a three-element array.
|
|
|
|
def jd_to_civil(jd, sg=GREGORIAN) # :nodoc:
|
|
|
|
if jd < sg
|
|
|
|
a = jd
|
|
|
|
else
|
|
|
|
x = ((jd - 1867216.25) / 36524.25).floor
|
|
|
|
a = jd + 1 + x - (x / 4.0).floor
|
|
|
|
end
|
|
|
|
b = a + 1524
|
|
|
|
c = ((b - 122.1) / 365.25).floor
|
|
|
|
d = (365.25 * c).floor
|
|
|
|
e = ((b - d) / 30.6001).floor
|
|
|
|
dom = b - d - (30.6001 * e).floor
|
|
|
|
if e <= 13
|
|
|
|
m = e - 1
|
|
|
|
y = c - 4716
|
|
|
|
else
|
|
|
|
m = e - 13
|
|
|
|
y = c - 4715
|
|
|
|
end
|
|
|
|
return y, m, dom
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert a Commercial Date to a Julian Day Number.
|
|
|
|
#
|
|
|
|
# +y+, +w+, and +d+ are the (commercial) year, week of the year,
|
|
|
|
# and day of the week of the Commercial Date to convert.
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
def commercial_to_jd(y, w, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
j = find_fdoy(y, sg) + 3
|
|
|
|
(j - (((j - 1) + 1) % 7)) +
|
|
|
|
7 * (w - 1) +
|
|
|
|
(d - 1)
|
|
|
|
end
|
2006-09-22 12:05:19 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert a Julian Day Number to a Commercial Date
|
|
|
|
#
|
|
|
|
# +jd+ is the Julian Day Number to convert.
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# Returns the corresponding Commercial Date as
|
|
|
|
# [commercial_year, week_of_year, day_of_week]
|
|
|
|
def jd_to_commercial(jd, sg=GREGORIAN) # :nodoc:
|
|
|
|
a = jd_to_civil(jd - 3, sg)[0]
|
|
|
|
y = if jd >= commercial_to_jd(a + 1, 1, 1, sg) then a + 1 else a end
|
|
|
|
w = 1 + ((jd - commercial_to_jd(y, 1, 1, sg)) / 7).floor
|
|
|
|
d = (jd + 1) % 7
|
|
|
|
d = 7 if d == 0
|
|
|
|
return y, w, d
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def weeknum_to_jd(y, w, d, f=0, sg=GREGORIAN) # :nodoc:
|
|
|
|
a = find_fdoy(y, sg) + 6
|
|
|
|
(a - ((a - f) + 1) % 7 - 7) + 7 * w + d
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def jd_to_weeknum(jd, f=0, sg=GREGORIAN) # :nodoc:
|
|
|
|
y, m, d = jd_to_civil(jd, sg)
|
|
|
|
a = find_fdoy(y, sg) + 6
|
|
|
|
w, d = (jd - (a - ((a - f) + 1) % 7) + 7).divmod(7)
|
|
|
|
return y, w, d
|
|
|
|
end
|
2006-08-25 19:09:30 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def nth_kday_to_jd(y, m, n, k, sg=GREGORIAN) # :nodoc:
|
|
|
|
j = if n > 0
|
|
|
|
find_fdom(y, m, sg) - 1
|
|
|
|
else
|
|
|
|
find_ldom(y, m, sg) + 7
|
|
|
|
end
|
|
|
|
(j - (((j - k) + 1) % 7)) + 7 * n
|
|
|
|
end
|
2006-08-25 19:09:30 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def jd_to_nth_kday(jd, sg=GREGORIAN) # :nodoc:
|
|
|
|
y, m, d = jd_to_civil(jd, sg)
|
|
|
|
j = find_fdom(y, m, sg)
|
|
|
|
return y, m, ((jd - j) / 7).floor + 1, jd_to_wday(jd)
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert an Astronomical Julian Day Number to a (civil) Julian
|
|
|
|
# Day Number.
|
|
|
|
#
|
|
|
|
# +ajd+ is the Astronomical Julian Day Number to convert.
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
#
|
|
|
|
# Returns the (civil) Julian Day Number as [day_number,
|
|
|
|
# fraction] where +fraction+ is always 1/2.
|
2008-01-05 19:38:22 -05:00
|
|
|
def ajd_to_jd(ajd, of=0) (ajd + of + HALF_DAYS_IN_DAY).divmod(1) end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a (civil) Julian Day Number to an Astronomical Julian
|
|
|
|
# Day Number.
|
|
|
|
#
|
|
|
|
# +jd+ is the Julian Day Number to convert, and +fr+ is a
|
|
|
|
# fractional day.
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
#
|
|
|
|
# Returns the Astronomical Julian Day Number as a single
|
|
|
|
# numeric value.
|
2008-01-05 19:38:22 -05:00
|
|
|
def jd_to_ajd(jd, fr, of=0) jd + fr - of - HALF_DAYS_IN_DAY end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a fractional day +fr+ to [hours, minutes, seconds,
|
|
|
|
# fraction_of_a_second]
|
|
|
|
def day_fraction_to_time(fr) # :nodoc:
|
2008-01-11 22:03:31 -05:00
|
|
|
ss, fr = fr.divmod(SECONDS_IN_DAY) # 4p
|
|
|
|
h, ss = ss.divmod(3600)
|
|
|
|
min, s = ss.divmod(60)
|
2006-12-30 10:27:01 -05:00
|
|
|
return h, min, s, fr * 86400
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert an +h+ hour, +min+ minutes, +s+ seconds period
|
|
|
|
# to a fractional day.
|
2008-01-05 19:38:22 -05:00
|
|
|
begin
|
|
|
|
Rational(Rational(1, 2), 2) # a challenge
|
|
|
|
|
2008-01-11 22:03:31 -05:00
|
|
|
def time_to_day_fraction(h, min, s)
|
|
|
|
Rational(h * 3600 + min * 60 + s, 86400) # 4p
|
2008-01-05 19:38:22 -05:00
|
|
|
end
|
|
|
|
rescue
|
2008-01-11 22:03:31 -05:00
|
|
|
def time_to_day_fraction(h, min, s)
|
|
|
|
if Integer === h && Integer === min && Integer === s
|
|
|
|
Rational(h * 3600 + min * 60 + s, 86400) # 4p
|
|
|
|
else
|
2008-01-17 07:05:43 -05:00
|
|
|
(h * 3600 + min * 60 + s).to_r/86400 # 4p
|
2008-01-11 22:03:31 -05:00
|
|
|
end
|
2008-01-05 19:38:22 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Convert an Astronomical Modified Julian Day Number to an
|
|
|
|
# Astronomical Julian Day Number.
|
2008-01-05 19:38:22 -05:00
|
|
|
def amjd_to_ajd(amjd) amjd + MJD_EPOCH_IN_AJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert an Astronomical Julian Day Number to an
|
|
|
|
# Astronomical Modified Julian Day Number.
|
2008-01-05 19:38:22 -05:00
|
|
|
def ajd_to_amjd(ajd) ajd - MJD_EPOCH_IN_AJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a Modified Julian Day Number to a Julian
|
|
|
|
# Day Number.
|
2008-01-05 19:38:22 -05:00
|
|
|
def mjd_to_jd(mjd) mjd + MJD_EPOCH_IN_CJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a Julian Day Number to a Modified Julian Day
|
|
|
|
# Number.
|
2008-01-05 19:38:22 -05:00
|
|
|
def jd_to_mjd(jd) jd - MJD_EPOCH_IN_CJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a count of the number of days since the adoption
|
|
|
|
# of the Gregorian Calendar (in Italy) to a Julian Day Number.
|
2008-01-05 19:38:22 -05:00
|
|
|
def ld_to_jd(ld) ld + LD_EPOCH_IN_CJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a Julian Day Number to the number of days since
|
|
|
|
# the adoption of the Gregorian Calendar (in Italy).
|
2008-01-05 19:38:22 -05:00
|
|
|
def jd_to_ld(jd) jd - LD_EPOCH_IN_CJD end # :nodoc:
|
2006-12-30 10:27:01 -05:00
|
|
|
|
|
|
|
# Convert a Julian Day Number to the day of the week.
|
|
|
|
#
|
|
|
|
# Sunday is day-of-week 0; Saturday is day-of-week 6.
|
|
|
|
def jd_to_wday(jd) (jd + 1) % 7 end # :nodoc:
|
|
|
|
|
|
|
|
# Is +jd+ a valid Julian Day Number?
|
|
|
|
#
|
|
|
|
# If it is, returns it. In fact, any value is treated as a valid
|
|
|
|
# Julian Day Number.
|
|
|
|
def _valid_jd? (jd, sg=GREGORIAN) jd end # :nodoc:
|
|
|
|
|
|
|
|
# Do the year +y+ and day-of-year +d+ make a valid Ordinal Date?
|
|
|
|
# Returns the corresponding Julian Day Number if they do, or
|
|
|
|
# nil if they don't.
|
|
|
|
#
|
|
|
|
# +d+ can be a negative number, in which case it counts backwards
|
|
|
|
# from the end of the year (-1 being the last day of the year).
|
|
|
|
# No year wraparound is performed, however, so valid values of
|
|
|
|
# +d+ are -365 .. -1, 1 .. 365 on a non-leap-year,
|
|
|
|
# -366 .. -1, 1 .. 366 on a leap year.
|
|
|
|
# A date falling in the period skipped in the Day of Calendar Reform
|
|
|
|
# adjustment is not valid.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
def _valid_ordinal? (y, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
if d < 0
|
|
|
|
j = find_ldoy(y, sg)
|
|
|
|
ny, nd = jd_to_ordinal(j + d + 1, sg)
|
|
|
|
return unless ny == y
|
|
|
|
d = nd
|
|
|
|
end
|
|
|
|
jd = ordinal_to_jd(y, d, sg)
|
|
|
|
return unless [y, d] == jd_to_ordinal(jd, sg)
|
|
|
|
jd
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Do year +y+, month +m+, and day-of-month +d+ make a
|
|
|
|
# valid Civil Date? Returns the corresponding Julian
|
|
|
|
# Day Number if they do, nil if they don't.
|
|
|
|
#
|
|
|
|
# +m+ and +d+ can be negative, in which case they count
|
|
|
|
# backwards from the end of the year and the end of the
|
|
|
|
# month respectively. No wraparound is performed, however,
|
|
|
|
# and invalid values cause an ArgumentError to be raised.
|
|
|
|
# A date falling in the period skipped in the Day of Calendar
|
|
|
|
# Reform adjustment is not valid.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
def _valid_civil? (y, m, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
if m < 0
|
|
|
|
m += 13
|
|
|
|
end
|
|
|
|
if d < 0
|
|
|
|
j = find_ldom(y, m, sg)
|
|
|
|
ny, nm, nd = jd_to_civil(j + d + 1, sg)
|
|
|
|
return unless [ny, nm] == [y, m]
|
|
|
|
d = nd
|
|
|
|
end
|
|
|
|
jd = civil_to_jd(y, m, d, sg)
|
|
|
|
return unless [y, m, d] == jd_to_civil(jd, sg)
|
|
|
|
jd
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Do year +y+, week-of-year +w+, and day-of-week +d+ make a
|
|
|
|
# valid Commercial Date? Returns the corresponding Julian
|
|
|
|
# Day Number if they do, nil if they don't.
|
|
|
|
#
|
|
|
|
# Monday is day-of-week 1; Sunday is day-of-week 7.
|
|
|
|
#
|
|
|
|
# +w+ and +d+ can be negative, in which case they count
|
|
|
|
# backwards from the end of the year and the end of the
|
|
|
|
# week respectively. No wraparound is performed, however,
|
|
|
|
# and invalid values cause an ArgumentError to be raised.
|
|
|
|
# A date falling in the period skipped in the Day of Calendar
|
|
|
|
# Reform adjustment is not valid.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
def _valid_commercial? (y, w, d, sg=GREGORIAN) # :nodoc:
|
|
|
|
if d < 0
|
|
|
|
d += 8
|
|
|
|
end
|
|
|
|
if w < 0
|
|
|
|
ny, nw, nd =
|
|
|
|
jd_to_commercial(commercial_to_jd(y + 1, 1, 1, sg) + w * 7, sg)
|
|
|
|
return unless ny == y
|
|
|
|
w = nw
|
|
|
|
end
|
|
|
|
jd = commercial_to_jd(y, w, d, sg)
|
|
|
|
return unless [y, w, d] == jd_to_commercial(jd, sg)
|
|
|
|
jd
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def _valid_weeknum? (y, w, d, f, sg=GREGORIAN) # :nodoc:
|
|
|
|
if d < 0
|
|
|
|
d += 7
|
|
|
|
end
|
|
|
|
if w < 0
|
|
|
|
ny, nw, nd, nf =
|
|
|
|
jd_to_weeknum(weeknum_to_jd(y + 1, 1, f, f, sg) + w * 7, f, sg)
|
|
|
|
return unless ny == y
|
|
|
|
w = nw
|
|
|
|
end
|
|
|
|
jd = weeknum_to_jd(y, w, d, f, sg)
|
|
|
|
return unless [y, w, d] == jd_to_weeknum(jd, f, sg)
|
|
|
|
jd
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def _valid_nth_kday? (y, m, n, k, sg=GREGORIAN) # :nodoc:
|
|
|
|
if k < 0
|
|
|
|
k += 7
|
|
|
|
end
|
|
|
|
if n < 0
|
|
|
|
ny, nm = (y * 12 + m).divmod(12)
|
|
|
|
nm, = (nm + 1) .divmod(1)
|
|
|
|
ny, nm, nn, nk =
|
|
|
|
jd_to_nth_kday(nth_kday_to_jd(ny, nm, 1, k, sg) + n * 7, sg)
|
|
|
|
return unless [ny, nm] == [y, m]
|
|
|
|
n = nn
|
|
|
|
end
|
|
|
|
jd = nth_kday_to_jd(y, m, n, k, sg)
|
|
|
|
return unless [y, m, n, k] == jd_to_nth_kday(jd, sg)
|
|
|
|
jd
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# Do hour +h+, minute +min+, and second +s+ constitute a valid time?
|
|
|
|
#
|
|
|
|
# If they do, returns their value as a fraction of a day. If not,
|
|
|
|
# returns nil.
|
|
|
|
#
|
|
|
|
# The 24-hour clock is used. Negative values of +h+, +min+, and
|
|
|
|
# +sec+ are treating as counting backwards from the end of the
|
|
|
|
# next larger unit (e.g. a +min+ of -2 is treated as 58). No
|
|
|
|
# wraparound is performed.
|
|
|
|
def _valid_time? (h, min, s) # :nodoc:
|
|
|
|
h += 24 if h < 0
|
|
|
|
min += 60 if min < 0
|
|
|
|
s += 60 if s < 0
|
2007-12-30 08:48:03 -05:00
|
|
|
return unless ((0...24) === h &&
|
|
|
|
(0...60) === min &&
|
|
|
|
(0...60) === s) ||
|
2006-12-30 10:27:01 -05:00
|
|
|
(24 == h &&
|
|
|
|
0 == min &&
|
|
|
|
0 == s)
|
|
|
|
time_to_day_fraction(h, min, s)
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
extend t
|
|
|
|
include t
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Is a year a leap year in the Julian calendar?
|
|
|
|
#
|
|
|
|
# All years divisible by 4 are leap years in the Julian calendar.
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.julian_leap? (y) y % 4 == 0 end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Is a year a leap year in the Gregorian calendar?
|
|
|
|
#
|
|
|
|
# All years divisible by 4 are leap years in the Gregorian calendar,
|
|
|
|
# except for years divisible by 100 and not by 400.
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.gregorian_leap? (y) y % 4 == 0 && y % 100 != 0 || y % 400 == 0 end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
class << self; alias_method :leap?, :gregorian_leap? end
|
2006-12-30 10:27:01 -05:00
|
|
|
class << self; alias_method :new!, :new end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.valid_jd? (jd, sg=ITALY)
|
|
|
|
!!_valid_jd?(jd, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_ordinal? (y, d, sg=ITALY)
|
|
|
|
!!_valid_ordinal?(y, d, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_civil? (y, m, d, sg=ITALY)
|
|
|
|
!!_valid_civil?(y, m, d, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self; alias_method :valid_date?, :valid_civil? end
|
|
|
|
|
|
|
|
def self.valid_commercial? (y, w, d, sg=ITALY)
|
|
|
|
!!_valid_commercial?(y, w, d, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.valid_weeknum? (y, w, d, f, sg=ITALY) # :nodoc:
|
|
|
|
!!_valid_weeknum?(y, w, d, f, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :valid_weeknum?
|
|
|
|
|
|
|
|
def self.valid_nth_kday? (y, m, n, k, sg=ITALY) # :nodoc:
|
|
|
|
!!_valid_nth_kday?(y, m, n, k, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :valid_nth_kday?
|
|
|
|
|
|
|
|
def self.valid_time? (h, min, s) # :nodoc:
|
|
|
|
!!_valid_time?(h, min, s)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :valid_time?
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new Date object from a Julian Day Number.
|
|
|
|
#
|
|
|
|
# +jd+ is the Julian Day Number; if not specified, it defaults to
|
2006-12-30 10:27:01 -05:00
|
|
|
# 0.
|
2003-08-23 08:00:21 -04:00
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.jd(jd=0, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
jd = _valid_jd?(jd, sg)
|
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new Date object from an Ordinal Date, specified
|
|
|
|
# by year +y+ and day-of-year +d+. +d+ can be negative,
|
|
|
|
# in which it counts backwards from the end of the year.
|
|
|
|
# No year wraparound is performed, however. An invalid
|
|
|
|
# value for +d+ results in an ArgumentError being raised.
|
|
|
|
#
|
|
|
|
# +y+ defaults to -4712, and +d+ to 1; this is Julian Day
|
|
|
|
# Number day 0.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.ordinal(y=-4712, d=1, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
unless jd = _valid_ordinal?(y, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new Date object for the Civil Date specified by
|
|
|
|
# year +y+, month +m+, and day-of-month +d+.
|
|
|
|
#
|
|
|
|
# +m+ and +d+ can be negative, in which case they count
|
|
|
|
# backwards from the end of the year and the end of the
|
|
|
|
# month respectively. No wraparound is performed, however,
|
|
|
|
# and invalid values cause an ArgumentError to be raised.
|
2004-01-18 17:01:12 -05:00
|
|
|
# can be negative
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# +y+ defaults to -4712, +m+ to 1, and +d+ to 1; this is
|
|
|
|
# Julian Day Number day 0.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.civil(y=-4712, m=1, d=1, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
unless jd = _valid_civil?(y, m, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1999-01-19 23:59:39 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
class << self; alias_method :new, :civil end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-18 19:19:47 -04:00
|
|
|
# Create a new Date object for the Commercial Date specified by
|
2003-08-23 08:00:21 -04:00
|
|
|
# year +y+, week-of-year +w+, and day-of-week +d+.
|
|
|
|
#
|
|
|
|
# Monday is day-of-week 1; Sunday is day-of-week 7.
|
|
|
|
#
|
|
|
|
# +w+ and +d+ can be negative, in which case they count
|
|
|
|
# backwards from the end of the year and the end of the
|
|
|
|
# week respectively. No wraparound is performed, however,
|
|
|
|
# and invalid values cause an ArgumentError to be raised.
|
|
|
|
#
|
2006-12-30 10:27:01 -05:00
|
|
|
# +y+ defaults to -4712, +w+ to 1, and +d+ to 1; this is
|
|
|
|
# Julian Day Number day 0.
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.commercial(y=-4712, w=1, d=1, sg=ITALY)
|
|
|
|
unless jd = _valid_commercial?(y, w, d, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.weeknum(y=-4712, w=0, d=1, f=0, sg=ITALY)
|
|
|
|
unless jd = _valid_weeknum?(y, w, d, f, sg)
|
|
|
|
raise ArgumentError, 'invalid date'
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :weeknum
|
|
|
|
|
|
|
|
def self.nth_kday(y=-4712, m=1, n=1, k=1, sg=ITALY)
|
|
|
|
unless jd = _valid_nth_kday?(y, m, n, k, sg)
|
|
|
|
raise ArgumentError, 'invalid date'
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :nth_kday
|
2006-08-25 19:09:30 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.rewrite_frags(elem) # :nodoc:
|
2002-05-18 08:41:51 -04:00
|
|
|
elem ||= {}
|
2006-08-25 19:09:30 -04:00
|
|
|
if seconds = elem[:seconds]
|
2006-09-22 12:05:19 -04:00
|
|
|
d, fr = seconds.divmod(86400)
|
|
|
|
h, fr = fr.divmod(3600)
|
|
|
|
min, fr = fr.divmod(60)
|
|
|
|
s, fr = fr.divmod(1)
|
2008-01-05 19:38:22 -05:00
|
|
|
elem[:jd] = UNIX_EPOCH_IN_CJD + d
|
2006-08-25 19:09:30 -04:00
|
|
|
elem[:hour] = h
|
|
|
|
elem[:min] = min
|
|
|
|
elem[:sec] = s
|
2006-09-23 23:11:56 -04:00
|
|
|
elem[:sec_fraction] = fr
|
2006-08-25 19:09:30 -04:00
|
|
|
elem.delete(:seconds)
|
|
|
|
elem.delete(:offset)
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2006-08-25 19:09:30 -04:00
|
|
|
elem
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :rewrite_frags
|
|
|
|
|
|
|
|
def self.complete_frags(elem) # :nodoc:
|
2006-09-23 23:11:56 -04:00
|
|
|
i = 0
|
2006-09-30 02:13:17 -04:00
|
|
|
g = [[:time, [:hour, :min, :sec]],
|
|
|
|
[nil, [:jd]],
|
|
|
|
[:ordinal, [:year, :yday, :hour, :min, :sec]],
|
|
|
|
[:civil, [:year, :mon, :mday, :hour, :min, :sec]],
|
|
|
|
[:commercial, [:cwyear, :cweek, :cwday, :hour, :min, :sec]],
|
|
|
|
[:wday, [:wday, :hour, :min, :sec]],
|
|
|
|
[:wnum0, [:year, :wnum0, :wday, :hour, :min, :sec]],
|
|
|
|
[:wnum1, [:year, :wnum1, :wday, :hour, :min, :sec]],
|
|
|
|
[nil, [:cwyear, :cweek, :wday, :hour, :min, :sec]],
|
|
|
|
[nil, [:year, :wnum0, :cwday, :hour, :min, :sec]],
|
|
|
|
[nil, [:year, :wnum1, :cwday, :hour, :min, :sec]]].
|
2006-09-23 23:11:56 -04:00
|
|
|
collect{|k, a| e = elem.values_at(*a).compact; [k, a, e]}.
|
|
|
|
select{|k, a, e| e.size > 0}.
|
|
|
|
sort_by{|k, a, e| [e.size, i -= 1]}.last
|
|
|
|
|
2006-09-30 02:13:17 -04:00
|
|
|
d = nil
|
|
|
|
|
2006-09-23 23:11:56 -04:00
|
|
|
if g && g[0] && (g[1].size - g[2].size) != 0
|
2006-09-30 02:13:17 -04:00
|
|
|
d ||= Date.today
|
2006-09-23 23:11:56 -04:00
|
|
|
|
|
|
|
case g[0]
|
|
|
|
when :ordinal
|
|
|
|
elem[:year] ||= d.year
|
|
|
|
elem[:yday] ||= 1
|
|
|
|
when :civil
|
|
|
|
g[1].each do |e|
|
|
|
|
break if elem[e]
|
2007-11-04 15:36:20 -05:00
|
|
|
elem[e] = d.__send__(e)
|
2006-09-23 23:11:56 -04:00
|
|
|
end
|
|
|
|
elem[:mon] ||= 1
|
|
|
|
elem[:mday] ||= 1
|
|
|
|
when :commercial
|
|
|
|
g[1].each do |e|
|
|
|
|
break if elem[e]
|
2007-11-04 15:36:20 -05:00
|
|
|
elem[e] = d.__send__(e)
|
2006-09-23 23:11:56 -04:00
|
|
|
end
|
|
|
|
elem[:cweek] ||= 1
|
|
|
|
elem[:cwday] ||= 1
|
2006-09-30 02:13:17 -04:00
|
|
|
when :wday
|
|
|
|
elem[:jd] ||= (d - d.wday + elem[:wday]).jd
|
2006-09-23 23:11:56 -04:00
|
|
|
when :wnum0
|
|
|
|
g[1].each do |e|
|
|
|
|
break if elem[e]
|
2007-11-04 15:36:20 -05:00
|
|
|
elem[e] = d.__send__(e)
|
2006-09-23 23:11:56 -04:00
|
|
|
end
|
|
|
|
elem[:wnum0] ||= 0
|
|
|
|
elem[:wday] ||= 0
|
|
|
|
when :wnum1
|
|
|
|
g[1].each do |e|
|
|
|
|
break if elem[e]
|
2007-11-04 15:36:20 -05:00
|
|
|
elem[e] = d.__send__(e)
|
2006-09-23 23:11:56 -04:00
|
|
|
end
|
|
|
|
elem[:wnum1] ||= 0
|
2009-08-11 10:14:41 -04:00
|
|
|
elem[:wday] ||= 1
|
2006-09-30 02:13:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-10-12 12:51:14 -04:00
|
|
|
if g && g[0] == :time
|
2006-09-30 02:13:17 -04:00
|
|
|
if self <= DateTime
|
|
|
|
d ||= Date.today
|
2006-09-23 23:11:56 -04:00
|
|
|
elem[:jd] ||= d.jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
elem[:hour] ||= 0
|
|
|
|
elem[:min] ||= 0
|
|
|
|
elem[:sec] ||= 0
|
|
|
|
elem[:sec] = [elem[:sec], 59].min
|
|
|
|
|
|
|
|
elem
|
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :complete_frags
|
|
|
|
|
|
|
|
def self.valid_date_frags?(elem, sg) # :nodoc:
|
2006-08-25 19:09:30 -04:00
|
|
|
catch :jd do
|
|
|
|
a = elem.values_at(:jd)
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_jd?(*(a << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = elem.values_at(:year, :yday)
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_ordinal?(*(a << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = elem.values_at(:year, :mon, :mday)
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_civil?(*(a << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = elem.values_at(:cwyear, :cweek, :cwday)
|
|
|
|
if a[2].nil? && elem[:wday]
|
|
|
|
a[2] = elem[:wday].nonzero? || 7
|
|
|
|
end
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_commercial?(*(a << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = elem.values_at(:year, :wnum0, :wday)
|
|
|
|
if a[2].nil? && elem[:cwday]
|
|
|
|
a[2] = elem[:cwday] % 7
|
|
|
|
end
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_weeknum?(*(a << 0 << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
a = elem.values_at(:year, :wnum1, :wday)
|
|
|
|
if a[2]
|
|
|
|
a[2] = (a[2] - 1) % 7
|
|
|
|
end
|
|
|
|
if a[2].nil? && elem[:cwday]
|
|
|
|
a[2] = (elem[:cwday] - 1) % 7
|
|
|
|
end
|
|
|
|
if a.all?
|
2006-12-30 10:27:01 -05:00
|
|
|
if jd = _valid_weeknum?(*(a << 1 << sg))
|
2006-08-25 19:09:30 -04:00
|
|
|
throw :jd, jd
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :valid_date_frags?
|
|
|
|
|
|
|
|
def self.valid_time_frags? (elem) # :nodoc:
|
|
|
|
h, min, s = elem.values_at(:hour, :min, :sec)
|
|
|
|
_valid_time?(h, min, s)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :valid_time_frags?
|
|
|
|
|
|
|
|
def self.new_by_frags(elem, sg) # :nodoc:
|
|
|
|
elem = rewrite_frags(elem)
|
|
|
|
elem = complete_frags(elem)
|
|
|
|
unless jd = valid_date_frags?(elem, sg)
|
2006-08-25 19:09:30 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :new_by_frags
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new Date object by parsing from a String
|
|
|
|
# according to a specified format.
|
|
|
|
#
|
|
|
|
# +str+ is a String holding a date representation.
|
2004-01-18 17:01:12 -05:00
|
|
|
# +fmt+ is the format that the date is in. See
|
2003-08-23 08:00:21 -04:00
|
|
|
# date/format.rb for details on supported formats.
|
|
|
|
#
|
|
|
|
# The default +str+ is '-4712-01-01', and the default
|
|
|
|
# +fmt+ is '%F', which means Year-Month-Day_of_Month.
|
|
|
|
# This gives Julian Day Number day 0.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# An ArgumentError will be raised if +str+ cannot be
|
|
|
|
# parsed.
|
2002-05-18 08:41:51 -04:00
|
|
|
def self.strptime(str='-4712-01-01', fmt='%F', sg=ITALY)
|
|
|
|
elem = _strptime(str, fmt)
|
2006-12-30 10:27:01 -05:00
|
|
|
new_by_frags(elem, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new Date object by parsing from a String,
|
|
|
|
# without specifying the format.
|
|
|
|
#
|
2006-12-30 10:27:01 -05:00
|
|
|
# +str+ is a String holding a date representation.
|
2003-08-23 08:00:21 -04:00
|
|
|
# +comp+ specifies whether to interpret 2-digit years
|
|
|
|
# as 19XX (>= 69) or 20XX (< 69); the default is not to.
|
|
|
|
# The method will attempt to parse a date from the String
|
|
|
|
# using various heuristics; see #_parse in date/format.rb
|
|
|
|
# for more details. If parsing fails, an ArgumentError
|
|
|
|
# will be raised.
|
|
|
|
#
|
|
|
|
# The default +str+ is '-4712-01-01'; this is Julian
|
|
|
|
# Day Number day 0.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2008-01-19 03:23:33 -05:00
|
|
|
def self.parse(str='-4712-01-01', comp=true, sg=ITALY)
|
|
|
|
elem = _parse(str, comp)
|
* lib/date.rb, lib/date/format.rb (Date._parse): now can take some
hints (its aim must be mainly determination of endianness of
date). [experimental]
* lib/date.rb, lib/date/format.rb (Date._parse): now completes
truncated year as default action. [experimental]
* lib/date.rb, lib/date/format.rb: added ::iso8601, ::rfc3339,
::xmlschema, ::rfc2822, ::httpdate, ::jisx0301, #xmlschema,
#httpdate. [experimental]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12833 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-07-21 09:02:29 -04:00
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.iso8601(str='-4712-01-01', sg=ITALY) # :nodoc:
|
|
|
|
elem = _iso8601(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
|
|
|
|
elem = _rfc3339(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.xmlschema(str='-4712-01-01', sg=ITALY) # :nodoc:
|
|
|
|
elem = _xmlschema(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc:
|
|
|
|
elem = _rfc2822(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self; alias_method :rfc822, :rfc2822 end
|
|
|
|
|
|
|
|
def self.httpdate(str='Mon, 01 Jan -4712 00:00:00 GMT', sg=ITALY) # :nodoc:
|
|
|
|
elem = _httpdate(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.jisx0301(str='-4712-01-01', sg=ITALY) # :nodoc:
|
|
|
|
elem = _jisx0301(str)
|
2006-12-30 10:27:01 -05:00
|
|
|
new_by_frags(elem, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2008-01-19 04:42:44 -05:00
|
|
|
def once(*ids) # :nodoc: -- restricted
|
2000-07-18 03:37:02 -04:00
|
|
|
for id in ids
|
|
|
|
module_eval <<-"end;"
|
2008-05-12 09:24:38 -04:00
|
|
|
alias_method :__#{id.object_id}__, :#{id.to_s}
|
|
|
|
private :__#{id.object_id}__
|
2008-01-19 04:42:44 -05:00
|
|
|
def #{id.to_s}(*args)
|
2008-05-12 09:24:38 -04:00
|
|
|
@__ca__[#{id.object_id}] ||= __#{id.object_id}__(*args)
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
|
|
|
end;
|
2000-07-06 03:21:26 -04:00
|
|
|
end
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
private :once
|
|
|
|
|
2000-07-06 03:21:26 -04:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
# *NOTE* this is the documentation for the method new!(). If
|
2003-08-23 08:00:21 -04:00
|
|
|
# you are reading this as the documentation for new(), that is
|
2004-01-18 17:01:12 -05:00
|
|
|
# because rdoc doesn't fully support the aliasing of the
|
2003-08-23 08:00:21 -04:00
|
|
|
# initialize() method.
|
|
|
|
# new() is in
|
|
|
|
# fact an alias for #civil(): read the documentation for that
|
|
|
|
# method instead.
|
|
|
|
#
|
|
|
|
# Create a new Date object.
|
|
|
|
#
|
2004-01-18 17:01:12 -05:00
|
|
|
# +ajd+ is the Astronomical Julian Day Number.
|
2003-08-23 08:00:21 -04:00
|
|
|
# +of+ is the offset from UTC as a fraction of a day.
|
2004-01-18 17:01:12 -05:00
|
|
|
# Both default to 0.
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform to use for this
|
|
|
|
# Date object.
|
|
|
|
#
|
|
|
|
# Using one of the factory methods such as Date::civil is
|
|
|
|
# generally easier and safer.
|
2008-01-19 04:42:44 -05:00
|
|
|
def initialize(ajd=0, of=0, sg=ITALY)
|
|
|
|
@ajd, @of, @sg = ajd, of, sg
|
|
|
|
@__ca__ = {}
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the date as an Astronomical Julian Day Number.
|
2002-06-22 11:26:58 -04:00
|
|
|
def ajd() @ajd end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the date as an Astronomical Modified Julian Day Number.
|
2006-12-30 10:27:01 -05:00
|
|
|
def amjd() ajd_to_amjd(@ajd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
once :amjd
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the date as a Julian Day Number.
|
2006-12-30 10:27:01 -05:00
|
|
|
def jd() ajd_to_jd(@ajd, @of)[0] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get any fractional day part of the date.
|
2006-12-30 10:27:01 -05:00
|
|
|
def day_fraction() ajd_to_jd(@ajd, @of)[1] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the date as a Modified Julian Day Number.
|
2006-12-30 10:27:01 -05:00
|
|
|
def mjd() jd_to_mjd(jd) end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the date as the number of days since the Day of Calendar
|
|
|
|
# Reform (in Italy and the Catholic countries).
|
2006-12-30 10:27:01 -05:00
|
|
|
def ld() jd_to_ld(jd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
once :jd, :day_fraction, :mjd, :ld
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the date as a Civil Date, [year, month, day_of_month]
|
2006-12-30 10:27:01 -05:00
|
|
|
def civil() jd_to_civil(jd, @sg) end # :nodoc:
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the date as an Ordinal Date, [year, day_of_year]
|
2006-12-30 10:27:01 -05:00
|
|
|
def ordinal() jd_to_ordinal(jd, @sg) end # :nodoc:
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the date as a Commercial Date, [year, week_of_year, day_of_week]
|
2006-12-30 10:27:01 -05:00
|
|
|
def commercial() jd_to_commercial(jd, @sg) end # :nodoc:
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def weeknum0() jd_to_weeknum(jd, 0, @sg) end # :nodoc:
|
|
|
|
def weeknum1() jd_to_weeknum(jd, 1, @sg) end # :nodoc:
|
2006-09-23 23:11:56 -04:00
|
|
|
|
|
|
|
once :civil, :ordinal, :commercial, :weeknum0, :weeknum1
|
|
|
|
private :civil, :ordinal, :commercial, :weeknum0, :weeknum1
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the year of this date.
|
2000-07-18 03:37:02 -04:00
|
|
|
def year() civil[0] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the day-of-the-year of this date.
|
|
|
|
#
|
|
|
|
# January 1 is day-of-the-year 1
|
2000-07-18 03:37:02 -04:00
|
|
|
def yday() ordinal[1] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the month of this date.
|
|
|
|
#
|
|
|
|
# January is month 1.
|
2000-07-18 03:37:02 -04:00
|
|
|
def mon() civil[1] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the day-of-the-month of this date.
|
2002-05-18 08:41:51 -04:00
|
|
|
def mday() civil[2] end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
alias_method :month, :mon
|
2002-05-18 08:41:51 -04:00
|
|
|
alias_method :day, :mday
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-09-23 23:11:56 -04:00
|
|
|
def wnum0() weeknum0[1] end # :nodoc:
|
|
|
|
def wnum1() weeknum1[1] end # :nodoc:
|
|
|
|
|
|
|
|
private :wnum0, :wnum1
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the time of this date as [hours, minutes, seconds,
|
|
|
|
# fraction_of_a_second]
|
2006-12-30 10:27:01 -05:00
|
|
|
def time() day_fraction_to_time(day_fraction) end # :nodoc:
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-05-18 08:41:51 -04:00
|
|
|
once :time
|
|
|
|
private :time
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the hour of this date.
|
2002-05-18 08:41:51 -04:00
|
|
|
def hour() time[0] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the minute of this date.
|
2002-05-18 08:41:51 -04:00
|
|
|
def min() time[1] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the second of this date.
|
2002-05-18 08:41:51 -04:00
|
|
|
def sec() time[2] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the fraction-of-a-second of this date.
|
2002-05-18 08:41:51 -04:00
|
|
|
def sec_fraction() time[3] end
|
|
|
|
|
2008-01-05 19:38:22 -05:00
|
|
|
alias_method :minute, :min
|
|
|
|
alias_method :second, :sec
|
|
|
|
alias_method :second_fraction, :sec_fraction
|
|
|
|
|
|
|
|
private :hour, :min, :sec, :sec_fraction,
|
|
|
|
:minute, :second, :second_fraction
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2006-09-04 08:43:20 -04:00
|
|
|
def zone() strftime('%:z') end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
|
|
|
private :zone
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the commercial year of this date. See *Commercial* *Date*
|
|
|
|
# in the introduction for how this differs from the normal year.
|
2000-07-18 03:37:02 -04:00
|
|
|
def cwyear() commercial[0] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the commercial week of the year of this date.
|
2000-07-18 03:37:02 -04:00
|
|
|
def cweek() commercial[1] end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Get the commercial day of the week of this date. Monday is
|
|
|
|
# commercial day-of-week 1; Sunday is commercial day-of-week 7.
|
2000-07-18 03:37:02 -04:00
|
|
|
def cwday() commercial[2] end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Get the week day of this date. Sunday is day-of-week 0;
|
|
|
|
# Saturday is day-of-week 6.
|
2006-12-30 10:27:01 -05:00
|
|
|
def wday() jd_to_wday(jd) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :wday
|
|
|
|
|
2006-09-30 02:13:17 -04:00
|
|
|
=begin
|
|
|
|
MONTHNAMES.each_with_index do |n, i|
|
|
|
|
if n
|
|
|
|
define_method(n.downcase + '?'){mon == i}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
=end
|
|
|
|
|
2005-10-29 04:03:46 -04:00
|
|
|
DAYNAMES.each_with_index do |n, i|
|
|
|
|
define_method(n.downcase + '?'){wday == i}
|
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def nth_kday? (n, k)
|
|
|
|
k == wday && jd === nth_kday_to_jd(year, mon, n, k, start)
|
|
|
|
end
|
|
|
|
|
|
|
|
private :nth_kday?
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Is the current date old-style (Julian Calendar)?
|
2006-12-30 10:27:01 -05:00
|
|
|
def julian? () jd < @sg end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Is the current date new-style (Gregorian Calendar)?
|
2006-12-30 10:27:01 -05:00
|
|
|
def gregorian? () !julian? end
|
2006-08-25 19:09:30 -04:00
|
|
|
|
|
|
|
once :julian?, :gregorian?
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
def fix_style # :nodoc:
|
|
|
|
if julian?
|
2006-09-22 12:05:19 -04:00
|
|
|
then self.class::JULIAN
|
|
|
|
else self.class::GREGORIAN end
|
2006-08-25 19:09:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private :fix_style
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Is this a leap year?
|
2000-07-18 03:37:02 -04:00
|
|
|
def leap?
|
2006-12-30 10:27:01 -05:00
|
|
|
jd_to_civil(civil_to_jd(year, 3, 1, fix_style) - 1,
|
|
|
|
fix_style)[-1] == 29
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
|
|
|
once :leap?
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# When is the Day of Calendar Reform for this Date object?
|
2002-06-22 11:26:58 -04:00
|
|
|
def start() @sg end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Create a copy of this Date object using a new Day of Calendar Reform.
|
2006-12-30 10:27:01 -05:00
|
|
|
def new_start(sg=self.class::ITALY) self.class.new!(@ajd, @of, sg) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a copy of this Date object that uses the Italian/Catholic
|
|
|
|
# Day of Calendar Reform.
|
2002-10-02 12:45:35 -04:00
|
|
|
def italy() new_start(self.class::ITALY) end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Create a copy of this Date object that uses the English/Colonial
|
|
|
|
# Day of Calendar Reform.
|
2002-10-02 12:45:35 -04:00
|
|
|
def england() new_start(self.class::ENGLAND) end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Create a copy of this Date object that always uses the Julian
|
|
|
|
# Calendar.
|
2002-10-02 12:45:35 -04:00
|
|
|
def julian() new_start(self.class::JULIAN) end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Create a copy of this Date object that always uses the Gregorian
|
|
|
|
# Calendar.
|
2002-10-02 12:45:35 -04:00
|
|
|
def gregorian() new_start(self.class::GREGORIAN) end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
def offset() @of end
|
2006-11-05 05:55:20 -05:00
|
|
|
|
|
|
|
def new_offset(of=0)
|
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
self.class.new!(@ajd, of, @sg)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
private :offset, :new_offset
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Return a new Date object that is +n+ days later than the
|
|
|
|
# current one.
|
|
|
|
#
|
|
|
|
# +n+ may be a negative value, in which case the new Date
|
|
|
|
# is earlier than the current one; however, #-() might be
|
|
|
|
# more intuitive.
|
|
|
|
#
|
|
|
|
# If +n+ is not a Numeric, a TypeError will be thrown. In
|
|
|
|
# particular, two Dates cannot be added to each other.
|
2000-07-18 03:37:02 -04:00
|
|
|
def + (n)
|
|
|
|
case n
|
2006-12-30 10:27:01 -05:00
|
|
|
when Numeric; return self.class.new!(@ajd + n, @of, @sg)
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
|
|
|
raise TypeError, 'expected numeric'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# If +x+ is a Numeric value, create a new Date object that is
|
|
|
|
# +x+ days earlier than the current one.
|
|
|
|
#
|
|
|
|
# If +x+ is a Date, return the number of days between the
|
|
|
|
# two dates; or, more precisely, how many days later the current
|
|
|
|
# date is than +x+.
|
|
|
|
#
|
|
|
|
# If +x+ is neither Numeric nor a Date, a TypeError is raised.
|
2000-07-18 03:37:02 -04:00
|
|
|
def - (x)
|
|
|
|
case x
|
2006-12-30 10:27:01 -05:00
|
|
|
when Numeric; return self.class.new!(@ajd - x, @of, @sg)
|
2002-06-22 11:26:58 -04:00
|
|
|
when Date; return @ajd - x.ajd
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2009-10-18 09:38:55 -04:00
|
|
|
raise TypeError, 'expected numeric or date'
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Compare this date with another date.
|
|
|
|
#
|
2004-01-18 17:01:12 -05:00
|
|
|
# +other+ can also be a Numeric value, in which case it is
|
2003-08-23 08:00:21 -04:00
|
|
|
# interpreted as an Astronomical Julian Day Number.
|
|
|
|
#
|
|
|
|
# Comparison is by Astronomical Julian Day Number, including
|
|
|
|
# fractional days. This means that both the time and the
|
|
|
|
# timezone offset are taken into account when comparing
|
|
|
|
# two DateTime instances. When comparing a DateTime instance
|
|
|
|
# with a Date instance, the time of the latter will be
|
|
|
|
# considered as falling on midnight UTC.
|
2000-07-18 03:37:02 -04:00
|
|
|
def <=> (other)
|
|
|
|
case other
|
2002-06-22 11:26:58 -04:00
|
|
|
when Numeric; return @ajd <=> other
|
|
|
|
when Date; return @ajd <=> other.ajd
|
2010-04-26 07:14:40 -04:00
|
|
|
else
|
|
|
|
begin
|
|
|
|
l, r = other.coerce(self)
|
|
|
|
return l <=> r
|
|
|
|
rescue NoMethodError
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2003-03-03 00:17:39 -05:00
|
|
|
nil
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# The relationship operator for Date.
|
|
|
|
#
|
|
|
|
# Compares dates by Julian Day Number. When comparing
|
|
|
|
# two DateTime instances, or a DateTime with a Date,
|
|
|
|
# the instances will be regarded as equivalent if they
|
|
|
|
# fall on the same date in local time.
|
2000-07-18 03:37:02 -04:00
|
|
|
def === (other)
|
|
|
|
case other
|
|
|
|
when Numeric; return jd == other
|
|
|
|
when Date; return jd == other.jd
|
2010-04-26 07:14:40 -04:00
|
|
|
else
|
|
|
|
l, r = other.coerce(self)
|
|
|
|
return l === r
|
2000-07-18 03:37:02 -04:00
|
|
|
end
|
2003-03-03 00:17:39 -05:00
|
|
|
false
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
def next_day(n=1) self + n end
|
|
|
|
def prev_day(n=1) self - n end
|
|
|
|
|
|
|
|
# Return a new Date one day after this one.
|
|
|
|
def next() next_day end
|
|
|
|
|
|
|
|
alias_method :succ, :next
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Return a new Date object that is +n+ months later than
|
|
|
|
# the current one.
|
|
|
|
#
|
|
|
|
# If the day-of-the-month of the current Date is greater
|
|
|
|
# than the last day of the target month, the day-of-the-month
|
|
|
|
# of the returned Date will be the last day of the target month.
|
2000-07-18 03:37:02 -04:00
|
|
|
def >> (n)
|
2006-09-22 12:05:19 -04:00
|
|
|
y, m = (year * 12 + (mon - 1) + n).divmod(12)
|
2006-12-30 10:27:01 -05:00
|
|
|
m, = (m + 1) .divmod(1)
|
2000-07-18 03:37:02 -04:00
|
|
|
d = mday
|
2010-03-22 06:32:10 -04:00
|
|
|
until jd2 = _valid_civil?(y, m, d, @sg)
|
|
|
|
d -= 1
|
|
|
|
raise ArgumentError, 'invalid date' unless d > 0
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
self + (jd2 - jd)
|
|
|
|
end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Return a new Date object that is +n+ months earlier than
|
|
|
|
# the current one.
|
|
|
|
#
|
|
|
|
# If the day-of-the-month of the current Date is greater
|
|
|
|
# than the last day of the target month, the day-of-the-month
|
|
|
|
# of the returned Date will be the last day of the target month.
|
2000-07-18 03:37:02 -04:00
|
|
|
def << (n) self >> -n end
|
2000-07-06 03:21:26 -04:00
|
|
|
|
2005-10-29 04:03:46 -04:00
|
|
|
def next_month(n=1) self >> n end
|
|
|
|
def prev_month(n=1) self << n end
|
|
|
|
|
2006-08-25 19:09:30 -04:00
|
|
|
def next_year(n=1) self >> n * 12 end
|
|
|
|
def prev_year(n=1) self << n * 12 end
|
|
|
|
|
2006-11-05 05:55:20 -05:00
|
|
|
require 'enumerator'
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Step the current date forward +step+ days at a
|
2004-01-18 17:01:12 -05:00
|
|
|
# time (or backward, if +step+ is negative) until
|
2003-08-23 08:00:21 -04:00
|
|
|
# we reach +limit+ (inclusive), yielding the resultant
|
|
|
|
# date at each step.
|
2006-09-23 23:11:56 -04:00
|
|
|
def step(limit, step=1) # :yield: date
|
2007-09-07 20:38:03 -04:00
|
|
|
=begin
|
|
|
|
if step.zero?
|
|
|
|
raise ArgumentError, "step can't be 0"
|
|
|
|
end
|
|
|
|
=end
|
2006-11-05 05:55:20 -05:00
|
|
|
unless block_given?
|
|
|
|
return to_enum(:step, limit, step)
|
|
|
|
end
|
2000-07-18 03:37:02 -04:00
|
|
|
da = self
|
2006-09-10 07:27:56 -04:00
|
|
|
op = %w(- <= >=)[step <=> 0]
|
2000-07-18 03:37:02 -04:00
|
|
|
while da.__send__(op, limit)
|
|
|
|
yield da
|
|
|
|
da += step
|
|
|
|
end
|
|
|
|
self
|
1998-01-16 07:13:05 -05:00
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Step forward one day at a time until we reach +max+
|
|
|
|
# (inclusive), yielding each date as we go.
|
2006-09-23 23:11:56 -04:00
|
|
|
def upto(max, &block) # :yield: date
|
2006-08-25 19:09:30 -04:00
|
|
|
step(max, +1, &block)
|
2004-01-18 17:01:12 -05:00
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Step backward one day at a time until we reach +min+
|
|
|
|
# (inclusive), yielding each date as we go.
|
|
|
|
def downto(min, &block) # :yield: date
|
2006-08-25 19:09:30 -04:00
|
|
|
step(min, -1, &block)
|
2003-08-23 08:00:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Is this Date equal to +other+?
|
|
|
|
#
|
|
|
|
# +other+ must both be a Date object, and represent the same date.
|
2006-12-30 10:27:01 -05:00
|
|
|
def eql? (other) Date === other && self == other end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Calculate a hash value for this date.
|
2002-06-22 11:26:58 -04:00
|
|
|
def hash() @ajd.hash end
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Return internal object state as a programmer-readable string.
|
2008-11-24 05:55:00 -05:00
|
|
|
def inspect
|
|
|
|
format('#<%s: %s (%s,%s,%s)>', self.class, to_s, @ajd, @of, @sg)
|
|
|
|
end
|
2003-08-23 08:00:21 -04:00
|
|
|
|
|
|
|
# Return the date as a human-readable string.
|
|
|
|
#
|
|
|
|
# The format used is YYYY-MM-DD.
|
2008-01-11 22:03:31 -05:00
|
|
|
def to_s() format('%.4d-%02d-%02d', year, mon, mday) end # 4p
|
2000-07-18 03:37:02 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Dump to Marshal format.
|
2006-12-30 10:27:01 -05:00
|
|
|
def marshal_dump() [@ajd, @of, @sg] end
|
2002-06-22 11:26:58 -04:00
|
|
|
|
2008-01-21 06:12:43 -05:00
|
|
|
# Load from Marshal format.
|
2008-01-21 06:10:53 -05:00
|
|
|
def marshal_load(a)
|
|
|
|
@ajd, @of, @sg, = a
|
|
|
|
@__ca__ = {}
|
|
|
|
end
|
1998-01-16 07:13:05 -05:00
|
|
|
|
|
|
|
end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Class representing a date and time.
|
|
|
|
#
|
|
|
|
# See the documentation to the file date.rb for an overview.
|
|
|
|
#
|
|
|
|
# DateTime objects are immutable once created.
|
|
|
|
#
|
|
|
|
# == Other methods.
|
|
|
|
#
|
|
|
|
# The following methods are defined in Date, but declared private
|
|
|
|
# there. They are made public in DateTime. They are documented
|
|
|
|
# here.
|
|
|
|
#
|
|
|
|
# === hour()
|
|
|
|
#
|
|
|
|
# Get the hour-of-the-day of the time. This is given
|
|
|
|
# using the 24-hour clock, counting from midnight. The first
|
2004-01-18 17:01:12 -05:00
|
|
|
# hour after midnight is hour 0; the last hour of the day is
|
2003-08-23 08:00:21 -04:00
|
|
|
# hour 23.
|
|
|
|
#
|
|
|
|
# === min()
|
|
|
|
#
|
|
|
|
# Get the minute-of-the-hour of the time.
|
|
|
|
#
|
|
|
|
# === sec()
|
|
|
|
#
|
|
|
|
# Get the second-of-the-minute of the time.
|
|
|
|
#
|
|
|
|
# === sec_fraction()
|
|
|
|
#
|
|
|
|
# Get the fraction of a second of the time. This is returned as
|
|
|
|
# a +Rational+.
|
|
|
|
#
|
|
|
|
# === zone()
|
|
|
|
#
|
|
|
|
# Get the time zone as a String. This is representation of the
|
|
|
|
# time offset such as "+1000", not the true time-zone name.
|
|
|
|
#
|
|
|
|
# === offset()
|
|
|
|
#
|
|
|
|
# Get the time zone offset as a fraction of a day. This is returned
|
|
|
|
# as a +Rational+.
|
|
|
|
#
|
|
|
|
# === new_offset(of=0)
|
|
|
|
#
|
|
|
|
# Create a new DateTime object, identical to the current one, except
|
|
|
|
# with a new time zone offset of +of+. +of+ is the new offset from
|
|
|
|
# UTC as a fraction of a day.
|
|
|
|
#
|
2002-05-18 08:41:51 -04:00
|
|
|
class DateTime < Date
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object corresponding to the specified
|
|
|
|
# Julian Day Number +jd+ and hour +h+, minute +min+, second +s+.
|
|
|
|
#
|
|
|
|
# The 24-hour clock is used. Negative values of +h+, +min+, and
|
|
|
|
# +sec+ are treating as counting backwards from the end of the
|
|
|
|
# next larger unit (e.g. a +min+ of -2 is treated as 58). No
|
|
|
|
# wraparound is performed. If an invalid time portion is specified,
|
|
|
|
# an ArgumentError is raised.
|
|
|
|
#
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# All day/time values default to 0.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.jd(jd=0, h=0, min=0, s=0, of=0, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
unless (jd = _valid_jd?(jd, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object corresponding to the specified
|
|
|
|
# Ordinal Date and hour +h+, minute +min+, second +s+.
|
|
|
|
#
|
|
|
|
# The 24-hour clock is used. Negative values of +h+, +min+, and
|
|
|
|
# +sec+ are treating as counting backwards from the end of the
|
|
|
|
# next larger unit (e.g. a +min+ of -2 is treated as 58). No
|
|
|
|
# wraparound is performed. If an invalid time portion is specified,
|
|
|
|
# an ArgumentError is raised.
|
|
|
|
#
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# +y+ defaults to -4712, and +d+ to 1; this is Julian Day Number
|
|
|
|
# day 0. The time values default to 0.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.ordinal(y=-4712, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
unless (jd = _valid_ordinal?(y, d, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object corresponding to the specified
|
|
|
|
# Civil Date and hour +h+, minute +min+, second +s+.
|
|
|
|
#
|
|
|
|
# The 24-hour clock is used. Negative values of +h+, +min+, and
|
|
|
|
# +sec+ are treating as counting backwards from the end of the
|
|
|
|
# next larger unit (e.g. a +min+ of -2 is treated as 58). No
|
|
|
|
# wraparound is performed. If an invalid time portion is specified,
|
|
|
|
# an ArgumentError is raised.
|
|
|
|
#
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
2004-01-18 17:01:12 -05:00
|
|
|
# +y+ defaults to -4712, +m+ to 1, and +d+ to 1; this is Julian Day
|
2003-08-23 08:00:21 -04:00
|
|
|
# Number day 0. The time values default to 0.
|
2002-06-22 11:26:58 -04:00
|
|
|
def self.civil(y=-4712, m=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
|
2006-12-30 10:27:01 -05:00
|
|
|
unless (jd = _valid_civil?(y, m, d, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2002-06-22 11:26:58 -04:00
|
|
|
class << self; alias_method :new, :civil end
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object corresponding to the specified
|
|
|
|
# Commercial Date and hour +h+, minute +min+, second +s+.
|
|
|
|
#
|
|
|
|
# The 24-hour clock is used. Negative values of +h+, +min+, and
|
|
|
|
# +sec+ are treating as counting backwards from the end of the
|
|
|
|
# next larger unit (e.g. a +min+ of -2 is treated as 58). No
|
|
|
|
# wraparound is performed. If an invalid time portion is specified,
|
|
|
|
# an ArgumentError is raised.
|
|
|
|
#
|
|
|
|
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
2006-12-30 10:27:01 -05:00
|
|
|
# +y+ defaults to -4712, +w+ to 1, and +d+ to 1; this is
|
|
|
|
# Julian Day Number day 0.
|
2003-08-23 08:00:21 -04:00
|
|
|
# The time values default to 0.
|
2006-12-30 10:27:01 -05:00
|
|
|
def self.commercial(y=-4712, w=1, d=1, h=0, min=0, s=0, of=0, sg=ITALY)
|
|
|
|
unless (jd = _valid_commercial?(y, w, d, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-12-30 10:27:01 -05:00
|
|
|
end
|
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.weeknum(y=-4712, w=0, d=1, f=0, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
|
|
|
|
unless (jd = _valid_weeknum?(y, w, d, f, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-12-30 10:27:01 -05:00
|
|
|
end
|
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
private_class_method :weeknum
|
|
|
|
|
|
|
|
def self.nth_kday(y=-4712, m=1, n=1, k=1, h=0, min=0, s=0, of=0, sg=ITALY) # :nodoc:
|
|
|
|
unless (jd = _valid_nth_kday?(y, m, n, k, sg)) &&
|
|
|
|
(fr = _valid_time?(h, min, s))
|
2002-05-18 08:41:51 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
if String === of
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(zone_to_diff(of) || 0, 86400)
|
2006-11-05 05:55:20 -05:00
|
|
|
end
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :nth_kday
|
|
|
|
|
|
|
|
def self.new_by_frags(elem, sg) # :nodoc:
|
|
|
|
elem = rewrite_frags(elem)
|
|
|
|
elem = complete_frags(elem)
|
|
|
|
unless (jd = valid_date_frags?(elem, sg)) &&
|
|
|
|
(fr = valid_time_frags?(elem))
|
2006-08-25 19:09:30 -04:00
|
|
|
raise ArgumentError, 'invalid date'
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
2008-01-05 19:38:22 -05:00
|
|
|
fr += (elem[:sec_fraction] || 0) / 86400
|
|
|
|
of = Rational(elem[:offset] || 0, 86400)
|
2006-12-30 10:27:01 -05:00
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
private_class_method :new_by_frags
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object by parsing from a String
|
|
|
|
# according to a specified format.
|
|
|
|
#
|
|
|
|
# +str+ is a String holding a date-time representation.
|
2004-01-18 17:01:12 -05:00
|
|
|
# +fmt+ is the format that the date-time is in. See
|
2003-08-23 08:00:21 -04:00
|
|
|
# date/format.rb for details on supported formats.
|
|
|
|
#
|
2006-09-22 12:05:19 -04:00
|
|
|
# The default +str+ is '-4712-01-01T00:00:00+00:00', and the default
|
|
|
|
# +fmt+ is '%FT%T%z'. This gives midnight on Julian Day Number day 0.
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
|
|
|
#
|
|
|
|
# An ArgumentError will be raised if +str+ cannot be
|
|
|
|
# parsed.
|
2006-09-22 12:05:19 -04:00
|
|
|
def self.strptime(str='-4712-01-01T00:00:00+00:00', fmt='%FT%T%z', sg=ITALY)
|
2002-05-18 08:41:51 -04:00
|
|
|
elem = _strptime(str, fmt)
|
2006-12-30 10:27:01 -05:00
|
|
|
new_by_frags(elem, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2003-08-23 08:00:21 -04:00
|
|
|
# Create a new DateTime object by parsing from a String,
|
|
|
|
# without specifying the format.
|
|
|
|
#
|
2006-12-30 10:27:01 -05:00
|
|
|
# +str+ is a String holding a date-time representation.
|
2003-08-23 08:00:21 -04:00
|
|
|
# +comp+ specifies whether to interpret 2-digit years
|
|
|
|
# as 19XX (>= 69) or 20XX (< 69); the default is not to.
|
|
|
|
# The method will attempt to parse a date-time from the String
|
|
|
|
# using various heuristics; see #_parse in date/format.rb
|
|
|
|
# for more details. If parsing fails, an ArgumentError
|
|
|
|
# will be raised.
|
|
|
|
#
|
2006-09-22 12:05:19 -04:00
|
|
|
# The default +str+ is '-4712-01-01T00:00:00+00:00'; this is Julian
|
2003-08-23 08:00:21 -04:00
|
|
|
# Day Number day 0.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2008-01-19 03:23:33 -05:00
|
|
|
def self.parse(str='-4712-01-01T00:00:00+00:00', comp=true, sg=ITALY)
|
|
|
|
elem = _parse(str, comp)
|
* lib/date.rb, lib/date/format.rb (Date._parse): now can take some
hints (its aim must be mainly determination of endianness of
date). [experimental]
* lib/date.rb, lib/date/format.rb (Date._parse): now completes
truncated year as default action. [experimental]
* lib/date.rb, lib/date/format.rb: added ::iso8601, ::rfc3339,
::xmlschema, ::rfc2822, ::httpdate, ::jisx0301, #xmlschema,
#httpdate. [experimental]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12833 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-07-21 09:02:29 -04:00
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.iso8601(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
|
|
|
|
elem = _iso8601(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rfc3339(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
|
|
|
|
elem = _rfc3339(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.xmlschema(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
|
|
|
|
elem = _xmlschema(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.rfc2822(str='Mon, 1 Jan -4712 00:00:00 +0000', sg=ITALY) # :nodoc:
|
|
|
|
elem = _rfc2822(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self; alias_method :rfc822, :rfc2822 end
|
|
|
|
|
|
|
|
def self.httpdate(str='Mon, 01 Jan -4712 00:00:00 GMT', sg=ITALY) # :nodoc:
|
|
|
|
elem = _httpdate(str)
|
|
|
|
new_by_frags(elem, sg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.jisx0301(str='-4712-01-01T00:00:00+00:00', sg=ITALY) # :nodoc:
|
|
|
|
elem = _jisx0301(str)
|
2006-12-30 10:27:01 -05:00
|
|
|
new_by_frags(elem, sg)
|
2002-05-18 08:41:51 -04:00
|
|
|
end
|
|
|
|
|
2008-01-05 19:38:22 -05:00
|
|
|
public :hour, :min, :sec, :sec_fraction, :zone, :offset, :new_offset,
|
|
|
|
:minute, :second, :second_fraction
|
2002-05-18 08:41:51 -04:00
|
|
|
|
2008-01-11 22:03:31 -05:00
|
|
|
def to_s # 4p
|
|
|
|
format('%.4d-%02d-%02dT%02d:%02d:%02d%s',
|
|
|
|
year, mon, mday, hour, min, sec, zone)
|
|
|
|
end
|
|
|
|
|
2004-09-25 19:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class Time
|
|
|
|
|
|
|
|
def to_time() getlocal end
|
|
|
|
|
|
|
|
def to_date
|
2007-11-04 15:36:20 -05:00
|
|
|
jd = Date.__send__(:civil_to_jd, year, mon, mday, Date::ITALY)
|
|
|
|
Date.new!(Date.__send__(:jd_to_ajd, jd, 0, 0), 0, Date::ITALY)
|
2004-09-25 19:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_datetime
|
2007-11-04 15:36:20 -05:00
|
|
|
jd = DateTime.__send__(:civil_to_jd, year, mon, mday, DateTime::ITALY)
|
|
|
|
fr = DateTime.__send__(:time_to_day_fraction, hour, min, [sec, 59].min) +
|
2009-05-09 06:28:37 -04:00
|
|
|
Rational(subsec, 86400)
|
2008-01-05 19:38:22 -05:00
|
|
|
of = Rational(utc_offset, 86400)
|
2007-11-04 15:36:20 -05:00
|
|
|
DateTime.new!(DateTime.__send__(:jd_to_ajd, jd, fr, of),
|
2006-12-30 10:27:01 -05:00
|
|
|
of, DateTime::ITALY)
|
2004-09-25 19:10:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Date
|
|
|
|
|
|
|
|
def to_time() Time.local(year, mon, mday) end
|
|
|
|
def to_date() self end
|
2006-12-30 10:27:01 -05:00
|
|
|
def to_datetime() DateTime.new!(jd_to_ajd(jd, 0, 0), @of, @sg) end
|
2004-09-25 19:10:36 -04:00
|
|
|
|
|
|
|
# Create a new Date object representing today.
|
2003-08-23 08:00:21 -04:00
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2008-10-07 11:08:42 -04:00
|
|
|
def self.today(sg=ITALY)
|
|
|
|
t = Time.now
|
|
|
|
jd = civil_to_jd(t.year, t.mon, t.mday, sg)
|
|
|
|
new!(jd_to_ajd(jd, 0, 0), 0, sg)
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
|
|
|
|
# Create a new DateTime object representing the current time.
|
|
|
|
#
|
|
|
|
# +sg+ specifies the Day of Calendar Reform.
|
2008-10-07 11:08:42 -04:00
|
|
|
def self.now(sg=ITALY)
|
|
|
|
t = Time.now
|
|
|
|
jd = civil_to_jd(t.year, t.mon, t.mday, sg)
|
|
|
|
fr = time_to_day_fraction(t.hour, t.min, [t.sec, 59].min) +
|
2009-05-09 06:28:37 -04:00
|
|
|
Rational(t.subsec, 86400)
|
2008-10-07 11:08:42 -04:00
|
|
|
of = Rational(t.utc_offset, 86400)
|
|
|
|
new!(jd_to_ajd(jd, fr, of), of, sg)
|
|
|
|
end
|
2006-11-05 05:55:20 -05:00
|
|
|
|
|
|
|
private_class_method :now
|
2004-09-25 19:10:36 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class DateTime < Date
|
|
|
|
|
|
|
|
def to_time
|
|
|
|
d = new_offset(0)
|
|
|
|
d.instance_eval do
|
2007-12-01 06:13:32 -05:00
|
|
|
Time.utc(year, mon, mday, hour, min, sec +
|
|
|
|
sec_fraction)
|
2004-09-25 19:10:36 -04:00
|
|
|
end.
|
|
|
|
getlocal
|
2002-06-22 11:26:58 -04:00
|
|
|
end
|
|
|
|
|
2006-12-30 10:27:01 -05:00
|
|
|
def to_date() Date.new!(jd_to_ajd(jd, 0, 0), 0, @sg) end
|
2004-09-25 19:10:36 -04:00
|
|
|
def to_datetime() self end
|
|
|
|
|
2006-11-05 05:55:20 -05:00
|
|
|
private_class_method :today
|
|
|
|
public_class_method :now
|
2002-06-22 11:26:58 -04:00
|
|
|
|
|
|
|
end
|