GPS时与格里高利历转换

格里高利历就是我们平时用的年月日的东西。

unit gpstime;

interface

uses
  SysUtils,Math;

procedure GPSToGer(week: integer; seconds: Extended; var year,month,day,dayofweek,hour,minute,second: Integer);
procedure GerToGPS(year,month,day,hour,minute: Integer; seconds: Extended; var weekno: Integer; var gpstime: Extended);

implementation
//GPS时间转换为格林高历(普通的日历)
procedure GPSToGer(week: integer; seconds: Extended; var year,month,day,dayofweek,hour,minute,second: Integer);
var JD: Extended;
    a: Integer;
    b,c,d,e: Integer;
begin
  if(week < 1024) then week := week+1024;
  JD := week*7+seconds/86400+2444244.5;
  a := Trunc(JD+0.5);
  b := a+1537;
  c := Trunc((b-122.1)/365.25);
  d := Trunc(365.25*c);
  e := Trunc((b-d)/30.6001);
  day := Trunc( b-d-Trunc(30.6001*e)+Frac(JD+0.5) );
  month := e-1-12*Trunc(e/14);
  year := c-4715-Trunc((7+month)/10);
  dayofweek := Trunc(JD+0.5) mod 7;        //星斯几,0为一,6为周日
  a := Trunc(seconds) mod 86400;
  hour := a div 3600;
  minute := (a mod 3600) div 60;
  second := a mod 60;
end;
//格林高历(普通的日历)转换为GPS时间
procedure GerToGPS(year,month,day,hour,minute: Integer; seconds: Extended; var weekno: Integer; var gpstime: Extended);
var dayofw, dayofy, yr, ttlday, m: Integer;
const dinmth: array[0..12] of integer=(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
  if (year < 1981) or  (month < 1) or (month > 12) or (day < 1) or (day > 31) then weekno := 0;
  if(month=1) then dayofy := day
  else
  begin
    dayofy := 0;
    for m := 1 to month - 1 do
      begin
        dayofy := dayofy+dinmth[m];
        if(m=2) then
        begin
          if((year mod 4=0) and (year mod 100<>0)) or (year mod 400=0) then
          dayofy := dayofy+1;
        end;
      end;
    dayofy := dayofy+day;
  end;
  ttlday := 360;
  for yr := 1981 to year - 1 do
  begin
    ttlday := ttlday+365;
    if((yr mod 4=0) and (yr mod 100<>0)) or (yr mod 400=0) then
    ttlday := ttlday+1;
  end;
  ttlday := ttlday+dayofy;
  weekno := ttlday div 7;
  dayofw := ttlday-7*weekno;
  gpstime := (hour * 3600 + minute * 60 + seconds + dayofw * 86400);
  if(weekno>1024) then weekno := weekno-1024;
end;

end.

软件截图: