现象归纳
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 00:47:11 CST 2009
jessinio@jessinio-laptop:/tmp$ sudo mv /etc/localtime /tmp/
jessinio@jessinio-laptop:/tmp$ date
Sat Dec 12 16:47:23 UTC 2009
可见, 与这个文件有关。
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 00:52:15 CST 2009
jessinio@jessinio-laptop:/tmp$ cat /etc/timezone
Asia/Shanghai
jessinio@jessinio-laptop:/tmp$ echo 'Australia/South' |sudo tee /etc/timezone
Australia/South
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 00:52:35 CST 2009
看来/etc/timezone文件不管用。
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 00:54:25 CST 2009
jessinio@jessinio-laptop:/tmp$ TZ='Australia/South'; export TZ
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 03:24:44 CST 2009
看来这个环境变量管用。
这是什么意思?
从 http://www.timeanddate.com/worldclock/ 看出, shanghai与San Francisco刚好差16个小时。
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 01:21:25 CST 2009
jessinio@jessinio-laptop:/tmp$ TZ='America/Phoenix'; export TZ
jessinio@jessinio-laptop:/tmp$ date
Sat Dec 12 10:21:28 MST 2009
jessinio@jessinio-laptop:/tmp$ sudo cp -r /usr/share/zoneinfo/America/Phoenix /etc/localtime
jessinio@jessinio-laptop:/tmp$ date
Sat Dec 12 10:23:04 MST 2009
jessinio@jessinio-laptop:/tmp$ TZ='Asia/Shanghai'; export TZ
jessinio@jessinio-laptop:/tmp$ date
Sun Dec 13 01:23:16 CST 2009
只使用一个TZ变量就可以完成时区的配置, TZ变量的存在, /etc/localtime没有一点作用
眼见为实
jessinio@jessinio-laptop:/tmp$ unset TZjessinio@jessinio-laptop:/tmp$ strace date 2>&1|grep 'open'
...
open("/etc/localtime", O_RDONLY) = 3
TZ变量不存在时, /etc/localtime起作用
jessinio@jessinio-laptop:/tmp$ TZ='Asia/Shanghai'; export TZ
jessinio@jessinio-laptop:/tmp$ strace date 2>&1|grep 'open'
...
open("/usr/share/zoneinfo/Asia/Shanghai", O_RDONLY) = 3
TZ变量存在时, /etc/localtime不起作用
CTIME函数
date命令应该也是使用了ctime函数。随便测试一下C代码的ctime函数
jessinio@jessinio-laptop:/tmp$ cat get_time.c
#include <time.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char * retval;
time_t epoch = time(NULL);
retval = ctime( &epoch );
printf("%s\n", retval);
return 0;
}
jessinio@jessinio-laptop:/tmp$ gcc get_time.c
jessinio@jessinio-laptop:/tmp$ strace a.out 2>&1|grep 'open'
jessinio@jessinio-laptop:/tmp$ strace a.out
strace: a.out: command not found
jessinio@jessinio-laptop:/tmp$ strace ./a.out 2>&1|grep 'open'
...
open("/etc/localtime", O_RDONLY) = 3
jessinio@jessinio-laptop:/tmp$ TZ='Asia/Shanghai'; export TZ
jessinio@jessinio-laptop:/tmp$ strace ./a.out 2>&1|grep 'open'
...
open("/usr/share/zoneinfo/Asia/Shanghai", O_RDONLY) = 3
可见, ctime函数本身就是这样实现的。
就算是python也是一样的。
jessinio@jessinio-laptop:/tmp$ cat get_time.py
#/usr/bin/env python
import time
time.ctime()
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.