linux标准时间和时间戳的转换

来源:互联网 发布:局域网内部监控软件 编辑:程序博客网 时间:2024/05/16 15:43

在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)格林威治标准时间:

原创粉丝点击