免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: actan
打印 上一主题 下一主题

split(/\s/, localtime) 有问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-01-20 09:31 |显示全部楼层 |倒序浏览
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)呢?

论坛徽章:
0
2 [报告]
发表于 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" 这里面某个时间会变得突然多一个空格出来吗?

论坛徽章:
0
3 [报告]
发表于 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是啥意思?

论坛徽章:
0
4 [报告]
发表于 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};


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

不过这次的问题很诡异啊

论坛徽章:
0
5 [报告]
发表于 2010-01-20 13:09 |显示全部楼层
多谢,学习了!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP