北斗周,周内秒,约化儒略日,协调世界时两两互相转换C++代码,用结构体,.h.cpp
时间: 2024-05-15 13:14:28 浏览: 155
以下是将北斗周转化为约化儒略日的C++代码:
```cpp
#include <iostream>
#include <cmath>
struct Date {
int year;
int month;
int day;
int hour;
int minute;
double second;
};
double getJulianDay(int year, int month, int day, double hour, double minute, double second) {
double a = floor((14 - month) / 12);
double y = year + 4800 - a;
double m = month + 12 * a - 3;
double jdn = day + floor((153 * m + 2) / 5) + 365 * y + floor(y / 4) - floor(y / 100) + floor(y / 400) - 32045;
jdn += (hour - 12) / 24.0 + minute / 1440.0 + second / 86400.0;
return jdn;
}
double getReducedJulianDay(int year, int month, int day, double hour, double minute, double second) {
double jd = getJulianDay(year, month, day, hour, minute, second);
return jd - 2400000.5;
}
double getReducedJulianDayFromWeek(int week, double seconds) {
double days = week * 7 + seconds / 86400.0;
return days + 2444244.5;
}
int main() {
int week = 2128;
double seconds = 345600.0;
double rjd = getReducedJulianDayFromWeek(week, seconds);
std::cout << "Reduced Julian Day: " << rjd << std::endl;
Date date = {0};
double jd = rjd + 2400000.5;
double z = floor(jd + 0.5);
double f = jd + 0.5 - z;
double a = z;
if (z >= 2299161) {
double alpha = floor((z - 1867216.25) / 36524.25);
a += 1 + alpha - floor(alpha / 4);
}
double b = a + 1524;
double c = floor((b - 122.1) / 365.25);
double d = floor(365.25 * c);
double e = floor((b - d) / 30.6001);
date.day = b - d - floor(30.6001 * e) + f;
date.month = e - 1;
if (e > 13) {
date.month -= 12;
}
date.year = c - 4715;
if (date.month > 2) {
date.year -= 1;
}
date.hour = floor((jd + 0.5 - z) * 24);
date.minute = floor(((jd + 0.5 - z) * 24 - date.hour) * 60);
date.second = (((jd + 0.5 - z) * 24 - date.hour) * 60 - date.minute) * 60;
std::cout << "Year: " << date.year << ", Month: " << date.month << ", Day: " << date.day << std::endl;
std::cout << "Hour: " << date.hour << ", Minute: " << date.minute << ", Second: " << date.second << std::endl;
return 0;
}
```
以下是将约化儒略日转化为协调世界时的C++代码:
```cpp
#include <iostream>
#include <cmath>
struct Date {
int year;
int month;
int day;
int hour;
int minute;
double second;
};
double getJulianDay(int year, int month, int day, double hour, double minute, double second) {
double a = floor((14 - month) / 12);
double y = year + 4800 - a;
double m = month + 12 * a - 3;
double jdn = day + floor((153 * m + 2) / 5) + 365 * y + floor(y / 4) - floor(y / 100) + floor(y / 400) - 32045;
jdn += (hour - 12) / 24.0 + minute / 1440.0 + second / 86400.0;
return jdn;
}
double getReducedJulianDay(int year, int month, int day, double hour, double minute, double second) {
double jd = getJulianDay(year, month, day, hour, minute, second);
return jd - 2400000.5;
}
double getUtcFromReducedJulianDay(double rjd) {
double jd = rjd + 2400000.5;
double t = (jd - 2451545.0) / 36525.0;
double tu = (jd - 2451545.0) / 365.25;
double s = 24110.5 + 8640184.812999999 * t + 0.093104 * pow(t, 2) - 6.2e-6 * pow(t, 3);
s += 86400.0 * (tu - floor(tu));
return s;
}
int main() {
double rjd = 58754.625;
double utc = getUtcFromReducedJulianDay(rjd);
std::cout << "UTC: " << utc / 3600.0 << " hours" << std::endl;
Date date = {0};
double jd = rjd + 2400000.5;
double z = floor(jd + 0.5);
double f = jd + 0.5 - z;
double a = z;
if (z >= 2299161) {
double alpha = floor((z - 1867216.25) / 36524.25);
a += 1 + alpha - floor(alpha / 4);
}
double b = a + 1524;
double c = floor((b - 122.1) / 365.25);
double d = floor(365.25 * c);
double e = floor((b - d) / 30.6001);
date.day = b - d - floor(30.6001 * e) + f;
date.month = e - 1;
if (e > 13) {
date.month -= 12;
}
date.year = c - 4715;
if (date.month > 2) {
date.year -= 1;
}
date.hour = floor((jd + 0.5 - z) * 24);
date.minute = floor(((jd + 0.5 - z) * 24 - date.hour) * 60);
date.second = (((jd + 0.5 - z) * 24 - date.hour) * 60 - date.minute) * 60;
double tai = utc + 32.184;
double tt = tai + 37.0;
double leapSeconds = 37 - (getUtcFromReducedJulianDay(rjd) - getUtcFromReducedJulianDay(2447892.5)) / 86400.0;
double tdt = tt - leapSeconds;
double jdTdt = jd + (tdt + 32.184) / 86400.0;
std::cout << "Year: " << date.year << ", Month: " << date.month << ", Day: " << date.day << std::endl;
std::cout << "Hour: " << date.hour << ", Minute: " << date.minute << ", Second: " << date.second << std::endl;
std::cout << "TT: " << tt / 3600.0 << " hours" << std::endl;
std::cout << "Leap Seconds: " << leapSeconds << " seconds" << std::endl;
std::cout << "TDT: " << tdt / 3600.0 << " hours" << std::endl;
std::cout << "JD(TDT): " << jdTdt << std::endl;
return 0;
}
```
阅读全文