1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* time.c (find_time_t): time guess strategy refined.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23936 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2009-07-02 15:37:06 +00:00
parent 6897fddd65
commit 26f32868c7
2 changed files with 139 additions and 104 deletions

View file

@ -1,3 +1,7 @@
Fri Jul 3 00:36:16 2009 Tanaka Akira <akr@fsij.org>
* time.c (find_time_t): time guess strategy refined.
Thu Jul 2 11:16:25 2009 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/imap.rb: added response to Net::IMAP::ResponseError.

71
time.c
View file

@ -1860,15 +1860,31 @@ timegm_noleapsecond(struct tm *tm)
DIV(tm_year+299,400))*86400;
}
#ifdef FIND_TIME_NUMGUESS
static unsigned long long find_time_numguess;
static VALUE find_time_numguess_getter(void)
{
return ULL2NUM(find_time_numguess);
}
#endif
static const char *
find_time_t(struct tm *tptr, int utc_p, time_t *tp)
{
time_t guess, guess_lo, guess_hi;
struct tm *tm, tm_lo, tm_hi;
int d, have_guess;
int d;
int find_dst;
struct tm result;
int try_interpolation;
unsigned long range;
#ifdef FIND_TIME_NUMGUESS
#define GUESS(p) (find_time_numguess++, (utc_p ? gmtime_with_leapsecond(p, &result) : LOCALTIME(p, result)))
#else
#define GUESS(p) (utc_p ? gmtime_with_leapsecond(p, &result) : LOCALTIME(p, result))
#endif
find_dst = 0 < tptr->tm_isdst;
@ -1918,13 +1934,13 @@ find_time_t(struct tm *tptr, int utc_p, time_t *tp)
if (d == 0) { guess = guess_hi; goto found; }
tm_hi = *tm;
have_guess = 0;
try_interpolation = 1;
while (guess_lo + 1 < guess_hi) {
/* there is a gap between guess_lo and guess_hi. */
unsigned long range = 0;
if (!have_guess) {
if (try_interpolation == 1) {
int a, b;
/* there is a gap between guess_lo and guess_hi. */
range = 0;
/*
Try precious guess by a linear interpolation at first.
`a' and `b' is a coefficient of guess_lo and guess_hi as:
@ -1995,10 +2011,20 @@ find_time_t(struct tm *tptr, int utc_p, time_t *tp)
*/
guess = guess_lo / d * a + (guess_lo % d) * a / d
+ guess_hi / d * b + (guess_hi % d) * b / d;
have_guess = 1;
try_interpolation = 2;
}
else if (try_interpolation == 2) {
guess = guess - range;
range = 0;
try_interpolation = 1;
}
else if (try_interpolation == 3) {
guess = guess + range;
range = 0;
try_interpolation = 1;
}
if (guess <= guess_lo || guess_hi <= guess) {
if (try_interpolation == 0 || guess <= guess_lo || guess_hi <= guess) {
/* Precious guess is invalid. try binary search. */
guess = guess_lo / 2 + guess_hi / 2;
if (guess <= guess_lo)
@ -2006,32 +2032,33 @@ find_time_t(struct tm *tptr, int utc_p, time_t *tp)
else if (guess >= guess_hi)
guess = guess_hi - 1;
range = 0;
try_interpolation = 1;
}
tm = GUESS(&guess);
if (!tm) goto error;
have_guess = 0;
d = tmcmp(tptr, tm);
if (d < 0) {
if (range)
try_interpolation = 2;
else if ((unsigned_time_t)(guess-guess_lo) > (unsigned_time_t)(guess_hi-guess))
try_interpolation = 0;
else
try_interpolation = 1;
guess_hi = guess;
tm_hi = *tm;
if (range) {
guess = guess - range;
range = 0;
if (guess_lo < guess && guess < guess_hi)
have_guess = 1;
}
}
else if (d > 0) {
if (range)
try_interpolation = 3;
else if ((unsigned_time_t)(guess-guess_lo) < (unsigned_time_t)(guess_hi-guess))
try_interpolation = 0;
else
try_interpolation = 1;
guess_lo = guess;
tm_lo = *tm;
if (range) {
guess = guess + range;
range = 0;
if (guess_lo < guess && guess < guess_hi)
have_guess = 1;
}
}
else {
found:
@ -3803,4 +3830,8 @@ Init_Time(void)
rb_define_method(rb_cTime, "marshal_dump", time_mdump, 0);
rb_define_method(rb_cTime, "marshal_load", time_mload, 1);
#endif
#ifdef FIND_TIME_NUMGUESS
rb_define_virtual_variable("$find_time_numguess", find_time_numguess_getter, NULL);
#endif
}