メモ代わり。てきとーに。 いや、ですからてきとーですって。 2年前ぐらいにPythonあたりでメールくれた方、ごめんなさい。メール紛失してしまい無視した形になってしまいました。。。

2008年2月10日日曜日

[Apache][CodeReading] Apache2.2.8コードリーディング10日目

今日もこつこつとリーディング。


今日は
apr_unix_setup_time()/Apache2.2.8周りを読破。

apr_unix_setup_timeでは、struct tmにtm_gmtoffか__tm_gmtoffを保持しないシステム用に
自力でgmtからのオフセット値を計算して保持している部分。
tm_gmtoffを保持しているシステムではコード自体がNOPになって何も処理しない。


APR_DECLARE(void) apr_unix_setup_time(void)
{
#ifdef NO_GMTOFF_IN_STRUCT_TM
/* Precompute the offset from GMT on systems where it's not
in struct tm.

Note: This offset is normalized to be independent of daylight
savings time; if the calculation happens to be done in a
time/place where a daylight savings adjustment is in effect,
the returned offset has the same value that it would have
in the same location if daylight savings were not in effect.
The reason for this is that the returned offset can be
applied to a past or future timestamp in explode_time(),
so the DST adjustment obtained from the current time won't
necessarily be applicable.

mktime() is the inverse of localtime(); so, presumably,
passing in a struct tm made by gmtime() let's us calculate
the true GMT offset. However, there's a catch: if daylight
savings is in effect, gmtime()will set the tm_isdst field
and confuse mktime() into returning a time that's offset
by one hour. In that case, we must adjust the calculated GMT
offset.

*/

struct timeval now;
time_t t1, t2;
struct tm t;

gettimeofday(&now, NULL);
t1 = now.tv_sec;
t2 = 0;

#if APR_HAS_THREADS && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
gmtime_r(&t1, &t);
#else
t = *gmtime(&t1);
#endif
t.tm_isdst = 0; /* we know this GMT time isn't daylight-savings */
t2 = mktime(&t);
server_gmt_offset = (apr_int32_t) difftime(t1, t2);
#endif /* NO_GMTOFF_IN_STRUCT_TM */
}


簡単に書くと、gettimeofdayをtimezoneにNULLを伴ってコールし(マニュアルによるとtimezoneに値を指定するのは時代遅れとのこと)GMT秒を取得。
秒からstruct tmへ変換。
変換結果の夏時間をクリアしてmktimeでローカル時間に変換。
結果の差分をgmt_offsetとしてスタティック変数に保管。

な感じか。
ちなみにDebian-EtchではNOP。


次回はいよいよpoolの初期化部分に入る予定。
きっとものすごくアツイに違いない。



.

0 コメント: