Flowdock

format.rb: Written by Tadayoshi Funaba 1999-2008 $Id: format.rb,v 2.43 2008-01-17 20:16:31+09 tadf Exp $


Class representing a date.

See the documentation to the file date.rb for an overview.

Internally, the date is represented as an Astronomical Julian Day Number, ajd. The Day of Calendar Reform, sg, is 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 can be retrieved for the different date formats supported 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.

Constants

ENGLAND = 2361222

MONTHNAMES = [nil] + %w(January February March April May June July August September October November December)

DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)

ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)

ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)

ITALY = 2299161

JULIAN = Infinity.new

GREGORIAN = -Infinity.new

HALF_DAYS_IN_DAY = Rational(1, 2)

HOURS_IN_DAY = Rational(1, 24)

MINUTES_IN_DAY = Rational(1, 1440)

SECONDS_IN_DAY = Rational(1, 86400)

MILLISECONDS_IN_DAY = Rational(1, 86400*10**3)

NANOSECONDS_IN_DAY = Rational(1, 86400*10**9)

MILLISECONDS_IN_SECOND = Rational(1, 10**3)

NANOSECONDS_IN_SECOND = Rational(1, 10**9)

MJD_EPOCH_IN_AJD = Rational(4800001, 2)

UNIX_EPOCH_IN_AJD = Rational(4881175, 2)

MJD_EPOCH_IN_CJD = 2400001

UNIX_EPOCH_IN_CJD = 2440588

LD_EPOCH_IN_CJD = 2299160

Attributes

Show files where this class is defined (4 files)
Register or log in to add new notes.
September 7, 2010 - (v1_8_6_287 - v1_8_7_72)
1 thank

To illustrate Date class let's calculate days between dates

Code example

date_from = Date.new(2010, 9, 11)
#<Date: 4910901/2,0,2299161>
date_till = Date.new(2010, 11, 12)
#<Date: 4911025/2,0,2299161>
rational_offset = (date_till - date_from)
#Rational62, 1
rational_offset.to_i
#62