本文主要是介绍Linux时间戳和标准时间的互转【转】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在LINUX系统中,有许多场合都使用时间戳的方式表示时间,即从1970年1月1日起至当前的天数或秒数。如/etc/shadow里的密码更改日期和失效日期,还有代理服务器的访问日志对访问时间的记录等等。 下面介绍几种时间戳格式和标准时间格式转换的方法: 1、分别以标准格式和时间戳来显示当前时间 [root@365linux ~]# date 2010年 08月 10日 星期二 03:39:21 CST [root@365linux ~]# date +%s 1281382775 2、显示指定时间的时间戳 [root@365linux ~]# date -d "2010-07-20 10:25:30" +%s 1279592730 3、将时间戳转换为标准时间格式 方法1:使用date命令 [root@365linux ~]# date -d "@1279592730" 2010年 07月 20日 星期二 10:25:30 CST [root@365linux ~]# date -d "1970-01-01 utc 1279592730 seconds" 2010年 07月 20日 星期二 10:25:30 CST [root@365linux ~]# date -d "1970-01-01 14781 days" "+%Y/%m/%d %H:%M:%S" 2010/06/21 00:00:00 [root@localhost tmp]# date -d "@1279592730" Tue Jul 20 10:25:30 CST 2010 [root@localhost tmp]# date -d "@1279592730" +"%Y%m%d %H:%M:%S" 20100720 10:25:30 [root@localhost tmp]# date -d "@1279592730" +"%F %H:%M:%S" 2010-07-20 10:25:30 [root@localhost tmp]# date -d "1970-01-01 utc 1279592730 seconds" Tue Jul 20 10:25:30 CST 2010 [root@localhost tmp]# date -d "1970-01-01 utc 1279592730 seconds" +"%F %H:%M:%S"
2010-07-20 10:25:30 方法2:使用awk里的时间函数 [root@365linux ~]# echo "1279592730" |awk '{print strftime ("%F %T",$0)}' 2010-07-20 10:25:30 方法3:使用perl处理 [root@365linux ~]# perl -e 'print localtime(1279592730)."\n";' Tue Jul 20 10:25:30 2010 补充: 关于时间格式的解释 UTC (Universal Time Coordinated,UTC)世界协调时间 CST (China Standard Time UTC+8:00)中国沿海时间(北京时间) GMT (Greenwich Mean Time)格林威治标准时间: 系统时区设置: [root@365linux ~]# vim /etc/sysconfig/clock
ZONE="Asia/Shanghai" UTC=true ARC=false [root@365linux ~]# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime --------------------------------------------------------------------------------------------------------------------------------------------------------------------------
转http://hi.baidu.com/taolizao/blog/item/2d6f9a1ba50ef3eae0fe0ba9.html |
这篇关于Linux时间戳和标准时间的互转【转】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!