Project

General

Profile

Bug #8485 ยป gregorian_periods.patch

Patch for dd87e4631 (trunk) - teleological (Riley Lynch), 06/04/2013 03:52 PM

View differences:

ext/date/date_core.c
2129 2129
}
2130 2130
#endif
2131 2131

  
2132
inline static int
2133
gregorian_end_of_period_p(int y)
2134
{
2135
    return (y + 4712 + 1) == CM_PERIOD_GCY;
2136
}
2137

  
2138
inline static void
2139
increment_period(VALUE *nth, int *ry)
2140
{
2141
    *nth = f_add(*nth, INT2FIX(1));
2142
    *ry -= CM_PERIOD_GCY;
2143
}
2144

  
2132 2145
static int
2133 2146
valid_ordinal_p(VALUE y, int d, double sg,
2134 2147
		VALUE *nth, int *ry,
......
2154 2167
    }
2155 2168
    else {
2156 2169
	decode_year(y, style, nth, ry);
2170
	if ((d > 327) && gregorian_end_of_period_p(*ry))
2171
	    increment_period(nth, ry);
2157 2172
	r = c_valid_ordinal_p(*ry, d, style, rd, rjd, ns);
2158 2173
    }
2159 2174
    return r;
2160 2175
}
2161 2176

  
2177
inline static void
2178
ensure_gregorian_civil_period(VALUE *nth, int *ry, int m, int d)
2179
{
2180
    if ((m == 12 || (m == 11 && d > 23)) && gregorian_end_of_period_p(*ry)) {
2181
        increment_period(nth, ry);
2182
    }
2183
}
2184

  
2162 2185
static int
2163 2186
valid_gregorian_p(VALUE y, int m, int d,
2164 2187
		  VALUE *nth, int *ry,
2165 2188
		  int *rm, int *rd)
2166 2189
{
2167 2190
    decode_year(y, -1, nth, ry);
2191
    ensure_gregorian_civil_period(nth, ry, m, d);
2168 2192
    return c_valid_gregorian_p(*ry, m, d, rm, rd);
2169 2193
}
2170 2194

  
......
2193 2217
    }
2194 2218
    else {
2195 2219
	decode_year(y, style, nth, ry);
2196
	if (style < 0)
2220
	if (style < 0) {
2221
	    ensure_gregorian_civil_period(nth, ry, m, d);
2197 2222
	    r = c_valid_gregorian_p(*ry, m, d, rm, rd);
2223
	}
2198 2224
	else
2199 2225
	    r = c_valid_julian_p(*ry, m, d, rm, rd);
2200 2226
	if (!r)
......
2229 2255
    }
2230 2256
    else {
2231 2257
	decode_year(y, style, nth, ry);
2258
	if ((w > 47) && gregorian_end_of_period_p(*ry))
2259
	    increment_period(nth, ry);
2232 2260
	r = c_valid_commercial_p(*ry, w, d, style, rw, rd, rjd, ns);
2233 2261
    }
2234 2262
    return r;
......
3617 3645
    d = tm.tm_mday;
3618 3646

  
3619 3647
    decode_year(INT2FIX(y), -1, &nth, &ry);
3648
    ensure_gregorian_civil_period(&nth, &ry, m, d);
3620 3649

  
3621 3650
    ret = d_simple_new_internal(klass,
3622 3651
				nth, 0,
......
7811 7840
    }
7812 7841

  
7813 7842
    decode_year(INT2FIX(y), -1, &nth, &ry);
7843
    ensure_gregorian_civil_period(&nth, &ry, m, d);
7814 7844

  
7815 7845
    ret = d_complex_new_internal(klass,
7816 7846
				 nth, 0,
......
8541 8571
    d = FIX2INT(f_mday(self));
8542 8572

  
8543 8573
    decode_year(y, -1, &nth, &ry);
8574
    ensure_gregorian_civil_period(&nth, &ry, m, d);
8544 8575

  
8545 8576
    ret = d_simple_new_internal(cDate,
8546 8577
				nth, 0,
......
8580 8611
    of = FIX2INT(f_utc_offset(self));
8581 8612

  
8582 8613
    decode_year(y, -1, &nth, &ry);
8614
    ensure_gregorian_civil_period(&nth, &ry, m, d);
8583 8615

  
8584 8616
    ret = d_complex_new_internal(cDateTime,
8585 8617
				 nth, 0,
test/date/test_date_arith.rb
80 80
		     Date.new(2001,1,17, Date::GREGORIAN)))
81 81
    assert_equal(0, (DateTime.new(2001,1,4,0,0,0,0,Date::JULIAN) <=>
82 82
		     DateTime.new(2001,1,17,0,0,0,0,Date::GREGORIAN)))
83

  
84
    assert_equal(0, (Date.jd(0) <=> Date.civil(-4713, 11, 24, Date::GREGORIAN)))
85
    assert_equal(0, (Date.jd(213447717) <=> Date.civil(579687, 11, 24)))
86
    assert_equal(0, (Date.jd(-213447717) <=> Date.civil(-589113, 11, 24, Date::GREGORIAN)))
83 87
  end
84 88

  
85 89
  def test_prev