UTC与GPS时间转换-[week, sow]
- utc2gps
- gps2utc
- 测试
- 参考
Ref: Global Positioning System
utc2gps
matlab源码
function res = utc2gps(utc_t, weekStart)
%% parameters
sec_day = 86400;
sec_week = 604800;
leapsec = 18;
% 默认周一为一周的开始
if nargin < 2
weekStart = datenum('1980-01-06 00:00:00'); % 默认周一为一周的开始
weekStart = weekStart * sec_day - leapsec;
end
utc_t_num = datenum(utc_t) * sec_day;
gps_week = floor((utc_t_num - weekStart)/sec_week);
gps_sec = mod((utc_t_num - weekStart), sec_week);
gps_sec = round(gps_sec, 3);
res = [gps_week, gps_sec];
end
gps2utc
matlab源码
function res = gps2utc(week_num, sow)
%% parameters
sec_day = 86400;
sec_week = 604800;
leapsec = 18;
% 默认周一为一周的开始
if nargin < 2
sow = 0;
end
weekStart = datenum('1980-01-06 00:00:00'); % from start time
weekStart = weekStart * sec_day - leapsec;
dnum = (week_num * sec_week + sow + weekStart)/sec_day; % unit: day
res = datetime(dnum,'ConvertFrom','datenum');
end
测试
clear
close all
addpath 'obs_read_lib/'
% 创建一个datetime对象
date = [2024 05 26 00 00 00.200000];
t = datetime(date);
% 转换为SOW
gps_t = utc2gps(t);
date_re = gps2utc(gps_t(:,1),gps_t(:,2));
gps_t_check = utc2gps(date_re);
% 显示结果
disp(gps_t);
参考
GPS 时间转换在线工具