1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
|
# date2.rb: Written by Tadayoshi Funaba 1998, 1999
# $Id: date2.rb,v 1.17 1999/09/15 05:34:07 tadf Exp $
class Date
include Comparable
IDENT = 2
MONTHNAMES = [ nil, 'January', 'February', 'March',
'April', 'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December' ]
DAYNAMES = [ 'Sunday', 'Monday', 'Tuesday',
'Wednesday', 'Thursday', 'Friday', 'Saturday' ]
ITALY = 2299161 # 1582-10-15
ENGLAND = 2361222 # 1752-09-14
JULIAN = false
GREGORIAN = true
class << self
def os? (jd, sg)
case sg
when Numeric; jd < sg
else; not sg
end
end
def ns? (jd, sg) not os?(jd, sg) end
def civil_to_jd(y, m, d, sg=GREGORIAN)
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 os?(jd, sg)
jd -= b
end
jd
end
def jd_to_civil(jd, sg=GREGORIAN)
if os?(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
def ordinal_to_jd(y, d, sg=GREGORIAN)
civil_to_jd(y, 1, d, sg)
end
def jd_to_ordinal(jd, sg=GREGORIAN)
y = jd_to_civil(jd, sg)[0]
doy = jd - civil_to_jd(y - 1, 12, 31, ns?(jd, sg))
return y, doy
end
def jd_to_commercial(jd, sg=GREGORIAN)
ns = ns?(jd, sg)
a = jd_to_civil(jd - 3, ns)[0]
y = if jd >= commercial_to_jd(a + 1, 1, 1, ns) then a + 1 else a end
w = 1 + (jd - commercial_to_jd(y, 1, 1, ns)) / 7
d = (jd + 1) % 7
if d.zero? then d = 7 end
return y, w, d
end
def commercial_to_jd(y, w, d, ns=GREGORIAN)
jd = civil_to_jd(y, 1, 4, ns)
(jd - (((jd - 1) + 1) % 7)) +
7 * (w - 1) +
(d - 1)
end
def clfloor(x, y=1)
q = (x / y).to_i
z = (q * y)
q -= 1 if (y > 0 and x < z) or (y < 0 and x > z)
r = x - q * y
return q, r
end
def rjd_to_jd(rjd) clfloor(rjd + 0.5) end
def jd_to_rjd(jd, fr) jd + fr - 0.5 end
def mjd_to_jd(mjd) mjd + 2400000.5 end
def jd_to_mjd(jd) jd - 2400000.5 end
def tjd_to_jd(tjd) tjd + 2440000.5 end
def jd_to_tjd(jd) jd - 2440000.5 end
def julian_leap? (y) y % 4 == 0 end
def gregorian_leap? (y) y % 4 == 0 and y % 100 != 0 or y % 400 == 0 end
alias_method :leap?, :gregorian_leap?
def new1(jd=0, sg=ITALY) new(jd, sg) end
def exist3? (y, m, d, sg=ITALY)
if m < 0
m += 13
end
if d < 0
ny, nm = Date.clfloor(y * 12 + m, 12)
nm, = Date.clfloor(m + 1, 1)
la = nil
31.downto 1 do |z|
break if la = exist3?(y, m, z, sg)
end
ns = ns?(la, sg)
d = jd_to_civil(civil_to_jd(ny, nm, 1, ns) + d, ns)[-1]
end
jd = civil_to_jd(y, m, d, sg)
return unless [y, m, d] == jd_to_civil(jd, sg)
jd
end
alias_method :exist?, :exist3?
def new3(y=-4712, m=1, d=1, sg=ITALY)
unless jd = exist3?(y, m, d, sg)
fail ArgumentError, 'invalid date'
end
new(jd, sg)
end
def exist2? (y, d, sg=ITALY)
if d < 0
ny = y + 1
la = nil
366.downto 1 do |z|
break if la = exist2?(y, z, sg)
end
ns = ns?(la, sg)
d = jd_to_ordinal(ordinal_to_jd(ny, 1, ns) + d, ns)[-1]
end
jd = ordinal_to_jd(y, d, sg)
return unless [y, d] == jd_to_ordinal(jd, sg)
jd
end
def new2(y=-4712, d=1, sg=ITALY)
unless jd = exist2?(y, d, sg)
fail ArgumentError, 'invalid date'
end
new(jd, sg)
end
def existw? (y, w, d, sg=ITALY)
if d < 0
d += 8
end
if w < 0
w = jd_to_commercial(commercial_to_jd(y + 1, 1, 1) + w * 7)[1]
end
jd = commercial_to_jd(y, w, d)
return unless ns?(jd, sg)
return unless [y, w, d] == jd_to_commercial(jd)
jd
end
def neww(y=1582, w=41, d=5, sg=ITALY)
unless jd = existw?(y, w, d, sg)
fail ArgumentError, 'invalid date'
end
new(jd, sg)
end
def today(sg=ITALY)
new(civil_to_jd(*(Time.now.to_a[3..5].reverse << sg)), sg)
end
def once(*ids)
for id in ids
module_eval <<-"end;"
alias_method :__#{id}__, #{id}
def #{id.id2name}(*args, &block)
def self.#{id.id2name}(*args, &block); @__#{id}__ end
@__#{id}__ = __#{id}__(*args, &block)
end
end;
end
end
private :once
end
def initialize(rjd=0, sg=ITALY) @rjd, @sg = rjd, sg end
def rjd() @rjd end
def rmjd() Date.jd_to_mjd(@rjd) end
def rtjd() Date.jd_to_tjd(@rjd) end
once :rmjd, :rtjd
def jd() Date.rjd_to_jd(@rjd)[0] end
def fr1() Date.rjd_to_jd(@rjd)[1] end
def mjd() Date.jd_to_mjd(jd) end
def tjd() Date.jd_to_tjd(jd) end
once :jd, :fr1, :mjd, :tjd
def civil() Date.jd_to_civil(jd, @sg) end
def ordinal() Date.jd_to_ordinal(jd, @sg) end
def commercial() Date.jd_to_commercial(jd, @sg) end
once :civil, :ordinal, :commercial
def year() civil[0] end
def yday() ordinal[1] end
def mon() civil[1] end
alias_method :month, :mon
once :year, :yday, :mon, :month
def mday() civil[2] end
alias_method :day, :mday
once :mday, :day
def cwyear() commercial[0] end
def cweek() commercial[1] end
def cwday() commercial[2] end
once :cwyear, :cweek, :cwday
def wday() (jd + 1) % 7 end
once :wday
def os? () Date.os?(jd, @sg) end
def ns? () Date.ns?(jd, @sg) end
once :os?, :ns?
def leap?
Date.jd_to_civil(Date.civil_to_jd(year, 3, 1, ns?) - 1,
ns?)[-1] == 29
end
once :leap?
def sg() @sg end
def newsg(sg=Date::ITALY) Date.new(@rjd, sg) end
def italy() newsg(Date::ITALY) end
def england() newsg(Date::ENGLAND) end
def julian() newsg(Date::JULIAN) end
def gregorian() newsg(Date::GREGORIAN) end
def + (n)
case n
when Numeric; return Date.new(@rjd + n, @sg)
end
fail TypeError, 'expected numeric'
end
def - (x)
case x
when Numeric; return Date.new(@rjd - x, @sg)
when Date; return @rjd - x.rjd
end
fail TypeError, 'expected numeric or date'
end
def <=> (other)
case other
when Numeric; return @rjd <=> other
when Date; return @rjd <=> other.rjd
end
fail TypeError, 'expected numeric or date'
end
def === (other)
case other
when Numeric; return jd == other
when Date; return jd == other.jd
end
fail TypeError, 'expected numeric or date'
end
def >> (n)
y, m = Date.clfloor(year * 12 + (mon - 1) + n, 12)
m, = Date.clfloor(m + 1, 1)
d = mday
d -= 1 until jd2 = Date.exist3?(y, m, d, ns?)
self + (jd2 - jd)
end
def << (n) self >> -n end
def step(limit, step)
rjd = @rjd
if (step > 0)
while rjd <= limit.rjd
yield Date.new(rjd, @sg)
rjd += step
end
else
while rjd >= limit.rjd
yield Date.new(rjd, @sg)
rjd += step
end
end
self
end
def upto(max, &block) step(max, +1, &block) end
def downto(min, &block) step(min, -1, &block) end
def succ() self + 1 end
alias_method :next, :succ
def eql? (other) Date === other and self == other end
def hash() Date.clfloor(@rjd)[0] end
def inspect() format('#<Date: %s,%s>', @rjd, @sg) end
def to_s() format('%.4d-%02d-%02d', year, mon, mday) end
def _dump(limit) Marshal.dump([@rjd, @sg], -1) end
def Date._load(str) Date.new(*Marshal.load(str)) end
end
|