1. 程式人生 > >C#程式設計練習(03):北斗時間系統、GPS時間系統及其與UTC時間系統之間的轉換

C#程式設計練習(03):北斗時間系統、GPS時間系統及其與UTC時間系統之間的轉換

需求說明:北斗周-周內秒轉化為日曆時,轉化為UTC時,轉化為GPS週週內秒

GPS周-周內秒轉化為日曆時,轉化為UTC時,轉化為北斗周-周內秒

設計示意圖:

原始碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//這是一個BDS/GPS周、周內秒與日曆時、UTC時的轉換與逆轉換程式
//自定義縮略語說明:WIS為周內秒,week inner second; NYR,年月日的拼音首字母縮寫
//本程式主要實現以下幾個功能:
//(1) GPS Week WIS  <-->  NYR
//(2) GPS Week WIS  <-->  UTC
//(3) BDS Week WIS  <-->  NYR
//(4) BDS Week WIS  <-->  UTC
//(5) BDS Week WIS  <--> GPS Week WIS 
namespace BDS_GPS_UTC
{
    class Program
    {
        ///////////////////功能模組一:GPS周-周內秒與日曆時的轉換與逆轉換///////////
        //GPS周-周內秒到年月日系統的轉換
        static private DateTime gpsWeekWIS2NYR(int gpsWeek, int gpsWIS)
        {
            int difFromBegin = gpsWeek * 604800 + gpsWIS;
            DateTime gpsBeginTime = new DateTime(1980,1,6,0,0,0);
            return gpsBeginTime.AddSeconds(difFromBegin);
        }

        //GPS日曆時與周-周內秒的轉換
        static private int[] gpsNYR2WeekWIS(DateTime gpsNYR)
        {
            int[] gpsWeekWIS = {0, 0};               
            DateTime gpsBeginUTC = new DateTime(1980,1,6,0,0,0);
            //計算兩個UTC時的時間間隔
            TimeSpan interval = gpsNYR - gpsBeginUTC;
            int gpsWeek = (int)interval.TotalSeconds / 604800;
            int gpsWIS = (int)interval.TotalSeconds % 604800;

            gpsWeekWIS[0] = gpsWeek;
            gpsWeekWIS[1] = gpsWIS;
            return gpsWeekWIS;
        }

        ///////////////////功能模組二:GPS周-周內秒與UTC時間系統的轉換與逆轉換///////////
        //GPS周-周內秒到UTC時間系統的轉換
        static private DateTime gpsWeekWIS2UTC(int gpsWeek, int gpsWIS)
        {
            DateTime gpsNYR = gpsWeekWIS2NYR(gpsWeek, gpsWIS);
            //GPS日曆時比UTC時快18秒
            return gpsNYR.AddSeconds(-18.0);
        }
        //GPS UTC時間系統到周-周內秒的轉換
        static private int[] gpsUTC2WeekWIS(DateTime gpsUTC)
        {
            return gpsNYR2WeekWIS(gpsUTC.AddSeconds(18));
        }

        ///////////////////功能模組三:BDS周-周內秒與日曆時的轉換與逆轉換///////////
        //BDS周-周內秒到年月日時間系統的轉換
        static private DateTime bdsWeekWIS2NYR(int bdsWeek, int bdsWIS)
        {
            int difFromBegin = bdsWeek * 604800 + bdsWIS;
            DateTime bdsBeginTime = new DateTime(2006, 1, 1, 0, 0, 0);
            return bdsBeginTime.AddSeconds(difFromBegin);
        }
        //年月日時間系統到BDS周-周內秒的轉換
        static private int[] bdsNYR2WeekWIS(DateTime bdsNYR)
        {
            //先轉換到UTC時間系統
            DateTime bdsBeginUTC = new DateTime(2006, 1, 1, 0, 0, 0);
            
            //計算當前UTC回推出BDS起始時刻UTC的時間差
            TimeSpan interval = bdsNYR - bdsBeginUTC;

            int[] bdsWeekWIS = {0,0};
            int bdsWeek = (int)interval.TotalSeconds / 604800;
            int bdsWIS = (int)interval.TotalSeconds % 604800;
            bdsWeekWIS[0] = bdsWeek;
            bdsWeekWIS[1] = bdsWIS;
            return bdsWeekWIS;
        }

        ///////////////////功能模組四:BDS周-周內秒與UTC時間系統的轉換與逆轉換///////////
        //BDS周-周內秒到UTC時間系統的轉換
        static private DateTime bdsWeekWIS2UTC(int bdsWeek, int bdsWIS)
        {
            DateTime bdsNYR = bdsWeekWIS2NYR(bdsWeek, bdsWIS);
            //BDS日曆時比UTC時快4秒
            return bdsNYR.AddSeconds(-4.0);
        }

        //UTC時間系統到BDS周-周內秒的轉換
        static private int[] bdsUTC2WeekWIS(DateTime bdsUTC)
        {                   
            return bdsNYR2WeekWIS(bdsUTC.AddSeconds(4));
        }

        ///////////////////功能模組五:GPS周-周內秒與BDS周-周內秒轉換與逆轉換///////////
        //GPS周-周內秒向BDS周-週週內秒的轉換
        static private int[] gpsWeekWIS2bdsWeekWIS(int gpsWeek, int gpsWIS)
        {
            int[] bdsWeekWIS = {0,0};
            int difFromBegin = gpsWeek * 604800 + gpsWIS - 1356 * 604800 - 14;
            int bdsWeek = difFromBegin / 604800;
            int bdsWIS = difFromBegin % 604800;
            bdsWeekWIS[0] = bdsWeek;
            bdsWeekWIS[1] = bdsWIS;
            return bdsWeekWIS;
        }
        //BDS周-周內秒到GPS周-周內秒的轉換
        static private int[] bdsWeekWIS2gpsWeekWIS(int bdsWeek, int bdsWIS)
        {
            int[] gpsWeekWIS = {0,0};

            int secDifGPS2BDS = bdsWeek * 604800 + bdsWIS + 1356 * 604800 + 14;
            int gpsWeek = secDifGPS2BDS / 604800;
            int gpsWIS = secDifGPS2BDS % 604800;

            gpsWeekWIS[0] = gpsWeek;
            gpsWeekWIS[1] = gpsWIS;
            return gpsWeekWIS;
        }

        static void Main(string[] args)
        {
            int[] gpsWeekWIS = { 2023, 432000};
            Console.Write("0 GPS周-周內秒:");
            Console.WriteLine("GPS周:{0:D},周內秒:{1:D}", gpsWeekWIS[0], gpsWeekWIS[1]);

            Console.Write("1.1 GPS周-周內秒對應日曆時:");
            Console.WriteLine(gpsWeekWIS2NYR(gpsWeekWIS[0], gpsWeekWIS[1]));

            Console.Write("2.1 GPS周-周內秒對應UTC時:");
            Console.WriteLine(gpsWeekWIS2UTC(gpsWeekWIS[0], gpsWeekWIS[1]));


            Console.Write("5.1 GPS周-周內秒轉BDS周-周內秒:");
            int[] bdsWeekWIS = gpsWeekWIS2bdsWeekWIS(gpsWeekWIS[0], gpsWeekWIS[1]);

            Console.WriteLine("北斗周:{0:D},周內秒: {1:D}", bdsWeekWIS[0], bdsWeekWIS[1]);
            Console.Write("3.1 BDS周-周內秒對應日曆時:");
            Console.WriteLine(bdsWeekWIS2NYR(bdsWeekWIS[0], bdsWeekWIS[1]));

            Console.Write("4.1 BDS周-周內秒對應UTC時:");
            Console.WriteLine(bdsWeekWIS2UTC(bdsWeekWIS[0], bdsWeekWIS[1]));

            Console.Write("5.2 BDS周-周內秒轉GPS周-周內秒:");
            int[] gpsWeekWISFromBDS = bdsWeekWIS2gpsWeekWIS(bdsWeekWIS[0], bdsWeekWIS[1]);
            Console.WriteLine("GPS周:{0:D},周內秒:{1:D}", gpsWeekWISFromBDS[0], gpsWeekWISFromBDS[1]);

            //日曆時轉GPS周-周內秒
            Console.Write("1.2 GPS年月日對應周-周內秒:");
            DateTime gpsNYR = new DateTime(2018, 10, 19, 0, 0, 0);
            int[] gpsWeekWISFromGpsNYR = gpsNYR2WeekWIS(gpsNYR);
            Console.WriteLine("GPS周:{0:D},周內秒:{1:D}", gpsWeekWISFromGpsNYR[0], gpsWeekWISFromGpsNYR[1]);

            //UTC時間系統轉GPS周-周內秒
            Console.Write("2.2 UTC對應GPS周-周內秒:");
            DateTime gpsUTC = new DateTime(2018, 10, 18, 23, 59, 42);
            int[] gpsWeekWISFromGpsUTC = gpsUTC2WeekWIS(gpsUTC);
            Console.WriteLine("GPS周:{0:D},周內秒:{1:D}", gpsWeekWISFromGpsUTC[0], gpsWeekWISFromGpsUTC[1]);

            //日曆時轉BDS周-周內秒
            Console.Write("3.2 BDS年月日對應周-周內秒:");
            DateTime bdsNYR = new DateTime(2018, 10, 18, 23, 59, 46);
            int[] bdsWeekWISFrombdsNYR = bdsNYR2WeekWIS(bdsNYR);
            Console.WriteLine("bds周:{0:D},周內秒:{1:D}", bdsWeekWISFrombdsNYR[0], bdsWeekWISFrombdsNYR[1]);


            Console.Write("4.2 UTC對應bds周-周內秒:");
            DateTime bdsUTC = new DateTime(2018, 10, 18, 23, 59, 42);
            int[] bdsWeekWISFrombdsUTC = bdsUTC2WeekWIS(bdsUTC);
            Console.WriteLine("bds周:{0:D},周內秒:{1:D}", bdsWeekWISFrombdsUTC[0], bdsWeekWISFrombdsUTC[1]);
        }
    }
}

算例演示: