Linux時間戳記和標準時間的互轉【轉】__Linux

來源:互聯網
上載者:User

在LINUX系統中,有許多場合都使用時間戳的方式表示時間,即從1970年1月1日起至當前的天數或秒數。如/etc/shadow裡的密碼更改日期和失效日期,還有Proxy 伺服器的訪問日誌對訪問時間的記錄等等。 下面介紹幾種時間戳記格式和標準時間格式轉換的方法: 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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 oracle 中將unix/linux時間戳記進行轉換 unix/linux時間戳記是按照從格林威治時間1970年1月1日期計算的一個秒數。 unix/linux時間戳記轉換為標準時間格式(主要是注意時區問題): select TO_DATE('19700101','yyyymmdd') + 1235728935/86400 +TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))/24 from dual 其中1235728935就是unix/linux時間戳記,轉換完之後就表示為 2009-2-27 18:02:15。 反過來也一樣,還是要考慮時區: select (to_date('2009-2-27 18:02:15','yyyy-mm-dd hh24:mi:ss') - to_date('1970-1-1','yyyy-mm-dd'))*86400- TO_NUMBER(SUBSTR(TZ_OFFSET(sessiontimezone),1,3))*3600 from dual 
 
 
轉http://hi.baidu.com/taolizao/blog/item/2d6f9a1ba50ef3eae0fe0ba9.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.