转换时间与日期到GPS时

手上一个项目要用到,查了些资料,写出了其计算算法的代码,记录于此。

C++版:

#include <stdio.h>
#include <math.h>
int dinmth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int  GetGPSTime(int year,int month,int day,int hour,int minute,double second,double *gpstime)
{
  int   dayofw,dayofy, yr, ttlday, m, weekno;
/* 检查时间是否超限*/
  if (year < 1981 || month < 1 || month > 12 || day < 1 || day > 31)
     weekno = 0;
/*  计算为今年中的哪一天 */
  if (month == 1)
     dayofy = day;
  else
  {
    dayofy = 0;
    for (m=1; m<=(month-1); m++)
    {
      dayofy += dinmth[m];
      if ( m==2 )
      {
 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
    dayofy += 1;
      }
    }
    dayofy += day;
  }
/*  计算GPS周与GPS秒 */
  ttlday = 360;
  for (yr=1981; yr<=(year-1); yr++)
  {
    ttlday  += 365;
    if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 ==0)
       ttlday  += 1;
  }
  ttlday += dayofy;
  weekno  = ttlday/7;
  dayofw  = ttlday - 7 * weekno;
  *gpstime =  (hour * 3600 + minute * 60 + second + dayofw * 86400);
  if(weekno>1024){weekno-=1024;}
  return weekno;
}

void main()     //主函数
{   
 int weekno;

double gpstime;

weekno=GetGPSTime(2009, 12, 2, 19, 44, 30, &gpstime);
 printf("%d %f",weekno,gpstime);
 scanf("%d",&weekno);
}

C#版:

     /// <summary>
        /// 将日期和时间转换为GPS时
         /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="hour"></param>
        /// <param name="minute"></param>
        /// <param name="second"></param>
        /// <param name="gpstime"></param>
        /// <param name="weekno"></param>
        public void GetGPSTime(int year, int month, int day, int hour, int minute, double second, ref double gpstime, ref int weekno)
        {
            int dayofw, dayofy, yr, ttlday, m;
            if (year < 1981 || month < 1 || month > 12 || day < 1 || day > 31)
            {
                weekno = 0;
            }
            if (month == 1)
            {
                dayofy = day;
            }
            else
            {
                dayofy = 0;
                for (m = 1; m <= (month - 1); m++)
                {
                    dayofy += dinmth[m];
                    if (m == 2)
                    {
                        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                            dayofy += 1;
                    }
                }
                dayofy += day;
            }
            ttlday = 360;
            for (yr = 1981; yr <= (year - 1); yr++)
            {
                ttlday += 365;
                if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0)
                    ttlday += 1;
            }
            ttlday += dayofy;
            weekno = ttlday / 7;
            dayofw = ttlday - 7 * weekno;
            gpstime = (hour * 3600 + minute * 60 + second + dayofw * 86400);
            if (weekno > 1024)
            {
                weekno -= 1024;
            }
        }

用法:

public partial class Form1 : Form
    {
        YUMA yuma = new YUMA();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DateTime currentTime = new DateTime();
            currentTime = DateTime.Now;
            int weekno=0;
            double gpstime=0;
            yuma.GetGPSTime(currentTime.Year, currentTime.Month, currentTime.Day, currentTime.Hour, currentTime.Minute, currentTime.Second, ref gpstime, ref weekno);
            MessageBox.Show(weekno.ToString() + "  " + gpstime.ToString());
        }
    }