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> Thu Jul 2 11:16:25 2009 Shugo Maeda <shugo@ruby-lang.org>
* lib/net/imap.rb: added response to Net::IMAP::ResponseError. * lib/net/imap.rb: added response to Net::IMAP::ResponseError.

239
time.c
View file

@ -1860,15 +1860,31 @@ timegm_noleapsecond(struct tm *tm)
DIV(tm_year+299,400))*86400; 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 * static const char *
find_time_t(struct tm *tptr, int utc_p, time_t *tp) find_time_t(struct tm *tptr, int utc_p, time_t *tp)
{ {
time_t guess, guess_lo, guess_hi; time_t guess, guess_lo, guess_hi;
struct tm *tm, tm_lo, tm_hi; struct tm *tm, tm_lo, tm_hi;
int d, have_guess; int d;
int find_dst; int find_dst;
struct tm result; 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)) #define GUESS(p) (utc_p ? gmtime_with_leapsecond(p, &result) : LOCALTIME(p, result))
#endif
find_dst = 0 < tptr->tm_isdst; find_dst = 0 < tptr->tm_isdst;
@ -1918,122 +1934,133 @@ find_time_t(struct tm *tptr, int utc_p, time_t *tp)
if (d == 0) { guess = guess_hi; goto found; } if (d == 0) { guess = guess_hi; goto found; }
tm_hi = *tm; tm_hi = *tm;
have_guess = 0; try_interpolation = 1;
while (guess_lo + 1 < guess_hi) { while (guess_lo + 1 < guess_hi) {
/* there is a gap between guess_lo and guess_hi. */ if (try_interpolation == 1) {
unsigned long range = 0; int a, b;
if (!have_guess) { /* there is a gap between guess_lo and guess_hi. */
int a, b; range = 0;
/* /*
Try precious guess by a linear interpolation at first. Try precious guess by a linear interpolation at first.
`a' and `b' is a coefficient of guess_lo and guess_hi as: `a' and `b' is a coefficient of guess_lo and guess_hi as:
guess = (guess_lo * a + guess_hi * b) / (a + b) guess = (guess_lo * a + guess_hi * b) / (a + b)
However this causes overflow in most cases, following assignment However this causes overflow in most cases, following assignment
is used instead: is used instead:
guess = guess_lo / d * a + (guess_lo % d) * a / d guess = guess_lo / d * a + (guess_lo % d) * a / d
+ guess_hi / d * b + (guess_hi % d) * b / d + guess_hi / d * b + (guess_hi % d) * b / d
where d = a + b where d = a + b
To avoid overflow in this assignment, `d' is restricted to less than To avoid overflow in this assignment, `d' is restricted to less than
sqrt(2**31). By this restriction and other reasons, the guess is sqrt(2**31). By this restriction and other reasons, the guess is
not accurate and some error is expected. `range' approximates not accurate and some error is expected. `range' approximates
the maximum error. the maximum error.
When these parameters are not suitable, i.e. guess is not within When these parameters are not suitable, i.e. guess is not within
guess_lo and guess_hi, simple guess by binary search is used. guess_lo and guess_hi, simple guess by binary search is used.
*/ */
range = 366 * 24 * 60 * 60; range = 366 * 24 * 60 * 60;
a = (tm_hi.tm_year - tptr->tm_year); a = (tm_hi.tm_year - tptr->tm_year);
b = (tptr->tm_year - tm_lo.tm_year); b = (tptr->tm_year - tm_lo.tm_year);
/* 46000 is selected as `some big number less than sqrt(2**31)'. */ /* 46000 is selected as `some big number less than sqrt(2**31)'. */
if (a + b <= 46000 / 12) { if (a + b <= 46000 / 12) {
range = 31 * 24 * 60 * 60; range = 31 * 24 * 60 * 60;
a *= 12; a *= 12;
b *= 12; b *= 12;
a += tm_hi.tm_mon - tptr->tm_mon; a += tm_hi.tm_mon - tptr->tm_mon;
b += tptr->tm_mon - tm_lo.tm_mon; b += tptr->tm_mon - tm_lo.tm_mon;
if (a + b <= 46000 / 31) { if (a + b <= 46000 / 31) {
range = 24 * 60 * 60; range = 24 * 60 * 60;
a *= 31; a *= 31;
b *= 31; b *= 31;
a += tm_hi.tm_mday - tptr->tm_mday; a += tm_hi.tm_mday - tptr->tm_mday;
b += tptr->tm_mday - tm_lo.tm_mday; b += tptr->tm_mday - tm_lo.tm_mday;
if (a + b <= 46000 / 24) { if (a + b <= 46000 / 24) {
range = 60 * 60; range = 60 * 60;
a *= 24; a *= 24;
b *= 24; b *= 24;
a += tm_hi.tm_hour - tptr->tm_hour; a += tm_hi.tm_hour - tptr->tm_hour;
b += tptr->tm_hour - tm_lo.tm_hour; b += tptr->tm_hour - tm_lo.tm_hour;
if (a + b <= 46000 / 60) { if (a + b <= 46000 / 60) {
range = 60; range = 60;
a *= 60; a *= 60;
b *= 60; b *= 60;
a += tm_hi.tm_min - tptr->tm_min; a += tm_hi.tm_min - tptr->tm_min;
b += tptr->tm_min - tm_lo.tm_min; b += tptr->tm_min - tm_lo.tm_min;
if (a + b <= 46000 / 60) { if (a + b <= 46000 / 60) {
range = 1; range = 1;
a *= 60; a *= 60;
b *= 60; b *= 60;
a += tm_hi.tm_sec - tptr->tm_sec; a += tm_hi.tm_sec - tptr->tm_sec;
b += tptr->tm_sec - tm_lo.tm_sec; b += tptr->tm_sec - tm_lo.tm_sec;
} }
} }
} }
} }
} }
if (a <= 0) a = 1; if (a <= 0) a = 1;
if (b <= 0) b = 1; if (b <= 0) b = 1;
d = a + b; d = a + b;
/* /*
Although `/' and `%' may produce unexpected result with negative Although `/' and `%' may produce unexpected result with negative
argument, it doesn't cause serious problem because there is a argument, it doesn't cause serious problem because there is a
fail safe. fail safe.
*/ */
guess = guess_lo / d * a + (guess_lo % d) * a / d guess = guess_lo / d * a + (guess_lo % d) * a / d
+ guess_hi / d * b + (guess_hi % d) * b / 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. */ /* Precious guess is invalid. try binary search. */
guess = guess_lo / 2 + guess_hi / 2; guess = guess_lo / 2 + guess_hi / 2;
if (guess <= guess_lo) if (guess <= guess_lo)
guess = guess_lo + 1; guess = guess_lo + 1;
else if (guess >= guess_hi) else if (guess >= guess_hi)
guess = guess_hi - 1; guess = guess_hi - 1;
range = 0; range = 0;
} try_interpolation = 1;
}
tm = GUESS(&guess); tm = GUESS(&guess);
if (!tm) goto error; if (!tm) goto error;
have_guess = 0;
d = tmcmp(tptr, tm); d = tmcmp(tptr, tm);
if (d < 0) {
guess_hi = guess; if (d < 0) {
tm_hi = *tm; if (range)
if (range) { try_interpolation = 2;
guess = guess - range; else if ((unsigned_time_t)(guess-guess_lo) > (unsigned_time_t)(guess_hi-guess))
range = 0; try_interpolation = 0;
if (guess_lo < guess && guess < guess_hi) else
have_guess = 1; try_interpolation = 1;
} guess_hi = guess;
} tm_hi = *tm;
else if (d > 0) { }
guess_lo = guess; else if (d > 0) {
tm_lo = *tm; if (range)
if (range) { try_interpolation = 3;
guess = guess + range; else if ((unsigned_time_t)(guess-guess_lo) < (unsigned_time_t)(guess_hi-guess))
range = 0; try_interpolation = 0;
if (guess_lo < guess && guess < guess_hi) else
have_guess = 1; try_interpolation = 1;
} guess_lo = guess;
} tm_lo = *tm;
else { }
else {
found: found:
if (!utc_p) { if (!utc_p) {
/* If localtime is nonmonotonic, another result may exist. */ /* If localtime is nonmonotonic, another result may exist. */
@ -3803,4 +3830,8 @@ Init_Time(void)
rb_define_method(rb_cTime, "marshal_dump", time_mdump, 0); rb_define_method(rb_cTime, "marshal_dump", time_mdump, 0);
rb_define_method(rb_cTime, "marshal_load", time_mload, 1); rb_define_method(rb_cTime, "marshal_load", time_mload, 1);
#endif #endif
#ifdef FIND_TIME_NUMGUESS
rb_define_virtual_variable("$find_time_numguess", find_time_numguess_getter, NULL);
#endif
} }