需求说明:北斗周-周内秒转化为日历时,转化为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]);
}
}
}
算例演示: