北斗整周数,周内秒,约化儒略日,UTC,互相转化用结构体C++,.h.cpp
时间: 2024-05-01 21:23:18 浏览: 106
以下是一个简单的示例代码,用于将北斗整周数、周内秒转换为约化儒略日和UTC时间,并且可以进行相应的互相转化。需要注意的是,这里只考虑了不考虑闰秒的情况。
```c++
#include <iostream>
#include <cmath>
using namespace std;
// 定义结构体
struct Time {
int year;
int month;
int day;
int hour;
int minute;
double second;
};
// 声明函数
double getJulianDay(int year, int month, int day, int hour, int minute, double second);
Time getUTCFromJD(double jd);
double getJDFromUTC(Time utc);
double getJDFromBD(int week, double seconds);
Time getUTCFromBD(int week, double seconds);
int main() {
// 示例代码
int week = 2125;
double seconds = 234567.89;
double jd = getJDFromBD(week, seconds);
Time utc = getUTCFromBD(week, seconds);
cout << "北斗整周数:" << week << endl;
cout << "周内秒数:" << seconds << endl;
cout << "约化儒略日:" << jd << endl;
cout << "UTC时间:" << utc.year << "-" << utc.month << "-" << utc.day << " " << utc.hour << ":" << utc.minute << ":" << utc.second << endl;
return 0;
}
// 计算儒略日
double getJulianDay(int year, int month, int day, int hour, int minute, double second) {
// 由于儒略日是从 4713 年 1 月 1 日 12 时开始计算的,因此需要先将时间转换为以 4713 年为基准的年月日时分秒
int Y = year;
int M = month;
if (M <= 2) {
Y--;
M += 12;
}
int D = day;
double H = hour + minute / 60.0 + second / 3600.0;
double JD = floor(365.25 * Y) + floor(Y / 400.0) - floor(Y / 100.0) + floor(30.59 * (M - 2)) + D + 1721088.5 + H / 24.0;
return JD;
}
// 将儒略日转换为UTC时间
Time getUTCFromJD(double jd) {
double JD = jd + 0.5;
int Z = floor(JD);
double F = JD - Z;
int A, B, C, D, E, day, month, year, hour, minute;
if (Z < 2299161) {
A = Z;
} else {
int alpha = floor((Z - 1867216.25) / 36524.25);
A = Z + 1 + alpha - floor(alpha / 4.0);
}
B = A + 1524;
C = floor((B - 122.1) / 365.25);
D = floor(365.25 * C);
E = floor((B - D) / 30.6001);
day = B - D - floor(30.6001 * E) + F;
if (E < 14) {
month = E - 1;
} else {
month = E - 13;
}
if (month > 2) {
year = C - 4716;
} else {
year = C - 4715;
}
double H = (F - floor(F)) * 24.0;
hour = static_cast<int>(H);
double M = (H - hour) * 60.0;
minute = static_cast<int>(M);
double S = (M - minute) * 60.0;
Time utc = {year, month, day, hour, minute, S};
return utc;
}
// 将UTC时间转换为儒略日
double getJDFromUTC(Time utc) {
double Y = utc.year;
double M = utc.month;
double D = utc.day;
double H = utc.hour + utc.minute / 60.0 + utc.second / 3600.0;
double JD = 367 * Y - floor(7 * (Y + floor((M + 9) / 12.0)) / 4.0) + floor(275 * M / 9.0) + D + 1721013.5 + H / 24.0;
return JD;
}
// 将北斗整周数和周内秒数转换为儒略日
double getJDFromBD(int week, double seconds) {
double JD = 2444244.5 + week * 7.0 + seconds / 86400.0;
return JD;
}
// 将儒略日转换为北斗整周数和周内秒数
Time getUTCFromBD(int week, double seconds) {
double JD = getJDFromBD(week, seconds);
Time utc = getUTCFromJD(JD);
return utc;
}
```
其中,`getJulianDay()` 函数用于计算儒略日,`getUTCFromJD()` 函数用于将儒略日转换为UTC时间,`getJDFromUTC()` 函数用于将UTC时间转换为儒略日,`getJDFromBD()` 函数用于将北斗整周数和周内秒数转换为儒略日,`getUTCFromBD()` 函数用于将儒略日转换为北斗整周数和周内秒数。
需要注意的是,上述代码只考虑了不考虑闰秒的情况。如果需要考虑闰秒,还需要进行相应的调整。
阅读全文