北斗周,周内秒,约化儒略日,协调世界时之间互相转换C++代码,用结构体,.h.cpp
时间: 2024-05-01 09:23:03 浏览: 20
以下是一个简单的C++代码示例,可用于将北斗周、周内秒、约化儒略日和协调世界时之间进行转换。这个代码使用结构体和头文件/源文件分离的形式。
time.h:
```c++
#ifndef TIME_H
#define TIME_H
struct Time {
int year;
int month;
int day;
int hour;
int minute;
double second;
};
int week_seconds_to_jd(int week, double seconds);
int jd_to_week_seconds(double jd, int& week);
Time jd_to_utc(double jd);
double utc_to_jd(Time t);
#endif
```
time.cpp:
```c++
#include "time.h"
#include <cmath>
const double JD_J2000 = 2451545.0;
const double SECONDS_PER_DAY = 86400.0;
const double SECONDS_PER_WEEK = 604800.0;
int week_seconds_to_jd(int week, double seconds) {
return (int)(JD_J2000 + week * 7.0 + seconds / SECONDS_PER_DAY);
}
int jd_to_week_seconds(double jd, int& week) {
double days = jd - JD_J2000;
week = (int)(days / 7.0);
return (int)(fmod(days, 7.0) * SECONDS_PER_DAY);
}
Time jd_to_utc(double jd) {
int y, m, d;
double seconds;
double jd0 = floor(jd + 0.5);
double c = jd0 + 0.5;
double x = floor((c - 1867216.25) / 36524.25);
double a = c + 1.0 + x - floor(x / 4.0);
double b = a + 1524.0;
double c1 = floor((b - 122.1) / 365.25);
double d1 = floor(365.25 * c1);
double e = floor((b - d1) / 30.6001);
d = (int)(b - d1 - floor(30.6001 * e));
m = (int)(e - (e > 13.5 ? 13.0 : 1.0));
y = (int)(c1 - (m > 2.5 ? 4716.0 : 4715.0));
double frac = jd + 0.5 - jd0;
seconds = frac * SECONDS_PER_DAY;
Time t = { y, m, d, (int)floor(seconds / 3600.0), (int)floor(fmod(seconds, 3600.0) / 60.0), fmod(seconds, 60.0) };
return t;
}
double utc_to_jd(Time t) {
int y = t.year;
int m = t.month;
int d = t.day;
int h = t.hour;
int mi = t.minute;
double s = t.second;
if (m <= 2) {
m += 12;
y--;
}
int a = (int)floor(y / 100.0);
int b = 2 - a + (int)floor(a / 4.0);
double jd = floor(365.25 * (y + 4716.0)) + floor(30.6001 * (m + 1)) + d + b - 1524.5;
double frac = (h + mi / 60.0 + s / 3600.0) / 24.0;
return jd + frac;
}
```
使用示例:
```c++
#include <iostream>
#include "time.h"
int main() {
int week = 2127;
double seconds = 345678.9;
int jd = week_seconds_to_jd(week, seconds);
std::cout << "JD: " << jd << std::endl;
int week2;
int seconds2 = jd_to_week_seconds(jd, week2);
std::cout << "Week: " << week2 << ", Seconds: " << seconds2 << std::endl;
Time t = jd_to_utc(jd);
std::cout << "UTC: " << t.year << "-" << t.month << "-" << t.day << " " << t.hour << ":" << t.minute << ":" << t.second << std::endl;
double jd2 = utc_to_jd(t);
std::cout << "JD: " << jd2 << std::endl;
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)