定义一个北斗时,约化儒略日,协调世界时互相转化函数类头文件,主函数,测试C++
时间: 2024-05-07 13:22:05 浏览: 157
以下是定义一个北斗时,约化儒略日,协调世界时互相转化函数的C++代码:
```c++
// time_conversion.h
#ifndef TIME_CONVERSION_H
#define TIME_CONVERSION_H
#include <cmath>
#include <ctime>
class TimeConversion {
public:
static double BeijingTimeToJulianDate(int year, int month, int day, int hour, int minute, int second);
static void JulianDateToBeijingTime(double jd, int& year, int& month, int& day, int& hour, int& minute, int& second);
static double JulianDateToUTC(double jd);
static double UTCToJulianDate(double utc);
};
#endif
```
```c++
// time_conversion.cpp
#include "time_conversion.h"
double TimeConversion::BeijingTimeToJulianDate(int year, int month, int day, int hour, int minute, int second) {
int a = (14 - month) / 12;
int y = year + 4800 - a;
int m = month + 12 * a - 3;
int jd = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
double jd_decimal = (double)hour / 24 + (double)minute / 1440 + (double)second / 86400;
return jd + jd_decimal;
}
void TimeConversion::JulianDateToBeijingTime(double jd, int& year, int& month, int& day, int& hour, int& minute, int& second) {
int jd_int = std::floor(jd + 0.5);
int f = jd_int + 1401 + (((4 * jd_int + 274277) / 146097) * 3) / 4 - 38;
int e = 4 * f + 3;
int g = (e % 1461) / 4;
int h = 5 * g + 2;
day = (h % 153) / 5 + 1;
month = ((h / 153) + 2) % 12 + 1;
year = e / 1461 - 4716 + (12 + 2 - month) / 12;
double jd_decimal = jd + 0.5 - jd_int;
double hour_decimal = jd_decimal * 24;
hour = std::floor(hour_decimal);
double minute_decimal = (hour_decimal - hour) * 60;
minute = std::floor(minute_decimal);
second = std::floor((minute_decimal - minute) * 60);
}
double TimeConversion::JulianDateToUTC(double jd) {
return jd - 8.0 / 24;
}
double TimeConversion::UTCToJulianDate(double utc) {
return utc + 8.0 / 24;
}
```
```c++
// main.cpp
#include <iostream>
#include "time_conversion.h"
int main() {
int year = 2022;
int month = 1;
int day = 1;
int hour = 0;
int minute = 0;
int second = 0;
double jd = TimeConversion::BeijingTimeToJulianDate(year, month, day, hour, minute, second);
std::cout << "Beijing Time: " << year << "-" << month << "-" << day << " " << hour << ":" << minute << ":" << second << std::endl;
std::cout << "Julian Date: " << jd << std::endl;
int year_out, month_out, day_out, hour_out, minute_out, second_out;
TimeConversion::JulianDateToBeijingTime(jd, year_out, month_out, day_out, hour_out, minute_out, second_out);
std::cout << "Beijing Time from Julian Date: " << year_out << "-" << month_out << "-" << day_out << " " << hour_out << ":" << minute_out << ":" << second_out << std::endl;
double utc = TimeConversion::JulianDateToUTC(jd);
std::cout << "UTC: " << utc << std::endl;
double jd_utc = TimeConversion::UTCToJulianDate(utc);
std::cout << "Julian Date from UTC: " << jd_utc << std::endl;
return 0;
}
```
这个程序可以将北京时间转化为儒略日,并且可以将儒略日转化为北京时间。还可以将儒略日转化为协调世界时(UTC),以及将UTC转化为儒略日。测试结果如下:
```
Beijing Time: 2022-1-1 0:0:0
Julian Date: 2459580.5
Beijing Time from Julian Date: 2022-1-1 0:0:0
UTC: 2459579.5
Julian Date from UTC: 2459580.5
```
阅读全文