程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 源碼分享:將UTC轉化為包含夏令時的local time(以歐州中部為例)

源碼分享:將UTC轉化為包含夏令時的local time(以歐州中部為例)

編輯:關於C語言

最近做的一個項目,涉及到GPS NEMA數據中的GPRMC的處理。從中提取的UTC需要轉化為更易理解和應用的local time。
直接看源碼:
[java]
//Convert the date, assume this is in Europe 
        int date = Integer.parseInt( lineData.get(9) ); 
        int days = date / 10000; 
        int months = ( date / 100 ) % 100; 
        int years = date % 100+2000; 
        String getDate = "Date: "+days+"/"+months+"/"+years+"\r\n"; 
        System.out.println("\n"+getDate); 
         
        //Convert the time, assume this is in Europe 
        int time = Integer.parseInt( lineData.get(1) ); 
        int hours = time / 10000; 
        int minutes = ( time / 100 ) % 100; 
        int seconds = time % 100; 
        int count= seconds+minutes*60+hours*60*60; 
         
        //setup for converting UTC into local time 
        int Marchday=(31-(((5*years/4)+4)%7)); 
        int Octoday=(31-(((5*years/4)+1)%7)); 
         
        if(     (months>3 && months<10)  
            ||  (months==3 && days>Marchday) 
            ||  (months==10 && days<Octoday) 
            ||  (months==3 && days== Marchday && count>=60*60) 
            ||  (months==10 && days== Octoday && count<=60*60) 
            ) 
            hours=hours+2;    // central Europe summer 
        else 
            hours=hours+1; 
          
        String getTime = "Time: "+hours+":"+minutes+":"+seconds+"\r\n"; 
        System.out.println(getTime); 

代碼中的LineData 是ArrayList, 用來存放GPRMC每一個單元的信息。
下邊是測試結果:
Testing by the example code:
$GPRMC,010001,A,3907.356,N,12102.482,W,000.0,360.0,261008,015.5,E*6F,234,12,123,45
$GPRMC,005959,A,3907.482,N,12102.436,W,000.0,360.0,261008,015.5,E*67,234,12,123,45
$GPRMC,183731,A,3907.482,N,12102.436,W,000.0,360.0,081111,015.5,E*67,234,12,123,45
$GPRMC,183731,A,3907.482,N,12102.436,W,000.0,360.0,291011,015.5,E*67,234,12,123,45
$GPRMC,003731,A,3907.482,N,12102.436,W,000.0,360.0,270311,015.5,E*67,234,12,123,45
$GPRMC,183731,A,3907.482,N,12102.436,W,000.0,360.0,270311,015.5,E*67,234,12,123,45
And the output of the time and date is below:
Date: 26/10/2008


Time: 2:0:1 


Date: 26/10/2008

Time: 2:59:59


Date: 8/11/2011

Time: 19:37:31


Date: 29/10/2011

Time: 20:37:31


Date: 27/3/2011

Time: 1:37:31


Date: 27/3/2011

Time: 20:37:31

The test seems OK.
(以下背景資料引自維基百科)
歐洲夏令時是為了利用季節性的日光而在春季開始提前一個小時的作息方法。此方法在全歐洲除冰島外的所有國家實行(冰島全年實行格林尼治時間)。在歐盟國家和其他一些非歐盟國家中,此方法實行時期從每年三月份的最後一個周日1:00(格林尼治時間)開始,至十月份最後一個周日 1:00(格林尼治時間)結束。
歐洲夏令時於以下日期的格林尼治標准時間1:00開始(時鐘提前一個小時):

2008年3月30日
2009年3月29日
2010年3月28日
2011年3月27日
用於計算歐洲夏令時開始時間的公式為:

3月(31-(5y÷4+4) mod 7)日的1:00(格林尼治標准時間)

(至2099年有效[3])其中y指年份,而對於非負數a,a mod b表示將a和b截尾後,兩數相除所得的余數。

歐洲夏令時於以下日期的格林尼治標准時間1:00結束(時鐘調後一個小時):

2008年10月26日
2009年10月25日
2010年10月31日
2011年10月30日
用於計算歐洲夏令時結束始時間的公式為:

10月(31-(5y÷4+1) mod 7)日的1:00(格林尼治標准時間)

(至2099年有效)

以上兩個方程對今天(05月20日)給出的夏令時偏移量為1 (1表示目前實施夏令時)。[4]

 

 

摘自 崔立學的博客專欄

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved