Chinaunix

标题: split(/\s/, localtime) 有问题? [打印本页]

作者: actan    时间: 2010-01-20 09:31
标题: split(/\s/, localtime) 有问题?
my @today = split(/\s/, localtime);
print Dumper(\@today), "<br />";




$VAR1 = [ 'Tue', 'Jan', '19', '20:24:36', '2010' ];
所以year应该是 $today[4]

但是为什么最近发现有时候是 $today[5]才是year呢

难道是因为 split(/\s/, localtime) 的问题吗?

有没有人遇到过?

直接print localtime的话 结果是这样的: 3624201901102180

这个怎么 split(/\s/, localtime)呢?
作者: iakuf    时间: 2010-01-20 09:40
直接print localtime的话 结果是这样的: 3624201901102180

这个怎么 split(/\s/, localtime)呢?.



这个要搞清楚,在perl中有个上下文,对不同的环境会输出不同的内容
比如

perl -e 'print  localtime'
perl -e 'print scalar localtime'

上面这二条完全不同,下面这台才是你要的.


另外
perl -e '@time = split(/\s/,localtime);print $time[4]'
这台是第5个域名输出年.上面这条输出是2010
作者: actan    时间: 2010-01-20 10:01
多谢!原来是context的问题,刚才查了一下手册,说

In scalar context, localtime() returns the ctime(3) value:

    $now_string = localtime;  # e.g., "Thu Oct 13 04:54:34 1994"

This scalar value is not locale dependent but is a Perl builtin.
好像这个ctime(3)是c里面的函数。
不过他的返回的格式稳定吗---前一段时间我一直是使用 $today[4]来得到年份,后来有几天这个不对了,得不到了,我同事改成了$today[5]好了。这几天$today[5]又不对了,我们发现改成$today[4]又对了。
疯了,难道 split(/\s/,localtime); 返回的字符串 不是永远是 以空格分隔的 五个字符串吗?
难道"Wed Jan 20 09:46:36 2010" 这里面某个时间会变得突然多一个空格出来吗?
作者: actan    时间: 2010-01-20 10:06
Because Perl currently relies on the native standard C localtime() function, it is only safe to use times between 0 and (2**31)-1. Times outside this range may result in unexpected behavior depending on your operating system's implementation of localtime().

between 0 and (2**31)-1是啥意思?
作者: flw    时间: 2010-01-20 10:28
直接在列表上下文调用 localtime,取它的分量就可以了。
作者: actan    时间: 2010-01-20 10:45
多谢版主

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
$mon = qq{0$mon} if (length $mon == 1);
print qq{$year-$mon-$mday $hour:$min:$sec\n};


以后就这样用估计还保险一些

不过这次的问题很诡异啊
作者: iakuf    时间: 2010-01-20 11:00
标题: 回复 #6 actan 的帖子
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
my $CurData = sprintf "%4d%.2d%.2d", $year + 1900 , $mon + 1 , $mday;

这样写会更加优雅.

如果要得到前几天之类,第一行还可以写成
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time()-1*86400)

这个中的-1就是前一天,取前几天都很容易,也很方便

[ 本帖最后由 iakuf 于 2010-1-20 11:02 编辑 ]
作者: flw    时间: 2010-01-20 11:23
原帖由 iakuf 于 2010-1-20 11:00 发表
  1. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  2. my $CurData = sprintf "%4d%.2d%.2d", $year + 1900 , $mon + 1 , $mday;
复制代码


这样写会更加优雅.

一点也不优雅。优雅的写法是 use POSIX 然后用 strftime:

my $foo = strftime '%Y-%m-%d %H:%M:%S', localtime;
作者: flw    时间: 2010-01-20 11:24
原帖由 actan 于 2010-1-20 10:45 发表
多谢版主

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon++;
$mon = qq{0$mon} if (length $mon == 1);
print qq{$year-$mon-$mday $hourminsec\n ...

localtime 的参数如果是当前时间的话,可以省略。
作者: wendaozhe    时间: 2010-01-20 11:48
原帖由 flw 于 2010-1-20 11:23 发表

一点也不优雅。优雅的写法是 use POSIX 然后用 strftime:

my $foo = strftime '%Y-%m-%d %H:%M:%S', localtime;

学习了
作者: actan    时间: 2010-01-20 13:09
多谢,学习了!




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2