سؤال

What is the extra data included in a date object? Given the following example:

time = Time.at(1392328830)
# => 2014-02-13 15:00:30 -0700
date = time.to_date
# => #<Date: 2014-02-13 ((2456702j,0s,0n),+0s,2299161j)>

What does all this represent? It's not clear from looking at the Ruby Date documentation.

((2456702j,0s,0n),+0s,2299161j)

هل كانت مفيدة؟

المحلول

What you're seeing is the output from Object.inspect which is a human-readable representation of an object. In the case of the Date class:

From date.rb:

# Return internal object state as a programmer-readable string.
def inspect() format('#<%s: %s,%s,%s>', self.class, @ajd, @of, @sg) end

# Return the date as a human-readable string.
#
# The format used is YYYY-MM-DD.
def to_s() strftime end

The instance variables are:

  1. @ajd is an Astronomical Julian Day Number
  2. @of is an offset or fraction of a day from UTC
  3. @sg is the Day of Calendar Reform

But what do these terms mean?

1. What is an Astronomical Julian Day Number? (@ajd)

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*. 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*. In the Date class, the Astronomical Julian Day Number includes fractional days.

2. Offset from what? (@offset)

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. UTC offset 0 is centered on England (also known as GMT). 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.

3. What is the Day of Calendar Reform? (@sg)

The Gregorian Calendar was introduced at different times in different regions. The day on which it was introduced for a particular region is the Day *of* Calendar *Reform* for that region. This is abbreviated as sg (for Start of Gregorian calendar) in the Date class.

From what I can tell, the Gregorian Calendar is calendar that self-corrects via leap years.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top