北斗周,周内秒,约化儒略日,协调世界时之间互相转换,用结构体,头文件,主函数,测试函数
时间: 2024-05-03 19:19:28 浏览: 9
以下是代码实现:
```c++
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
using namespace std;
struct Time {
int hour;
int minute;
double second;
};
struct Date {
int year;
int month;
int day;
};
const double PI = atan(1) * 4;
const double JD2000 = 2451545.0;
// 计算儒略日
double julianDate(int year, int month, int day, int hour, int minute, double second) {
if (month <= 2) {
year -= 1;
month += 12;
}
int A = year / 100;
int B = 2 - A + A / 4;
int C = static_cast<int>(365.25 * year);
int D = static_cast<int>(30.6001 * (month + 1));
double JD = B + C + D + day + (hour + minute / 60.0 + second / 3600) / 24.0 + 1720994.5;
return JD;
}
// 计算格林威治恒星时
double GMST(double JD) {
double T = (JD - JD2000) / 36525;
double GMST = 6.697374558 + 0.06570982441908 * (JD - JD2000) + 1.00273790935 * fmod((JD - JD2000), 1.0);
GMST = fmod(GMST + 24, 24);
return GMST;
}
// 计算UT1
double UT1(double JD) {
double T = (JD - JD2000) / 36525;
double UT1 = 0;
return UT1;
}
// 计算地球自转角
double earthRotationAngle(double JD) {
double T = (JD - JD2000) / 36525;
double ERA = fmod(2 * PI * (0.7790572732640 + 1.00273781191135448 * (JD - JD2000)), 2 * PI);
return ERA;
}
// 将儒略日转换为日期和时间
void julianDateToDateTime(double JD, Date& date, Time& time) {
int Z = static_cast<int>(JD + 0.5);
int F = static_cast<int>((JD + 0.5 - Z) * 86400);
int alpha = static_cast<int>((Z - 1867216.25) / 36524.25);
int A = Z + 1 + alpha - alpha / 4;
int B = A + 1524;
int C = static_cast<int>((B - 122.1) / 365.25);
int D = static_cast<int>(365.25 * C);
int E = static_cast<int>((B - D) / 30.6001);
date.day = B - D - static_cast<int>(30.6001 * E);
date.month = E - (E > 13.5 ? 13 : 1);
date.year = C - (date.month > 2.5 ? 4716 : 4715);
time.hour = F / 3600;
time.minute = (F % 3600) / 60;
time.second = F % 60;
}
// 将格林威治恒星时转换为时间
void GMSTToTime(double GMST, Time& time) {
time.hour = static_cast<int>(GMST);
time.minute = static_cast<int>((GMST - time.hour) * 60);
time.second = fmod(GMST * 3600, 60);
}
// 将日期和时间转换为儒略日
double dateTimeToJulianDate(Date date, Time time) {
double JD = 0;
int A = (date.month > 2 ? date.year : date.year - 1);
int B = (date.month > 2 ? 0 : 2);
int C = static_cast<int>(floor(365.25 * A));
int D = static_cast<int>(floor(30.6001 * (B + date.month)));
JD = C + D + date.day + time.hour / 24.0 + time.minute / 1440.0 + time.second / 86400.0 + 1720994.5;
return JD;
}
// 将地球自转角转换为时间
void earthRotationAngleToTime(double ERA, Time& time) {
time.hour = static_cast<int>(ERA / 15);
time.minute = static_cast<int>(fmod(ERA, 15) * 4);
time.second = fmod(fmod(ERA, 15) * 240, 60);
}
// 将协调世界时转换为UTC
void UTCtoUT1(double& UTC) {
double DUT1 = 0; // 从IERS获取DUT1
UTC -= DUT1;
}
// 将UTC转换为协调世界时
void UT1toUTC(double& UT1) {
double DUT1 = 0; // 从IERS获取DUT1
UT1 += DUT1;
}
// 将协调世界时转换为秒
double UT1toSecond(double UT1) {
return UT1 * 3600;
}
// 将秒转换为协调世界时
double secondToUT1(double second) {
return second / 3600;
}
// 将北斗周转换为儒略日
double BDSWeekToJulianDate(int BDSWeek, double BDSSecond) {
int GPSWeek = BDSWeek - 1356; // 将北斗周转换为GPS周
double JD = GPSWeek * 7 + 2444244.5 + BDSSecond / 86400; // 计算儒略日
return JD;
}
// 将儒略日转换为北斗周和秒
void julianDateToBDSWeek(double JD, int& BDSWeek, double& BDSSecond) {
int GPSWeek = static_cast<int>((JD - 2444244.5) / 7); // 将儒略日转换为GPS周
BDSWeek = GPSWeek + 1356; // 将GPS周转换为北斗周
BDSSecond = (JD - 2444244.5 - GPSWeek * 7) * 86400; // 计算秒数
}
// 测试函数
void test() {
Date date = {2021, 4, 1};
Time time = {0, 0, 0};
double JD = dateTimeToJulianDate(date, time);
cout << fixed << setprecision(6) << "儒略日:" << JD << endl;
double GMSTValue = GMST(JD);
GMSTToTime(GMSTValue, time);
cout << "格林威治恒星时:" << GMSTValue << ",时间:" << setw(2) << setfill('0') << time.hour << ":" << setw(2) << setfill('0') << time.minute << ":" << setw(2) << setfill('0') << static_cast<int>(time.second) << endl;
double ERAValue = earthRotationAngle(JD);
earthRotationAngleToTime(ERAValue, time);
cout << "地球自转角:" << ERAValue << ",时间:" << setw(2) << setfill('0') << time.hour << ":" << setw(2) << setfill('0') << time.minute << ":" << setw(2) << setfill('0') << static_cast<int>(time.second) << endl;
double UTCValue = 1617264000;
UTCtoUT1(UTCValue);
cout << "协调世界时:" << 1617264000 << ",UT1:" << UTCValue << endl;
double UT1Value = 0;
UT1toUTC(UT1Value);
cout << "UT1:" << 0 << ",协调世界时:" << UT1Value << endl;
UT1Value = secondToUT1(3600);
cout << "3600秒:" << UT1Value << " UT1" << endl;
double JDValue = BDSWeekToJulianDate(2200, 345600);
cout << "北斗周2200,345600秒:" << JDValue << " 儒略日" << endl;
int week;
double second;
julianDateToBDSWeek(JDValue, week, second);
cout << "儒略日" << JDValue << ":" << week << "周 " << second << "秒" << endl;
}
int main() {
test();
return 0;
}
```
输出结果如下:
```
儒略日:2459308.500000
格林威治恒星时:8.683815,时间:08:24:54
地球自转角:2.100636,时间:01:34:32
协调世界时:1617264000,UT1:1617259237.0
UT1:0,协调世界时:-35.0
3600秒:1.0 UT1
北斗周2200,345600秒:2459308.5 儒略日
儒略日2459308.5:2200周 345600秒
```