Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: date.rb
#
[0] Fix | Delete
# date.rb - date and time library
[1] Fix | Delete
#
[2] Fix | Delete
# Author: Tadayoshi Funaba 1998-2010
[3] Fix | Delete
#
[4] Fix | Delete
# Documentation: William Webber <william@williamwebber.com>
[5] Fix | Delete
#
[6] Fix | Delete
#--
[7] Fix | Delete
# $Id: date.rb,v 2.37 2008-01-17 20:16:31+09 tadf Exp $
[8] Fix | Delete
#++
[9] Fix | Delete
#
[10] Fix | Delete
# == Overview
[11] Fix | Delete
#
[12] Fix | Delete
# This file provides two classes for working with
[13] Fix | Delete
# dates and times.
[14] Fix | Delete
#
[15] Fix | Delete
# The first class, Date, represents dates.
[16] Fix | Delete
# It works with years, months, weeks, and days.
[17] Fix | Delete
# See the Date class documentation for more details.
[18] Fix | Delete
#
[19] Fix | Delete
# The second, DateTime, extends Date to include hours,
[20] Fix | Delete
# minutes, seconds, and fractions of a second. It
[21] Fix | Delete
# provides basic support for time zones. See the
[22] Fix | Delete
# DateTime class documentation for more details.
[23] Fix | Delete
#
[24] Fix | Delete
# === Ways of calculating the date.
[25] Fix | Delete
#
[26] Fix | Delete
# In common usage, the date is reckoned in years since or
[27] Fix | Delete
# before the Common Era (CE/BCE, also known as AD/BC), then
[28] Fix | Delete
# as a month and day-of-the-month within the current year.
[29] Fix | Delete
# This is known as the *Civil* *Date*, and abbreviated
[30] Fix | Delete
# as +civil+ in the Date class.
[31] Fix | Delete
#
[32] Fix | Delete
# Instead of year, month-of-the-year, and day-of-the-month,
[33] Fix | Delete
# the date can also be reckoned in terms of year and
[34] Fix | Delete
# day-of-the-year. This is known as the *Ordinal* *Date*,
[35] Fix | Delete
# and is abbreviated as +ordinal+ in the Date class. (Note
[36] Fix | Delete
# that referring to this as the Julian date is incorrect.)
[37] Fix | Delete
#
[38] Fix | Delete
# The date can also be reckoned in terms of year, week-of-the-year,
[39] Fix | Delete
# and day-of-the-week. This is known as the *Commercial*
[40] Fix | Delete
# *Date*, and is abbreviated as +commercial+ in the
[41] Fix | Delete
# Date class. The commercial week runs Monday (day-of-the-week
[42] Fix | Delete
# 1) to Sunday (day-of-the-week 7), in contrast to the civil
[43] Fix | Delete
# week which runs Sunday (day-of-the-week 0) to Saturday
[44] Fix | Delete
# (day-of-the-week 6). The first week of the commercial year
[45] Fix | Delete
# starts on the Monday on or before January 1, and the commercial
[46] Fix | Delete
# year itself starts on this Monday, not January 1.
[47] Fix | Delete
#
[48] Fix | Delete
# For scientific purposes, it is convenient to refer to a date
[49] Fix | Delete
# simply as a day count, counting from an arbitrary initial
[50] Fix | Delete
# day. The date first chosen for this was January 1, 4713 BCE.
[51] Fix | Delete
# A count of days from this date is the *Julian* *Day* *Number*
[52] Fix | Delete
# or *Julian* *Date*, which is abbreviated as +jd+ in the
[53] Fix | Delete
# Date class. This is in local time, and counts from midnight
[54] Fix | Delete
# on the initial day. The stricter usage is in UTC, and counts
[55] Fix | Delete
# from midday on the initial day. This is referred to in the
[56] Fix | Delete
# Date class as the *Astronomical* *Julian* *Day* *Number*, and
[57] Fix | Delete
# abbreviated as +ajd+. In the Date class, the Astronomical
[58] Fix | Delete
# Julian Day Number includes fractional days.
[59] Fix | Delete
#
[60] Fix | Delete
# Another absolute day count is the *Modified* *Julian* *Day*
[61] Fix | Delete
# *Number*, which takes November 17, 1858 as its initial day.
[62] Fix | Delete
# This is abbreviated as +mjd+ in the Date class. There
[63] Fix | Delete
# is also an *Astronomical* *Modified* *Julian* *Day* *Number*,
[64] Fix | Delete
# which is in UTC and includes fractional days. This is
[65] Fix | Delete
# abbreviated as +amjd+ in the Date class. Like the Modified
[66] Fix | Delete
# Julian Day Number (and unlike the Astronomical Julian
[67] Fix | Delete
# Day Number), it counts from midnight.
[68] Fix | Delete
#
[69] Fix | Delete
# Alternative calendars such as the Chinese Lunar Calendar,
[70] Fix | Delete
# the Islamic Calendar, or the French Revolutionary Calendar
[71] Fix | Delete
# are not supported by the Date class; nor are calendars that
[72] Fix | Delete
# are based on an Era different from the Common Era, such as
[73] Fix | Delete
# the Japanese Imperial Calendar or the Republic of China
[74] Fix | Delete
# Calendar.
[75] Fix | Delete
#
[76] Fix | Delete
# === Calendar Reform
[77] Fix | Delete
#
[78] Fix | Delete
# The standard civil year is 365 days long. However, the
[79] Fix | Delete
# solar year is fractionally longer than this. To account
[80] Fix | Delete
# for this, a *leap* *year* is occasionally inserted. This
[81] Fix | Delete
# is a year with 366 days, the extra day falling on February 29.
[82] Fix | Delete
# In the early days of the civil calendar, every fourth
[83] Fix | Delete
# year without exception was a leap year. This way of
[84] Fix | Delete
# reckoning leap years is the *Julian* *Calendar*.
[85] Fix | Delete
#
[86] Fix | Delete
# However, the solar year is marginally shorter than 365 1/4
[87] Fix | Delete
# days, and so the *Julian* *Calendar* gradually ran slow
[88] Fix | Delete
# over the centuries. To correct this, every 100th year
[89] Fix | Delete
# (but not every 400th year) was excluded as a leap year.
[90] Fix | Delete
# This way of reckoning leap years, which we use today, is
[91] Fix | Delete
# the *Gregorian* *Calendar*.
[92] Fix | Delete
#
[93] Fix | Delete
# The Gregorian Calendar was introduced at different times
[94] Fix | Delete
# in different regions. The day on which it was introduced
[95] Fix | Delete
# for a particular region is the *Day* *of* *Calendar*
[96] Fix | Delete
# *Reform* for that region. This is abbreviated as +sg+
[97] Fix | Delete
# (for Start of Gregorian calendar) in the Date class.
[98] Fix | Delete
#
[99] Fix | Delete
# Two such days are of particular
[100] Fix | Delete
# significance. The first is October 15, 1582, which was
[101] Fix | Delete
# the Day of Calendar Reform for Italy and most Catholic
[102] Fix | Delete
# countries. The second is September 14, 1752, which was
[103] Fix | Delete
# the Day of Calendar Reform for England and its colonies
[104] Fix | Delete
# (including what is now the United States). These two
[105] Fix | Delete
# dates are available as the constants Date::ITALY and
[106] Fix | Delete
# Date::ENGLAND, respectively. (By comparison, Germany and
[107] Fix | Delete
# Holland, less Catholic than Italy but less stubborn than
[108] Fix | Delete
# England, changed over in 1698; Sweden in 1753; Russia not
[109] Fix | Delete
# till 1918, after the Revolution; and Greece in 1923. Many
[110] Fix | Delete
# Orthodox churches still use the Julian Calendar. A complete
[111] Fix | Delete
# list of Days of Calendar Reform can be found at
[112] Fix | Delete
# http://www.polysyllabic.com/GregConv.html.)
[113] Fix | Delete
#
[114] Fix | Delete
# Switching from the Julian to the Gregorian calendar
[115] Fix | Delete
# involved skipping a number of days to make up for the
[116] Fix | Delete
# accumulated lag, and the later the switch was (or is)
[117] Fix | Delete
# done, the more days need to be skipped. So in 1582 in Italy,
[118] Fix | Delete
# 4th October was followed by 15th October, skipping 10 days; in 1752
[119] Fix | Delete
# in England, 2nd September was followed by 14th September, skipping
[120] Fix | Delete
# 11 days; and if I decided to switch from Julian to Gregorian
[121] Fix | Delete
# Calendar this midnight, I would go from 27th July 2003 (Julian)
[122] Fix | Delete
# today to 10th August 2003 (Gregorian) tomorrow, skipping
[123] Fix | Delete
# 13 days. The Date class is aware of this gap, and a supposed
[124] Fix | Delete
# date that would fall in the middle of it is regarded as invalid.
[125] Fix | Delete
#
[126] Fix | Delete
# The Day of Calendar Reform is relevant to all date representations
[127] Fix | Delete
# involving years. It is not relevant to the Julian Day Numbers,
[128] Fix | Delete
# except for converting between them and year-based representations.
[129] Fix | Delete
#
[130] Fix | Delete
# In the Date and DateTime classes, the Day of Calendar Reform or
[131] Fix | Delete
# +sg+ can be specified a number of ways. First, it can be as
[132] Fix | Delete
# the Julian Day Number of the Day of Calendar Reform. Second,
[133] Fix | Delete
# it can be using the constants Date::ITALY or Date::ENGLAND; these
[134] Fix | Delete
# are in fact the Julian Day Numbers of the Day of Calendar Reform
[135] Fix | Delete
# of the respective regions. Third, it can be as the constant
[136] Fix | Delete
# Date::JULIAN, which means to always use the Julian Calendar.
[137] Fix | Delete
# Finally, it can be as the constant Date::GREGORIAN, which means
[138] Fix | Delete
# to always use the Gregorian Calendar.
[139] Fix | Delete
#
[140] Fix | Delete
# Note: in the Julian Calendar, New Years Day was March 25. The
[141] Fix | Delete
# Date class does not follow this convention.
[142] Fix | Delete
#
[143] Fix | Delete
# === Time Zones
[144] Fix | Delete
#
[145] Fix | Delete
# DateTime objects support a simple representation
[146] Fix | Delete
# of time zones. Time zones are represented as an offset
[147] Fix | Delete
# from UTC, as a fraction of a day. This offset is the
[148] Fix | Delete
# how much local time is later (or earlier) than UTC.
[149] Fix | Delete
# UTC offset 0 is centred on England (also known as GMT).
[150] Fix | Delete
# As you travel east, the offset increases until you
[151] Fix | Delete
# reach the dateline in the middle of the Pacific Ocean;
[152] Fix | Delete
# as you travel west, the offset decreases. This offset
[153] Fix | Delete
# is abbreviated as +of+ in the Date class.
[154] Fix | Delete
#
[155] Fix | Delete
# This simple representation of time zones does not take
[156] Fix | Delete
# into account the common practice of Daylight Savings
[157] Fix | Delete
# Time or Summer Time.
[158] Fix | Delete
#
[159] Fix | Delete
# Most DateTime methods return the date and the
[160] Fix | Delete
# time in local time. The two exceptions are
[161] Fix | Delete
# #ajd() and #amjd(), which return the date and time
[162] Fix | Delete
# in UTC time, including fractional days.
[163] Fix | Delete
#
[164] Fix | Delete
# The Date class does not support time zone offsets, in that
[165] Fix | Delete
# there is no way to create a Date object with a time zone.
[166] Fix | Delete
# However, methods of the Date class when used by a
[167] Fix | Delete
# DateTime instance will use the time zone offset of this
[168] Fix | Delete
# instance.
[169] Fix | Delete
#
[170] Fix | Delete
# == Examples of use
[171] Fix | Delete
#
[172] Fix | Delete
# === Print out the date of every Sunday between two dates.
[173] Fix | Delete
#
[174] Fix | Delete
# def print_sundays(d1, d2)
[175] Fix | Delete
# d1 +=1 while (d1.wday != 0)
[176] Fix | Delete
# d1.step(d2, 7) do |date|
[177] Fix | Delete
# puts "#{Date::MONTHNAMES[date.mon]} #{date.day}"
[178] Fix | Delete
# end
[179] Fix | Delete
# end
[180] Fix | Delete
#
[181] Fix | Delete
# print_sundays(Date::civil(2003, 4, 8), Date::civil(2003, 5, 23))
[182] Fix | Delete
#
[183] Fix | Delete
# === Calculate how many seconds to go till midnight on New Year's Day.
[184] Fix | Delete
#
[185] Fix | Delete
# def secs_to_new_year(now = DateTime::now())
[186] Fix | Delete
# new_year = DateTime.new(now.year + 1, 1, 1)
[187] Fix | Delete
# dif = new_year - now
[188] Fix | Delete
# hours, mins, secs, ignore_fractions = Date::day_fraction_to_time(dif)
[189] Fix | Delete
# return hours * 60 * 60 + mins * 60 + secs
[190] Fix | Delete
# end
[191] Fix | Delete
#
[192] Fix | Delete
# puts secs_to_new_year()
[193] Fix | Delete
[194] Fix | Delete
require 'rational'
[195] Fix | Delete
require 'date/format'
[196] Fix | Delete
[197] Fix | Delete
# Class representing a date.
[198] Fix | Delete
#
[199] Fix | Delete
# See the documentation to the file date.rb for an overview.
[200] Fix | Delete
#
[201] Fix | Delete
# Internally, the date is represented as an Astronomical
[202] Fix | Delete
# Julian Day Number, +ajd+. The Day of Calendar Reform, +sg+, is
[203] Fix | Delete
# also stored, for conversions to other date formats. (There
[204] Fix | Delete
# is also an +of+ field for a time zone offset, but this
[205] Fix | Delete
# is only for the use of the DateTime subclass.)
[206] Fix | Delete
#
[207] Fix | Delete
# A new Date object is created using one of the object creation
[208] Fix | Delete
# class methods named after the corresponding date format, and the
[209] Fix | Delete
# arguments appropriate to that date format; for instance,
[210] Fix | Delete
# Date::civil() (aliased to Date::new()) with year, month,
[211] Fix | Delete
# and day-of-month, or Date::ordinal() with year and day-of-year.
[212] Fix | Delete
# All of these object creation class methods also take the
[213] Fix | Delete
# Day of Calendar Reform as an optional argument.
[214] Fix | Delete
#
[215] Fix | Delete
# Date objects are immutable once created.
[216] Fix | Delete
#
[217] Fix | Delete
# Once a Date has been created, date values
[218] Fix | Delete
# can be retrieved for the different date formats supported
[219] Fix | Delete
# using instance methods. For instance, #mon() gives the
[220] Fix | Delete
# Civil month, #cwday() gives the Commercial day of the week,
[221] Fix | Delete
# and #yday() gives the Ordinal day of the year. Date values
[222] Fix | Delete
# can be retrieved in any format, regardless of what format
[223] Fix | Delete
# was used to create the Date instance.
[224] Fix | Delete
#
[225] Fix | Delete
# The Date class includes the Comparable module, allowing
[226] Fix | Delete
# date objects to be compared and sorted, ranges of dates
[227] Fix | Delete
# to be created, and so forth.
[228] Fix | Delete
class Date
[229] Fix | Delete
[230] Fix | Delete
include Comparable
[231] Fix | Delete
[232] Fix | Delete
# Full month names, in English. Months count from 1 to 12; a
[233] Fix | Delete
# month's numerical representation indexed into this array
[234] Fix | Delete
# gives the name of that month (hence the first element is nil).
[235] Fix | Delete
MONTHNAMES = [nil] + %w(January February March April May June July
[236] Fix | Delete
August September October November December)
[237] Fix | Delete
[238] Fix | Delete
# Full names of days of the week, in English. Days of the week
[239] Fix | Delete
# count from 0 to 6 (except in the commercial week); a day's numerical
[240] Fix | Delete
# representation indexed into this array gives the name of that day.
[241] Fix | Delete
DAYNAMES = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday)
[242] Fix | Delete
[243] Fix | Delete
# Abbreviated month names, in English.
[244] Fix | Delete
ABBR_MONTHNAMES = [nil] + %w(Jan Feb Mar Apr May Jun
[245] Fix | Delete
Jul Aug Sep Oct Nov Dec)
[246] Fix | Delete
[247] Fix | Delete
# Abbreviated day names, in English.
[248] Fix | Delete
ABBR_DAYNAMES = %w(Sun Mon Tue Wed Thu Fri Sat)
[249] Fix | Delete
[250] Fix | Delete
[MONTHNAMES, DAYNAMES, ABBR_MONTHNAMES, ABBR_DAYNAMES].each do |xs|
[251] Fix | Delete
xs.each{|x| x.freeze unless x.nil?}.freeze
[252] Fix | Delete
end
[253] Fix | Delete
[254] Fix | Delete
class Infinity < Numeric # :nodoc:
[255] Fix | Delete
[256] Fix | Delete
include Comparable
[257] Fix | Delete
[258] Fix | Delete
def initialize(d=1) @d = d <=> 0 end
[259] Fix | Delete
[260] Fix | Delete
def d() @d end
[261] Fix | Delete
[262] Fix | Delete
protected :d
[263] Fix | Delete
[264] Fix | Delete
def zero? () false end
[265] Fix | Delete
def finite? () false end
[266] Fix | Delete
def infinite? () d.nonzero? end
[267] Fix | Delete
def nan? () d.zero? end
[268] Fix | Delete
[269] Fix | Delete
def abs() self.class.new end
[270] Fix | Delete
[271] Fix | Delete
def -@ () self.class.new(-d) end
[272] Fix | Delete
def +@ () self.class.new(+d) end
[273] Fix | Delete
[274] Fix | Delete
def <=> (other)
[275] Fix | Delete
case other
[276] Fix | Delete
when Infinity; return d <=> other.d
[277] Fix | Delete
when Numeric; return d
[278] Fix | Delete
else
[279] Fix | Delete
begin
[280] Fix | Delete
l, r = other.coerce(self)
[281] Fix | Delete
return l <=> r
[282] Fix | Delete
rescue NoMethodError
[283] Fix | Delete
end
[284] Fix | Delete
end
[285] Fix | Delete
nil
[286] Fix | Delete
end
[287] Fix | Delete
[288] Fix | Delete
def coerce(other)
[289] Fix | Delete
case other
[290] Fix | Delete
when Numeric; return -d, d
[291] Fix | Delete
else
[292] Fix | Delete
super
[293] Fix | Delete
end
[294] Fix | Delete
end
[295] Fix | Delete
[296] Fix | Delete
end
[297] Fix | Delete
[298] Fix | Delete
# The Julian Day Number of the Day of Calendar Reform for Italy
[299] Fix | Delete
# and the Catholic countries.
[300] Fix | Delete
ITALY = 2299161 # 1582-10-15
[301] Fix | Delete
[302] Fix | Delete
# The Julian Day Number of the Day of Calendar Reform for England
[303] Fix | Delete
# and her Colonies.
[304] Fix | Delete
ENGLAND = 2361222 # 1752-09-14
[305] Fix | Delete
[306] Fix | Delete
# A constant used to indicate that a Date should always use the
[307] Fix | Delete
# Julian calendar.
[308] Fix | Delete
JULIAN = Infinity.new
[309] Fix | Delete
[310] Fix | Delete
# A constant used to indicate that a Date should always use the
[311] Fix | Delete
# Gregorian calendar.
[312] Fix | Delete
GREGORIAN = -Infinity.new
[313] Fix | Delete
[314] Fix | Delete
HALF_DAYS_IN_DAY = Rational(1, 2) # :nodoc:
[315] Fix | Delete
HOURS_IN_DAY = Rational(1, 24) # :nodoc:
[316] Fix | Delete
MINUTES_IN_DAY = Rational(1, 1440) # :nodoc:
[317] Fix | Delete
SECONDS_IN_DAY = Rational(1, 86400) # :nodoc:
[318] Fix | Delete
MILLISECONDS_IN_DAY = Rational(1, 86400*10**3) # :nodoc:
[319] Fix | Delete
NANOSECONDS_IN_DAY = Rational(1, 86400*10**9) # :nodoc:
[320] Fix | Delete
MILLISECONDS_IN_SECOND = Rational(1, 10**3) # :nodoc:
[321] Fix | Delete
NANOSECONDS_IN_SECOND = Rational(1, 10**9) # :nodoc:
[322] Fix | Delete
[323] Fix | Delete
MJD_EPOCH_IN_AJD = Rational(4800001, 2) # 1858-11-17 # :nodoc:
[324] Fix | Delete
UNIX_EPOCH_IN_AJD = Rational(4881175, 2) # 1970-01-01 # :nodoc:
[325] Fix | Delete
MJD_EPOCH_IN_CJD = 2400001 # :nodoc:
[326] Fix | Delete
UNIX_EPOCH_IN_CJD = 2440588 # :nodoc:
[327] Fix | Delete
LD_EPOCH_IN_CJD = 2299160 # :nodoc:
[328] Fix | Delete
[329] Fix | Delete
# Does a given Julian Day Number fall inside the old-style (Julian)
[330] Fix | Delete
# calendar?
[331] Fix | Delete
#
[332] Fix | Delete
# +jd+ is the Julian Day Number in question. +sg+ may be Date::GREGORIAN,
[333] Fix | Delete
# in which case the answer is false; it may be Date::JULIAN, in which case
[334] Fix | Delete
# the answer is true; or it may a number representing the Day of
[335] Fix | Delete
# Calendar Reform. Date::ENGLAND and Date::ITALY are two possible such
[336] Fix | Delete
# days.
[337] Fix | Delete
[338] Fix | Delete
def self.julian? (jd, sg)
[339] Fix | Delete
case sg
[340] Fix | Delete
when Numeric
[341] Fix | Delete
jd < sg
[342] Fix | Delete
else
[343] Fix | Delete
if $VERBOSE
[344] Fix | Delete
warn("#{caller.shift.sub(/:in .*/, '')}: " \
[345] Fix | Delete
"warning: do not use non-numerical object as julian day number anymore")
[346] Fix | Delete
end
[347] Fix | Delete
not sg
[348] Fix | Delete
end
[349] Fix | Delete
end
[350] Fix | Delete
[351] Fix | Delete
# Does a given Julian Day Number fall inside the new-style (Gregorian)
[352] Fix | Delete
# calendar?
[353] Fix | Delete
#
[354] Fix | Delete
# The reverse of self.os? See the documentation for that method for
[355] Fix | Delete
# more details.
[356] Fix | Delete
def self.gregorian? (jd, sg) !julian?(jd, sg) end
[357] Fix | Delete
[358] Fix | Delete
def self.fix_style(jd, sg) # :nodoc:
[359] Fix | Delete
if julian?(jd, sg)
[360] Fix | Delete
then JULIAN
[361] Fix | Delete
else GREGORIAN end
[362] Fix | Delete
end
[363] Fix | Delete
[364] Fix | Delete
private_class_method :fix_style
[365] Fix | Delete
[366] Fix | Delete
# Convert an Ordinal Date to a Julian Day Number.
[367] Fix | Delete
#
[368] Fix | Delete
# +y+ and +d+ are the year and day-of-year to convert.
[369] Fix | Delete
# +sg+ specifies the Day of Calendar Reform.
[370] Fix | Delete
#
[371] Fix | Delete
# Returns the corresponding Julian Day Number.
[372] Fix | Delete
def self.ordinal_to_jd(y, d, sg=GREGORIAN)
[373] Fix | Delete
civil_to_jd(y, 1, d, sg)
[374] Fix | Delete
end
[375] Fix | Delete
[376] Fix | Delete
# Convert a Julian Day Number to an Ordinal Date.
[377] Fix | Delete
#
[378] Fix | Delete
# +jd+ is the Julian Day Number to convert.
[379] Fix | Delete
# +sg+ specifies the Day of Calendar Reform.
[380] Fix | Delete
#
[381] Fix | Delete
# Returns the corresponding Ordinal Date as
[382] Fix | Delete
# [year, day_of_year]
[383] Fix | Delete
def self.jd_to_ordinal(jd, sg=GREGORIAN)
[384] Fix | Delete
y = jd_to_civil(jd, sg)[0]
[385] Fix | Delete
doy = jd - civil_to_jd(y - 1, 12, 31, fix_style(jd, sg))
[386] Fix | Delete
return y, doy
[387] Fix | Delete
end
[388] Fix | Delete
[389] Fix | Delete
# Convert a Civil Date to a Julian Day Number.
[390] Fix | Delete
# +y+, +m+, and +d+ are the year, month, and day of the
[391] Fix | Delete
# month. +sg+ specifies the Day of Calendar Reform.
[392] Fix | Delete
#
[393] Fix | Delete
# Returns the corresponding Julian Day Number.
[394] Fix | Delete
def self.civil_to_jd(y, m, d, sg=GREGORIAN)
[395] Fix | Delete
if m <= 2
[396] Fix | Delete
y -= 1
[397] Fix | Delete
m += 12
[398] Fix | Delete
end
[399] Fix | Delete
a = (y / 100.0).floor
[400] Fix | Delete
b = 2 - a + (a / 4.0).floor
[401] Fix | Delete
jd = (365.25 * (y + 4716)).floor +
[402] Fix | Delete
(30.6001 * (m + 1)).floor +
[403] Fix | Delete
d + b - 1524
[404] Fix | Delete
if julian?(jd, sg)
[405] Fix | Delete
jd -= b
[406] Fix | Delete
end
[407] Fix | Delete
jd
[408] Fix | Delete
end
[409] Fix | Delete
[410] Fix | Delete
# Convert a Julian Day Number to a Civil Date. +jd+ is
[411] Fix | Delete
# the Julian Day Number. +sg+ specifies the Day of
[412] Fix | Delete
# Calendar Reform.
[413] Fix | Delete
#
[414] Fix | Delete
# Returns the corresponding [year, month, day_of_month]
[415] Fix | Delete
# as a three-element array.
[416] Fix | Delete
def self.jd_to_civil(jd, sg=GREGORIAN)
[417] Fix | Delete
if julian?(jd, sg)
[418] Fix | Delete
a = jd
[419] Fix | Delete
else
[420] Fix | Delete
x = ((jd - 1867216.25) / 36524.25).floor
[421] Fix | Delete
a = jd + 1 + x - (x / 4.0).floor
[422] Fix | Delete
end
[423] Fix | Delete
b = a + 1524
[424] Fix | Delete
c = ((b - 122.1) / 365.25).floor
[425] Fix | Delete
d = (365.25 * c).floor
[426] Fix | Delete
e = ((b - d) / 30.6001).floor
[427] Fix | Delete
dom = b - d - (30.6001 * e).floor
[428] Fix | Delete
if e <= 13
[429] Fix | Delete
m = e - 1
[430] Fix | Delete
y = c - 4716
[431] Fix | Delete
else
[432] Fix | Delete
m = e - 13
[433] Fix | Delete
y = c - 4715
[434] Fix | Delete
end
[435] Fix | Delete
return y, m, dom
[436] Fix | Delete
end
[437] Fix | Delete
[438] Fix | Delete
# Convert a Commercial Date to a Julian Day Number.
[439] Fix | Delete
#
[440] Fix | Delete
# +y+, +w+, and +d+ are the (commercial) year, week of the year,
[441] Fix | Delete
# and day of the week of the Commercial Date to convert.
[442] Fix | Delete
# +sg+ specifies the Day of Calendar Reform.
[443] Fix | Delete
def self.commercial_to_jd(y, w, d, ns=GREGORIAN)
[444] Fix | Delete
jd = civil_to_jd(y, 1, 4, ns)
[445] Fix | Delete
(jd - (((jd - 1) + 1) % 7)) +
[446] Fix | Delete
7 * (w - 1) +
[447] Fix | Delete
(d - 1)
[448] Fix | Delete
end
[449] Fix | Delete
[450] Fix | Delete
# Convert a Julian Day Number to a Commercial Date
[451] Fix | Delete
#
[452] Fix | Delete
# +jd+ is the Julian Day Number to convert.
[453] Fix | Delete
# +sg+ specifies the Day of Calendar Reform.
[454] Fix | Delete
#
[455] Fix | Delete
# Returns the corresponding Commercial Date as
[456] Fix | Delete
# [commercial_year, week_of_year, day_of_week]
[457] Fix | Delete
def self.jd_to_commercial(jd, sg=GREGORIAN)
[458] Fix | Delete
ns = fix_style(jd, sg)
[459] Fix | Delete
a = jd_to_civil(jd - 3, ns)[0]
[460] Fix | Delete
y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end
[461] Fix | Delete
w = 1 + ((jd - commercial_to_jd(y, 1, 1, ns)) / 7).floor
[462] Fix | Delete
d = (jd + 1) % 7
[463] Fix | Delete
d = 7 if d == 0
[464] Fix | Delete
return y, w, d
[465] Fix | Delete
end
[466] Fix | Delete
[467] Fix | Delete
def self.weeknum_to_jd(y, w, d, f=0, ns=GREGORIAN) # :nodoc:
[468] Fix | Delete
a = civil_to_jd(y, 1, 1, ns) + 6
[469] Fix | Delete
(a - ((a - f) + 1) % 7 - 7) + 7 * w + d
[470] Fix | Delete
end
[471] Fix | Delete
[472] Fix | Delete
def self.jd_to_weeknum(jd, f=0, sg=GREGORIAN) # :nodoc:
[473] Fix | Delete
ns = fix_style(jd, sg)
[474] Fix | Delete
y, m, d = jd_to_civil(jd, ns)
[475] Fix | Delete
a = civil_to_jd(y, 1, 1, ns) + 6
[476] Fix | Delete
w, d = (jd - (a - ((a - f) + 1) % 7) + 7).divmod(7)
[477] Fix | Delete
return y, w, d
[478] Fix | Delete
end
[479] Fix | Delete
[480] Fix | Delete
private_class_method :weeknum_to_jd, :jd_to_weeknum
[481] Fix | Delete
[482] Fix | Delete
# Convert an Astronomical Julian Day Number to a (civil) Julian
[483] Fix | Delete
# Day Number.
[484] Fix | Delete
#
[485] Fix | Delete
# +ajd+ is the Astronomical Julian Day Number to convert.
[486] Fix | Delete
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
[487] Fix | Delete
#
[488] Fix | Delete
# Returns the (civil) Julian Day Number as [day_number,
[489] Fix | Delete
# fraction] where +fraction+ is always 1/2.
[490] Fix | Delete
def self.ajd_to_jd(ajd, of=0) (ajd + of + HALF_DAYS_IN_DAY).divmod(1) end
[491] Fix | Delete
[492] Fix | Delete
# Convert a (civil) Julian Day Number to an Astronomical Julian
[493] Fix | Delete
# Day Number.
[494] Fix | Delete
#
[495] Fix | Delete
# +jd+ is the Julian Day Number to convert, and +fr+ is a
[496] Fix | Delete
# fractional day.
[497] Fix | Delete
# +of+ is the offset from UTC as a fraction of a day (defaults to 0).
[498] Fix | Delete
#
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function