Monday, May 05, 2008

perl 调用 localtime 的时区问题

测试用例:
{
local $ENV{TZ} = 'UTC+0';
print scalar localtime, "\n";
}
print scalar localtime, "\n";

@CentOS 4.2
Mon Sep 24 01:05:52 2007
Mon Sep 24 01:05:52 2007

@CentOS 4.4
Mon Sep 24 01:05:52 2007
Mon Sep 24 09:05:52 2007

Bug 的详情可以参考
http://rt.perl.org/rt3/Public/Bug/Display.html?id=26136
摘要:
* threaded perl calls localtime_r(3) instead of localtime(3)
* localtime(3) calls tzset(3), but localtime_r(3) does not

一些时间类的模块受此影响,比如 Date::Simple。

CentOS 4.4/ Red Hat Enterprise Linux 4 Update 4 已解决此问题,详情见 perl 的 changelog:
$ rpm -q --changelog perl
* Thu May 11 2006 Jason Vas Dias <jvdias@redhat.com> - 3:5.8.5-30.RHEL4

- fix errata testing issues:
o bug 161305 localtime_r_needs_tzset incorrectly not set on 64-bit arches
o generate perl headers correctly to avoid missing / unresolved headers

* Thu Apr 06 2006 Jason Vas Dias <jvdias@redhat.com> - 3:5.8.5-28.RHEL4

- fix bug 161305: call tzset() before localtime_r

而 perl 的 community patch 应用于 perl 5.8.8 发布之后,此后发布的新版本目前只有 perl 5.10.0。

综上,有以下数种对应解决办法:
1、如果 OS 为 CentOS 4.4 以下,使用 yum 直接升级 perl 即可。
2、其他 OS 可以查找是否有相应的 vendor patch。
3、自行编译安装禁用线程(-Uusethreads)的 perl。
4、自行编译安装 perl 5.10.0。
5、给受影响的模块打补丁,Date::Simple 的补丁这里有。

相关链接:
小心时区陷阱(一)
小心时区陷阱(二)

Sunday, May 04, 2008

Apacheのログに携帯のユーザIDを記録する

iモードIDの登場に伴って、三キャリアのユーザIDはすべてHTTPリクエストヘッダから取れるようになった。Apacheの場合、下記ような設定でユーザIDはログに記録される。
SetEnvIf X-DCMGUID    ^(.+)$ User-ID=$1
SetEnvIf X-UP-SUBNO ^(.+)$ User-ID=$1
SetEnvIf X-JPHONE-UID ^(.+)$ User-ID=$1
CustomLog "logs/access_log" "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{User-ID}e\""