输入一个现在时间,转化为北斗周,周内秒,约化儒略日,协调时C++代码,用结构体,.h.cpp
时间: 2024-05-09 15:15:57 浏览: 16
以下是使用结构体实现的C++代码,包括时间转换函数和主函数:
```
// time.h
#pragma once
struct Time {
int year; // 年份
int month; // 月份
int day; // 日
int hour; // 小时
int minute; // 分钟
int second; // 秒
int millisecond;// 毫秒
};
// time.cpp
#include "time.h"
#include <cmath>
// 判断是否为闰年
bool isLeapYear(int year) {
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
return true;
}
return false;
}
// 计算一年中的天数
int daysOfYear(int year) {
if (isLeapYear(year)) {
return 366;
}
return 365;
}
// 计算一月中的天数
int daysOfMonth(int year, int month) {
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
case 4:
case 6:
case 9:
case 11:
return 30;
case 2:
if (isLeapYear(year)) {
return 29;
}
return 28;
default:
return 0;
}
}
// 计算儒略日
double julianDay(int year, int month, int day, int hour, int minute, int second, int millisecond) {
double a = (14 - month) / 12;
double y = year + 4800 - a;
double m = month + 12 * a - 3;
double JDN = day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y / 400 - 32045;
double JD = JDN + (hour - 12) / 24.0 + minute / 1440.0 + second / 86400.0 + millisecond / 86400000.0;
return JD;
}
// 计算北斗周和周内秒
void weekSecond(double julianDay, int& week, double& second) {
double JD0 = 2444244.5;
double week0 = 1356;
double weekSeconds = 604800;
double deltaJD = julianDay - JD0;
week = (int)floor(deltaJD / 7.0) - (int)floor((week0 - deltaJD / 7.0) / 1024.0);
second = fmod(deltaJD, 7.0) * 86400.0;
}
// 计算协调世界时
double utc2tai(int year, int month, int day, int hour, int minute, int second, int millisecond) {
double JD = julianDay(year, month, day, hour, minute, second, millisecond);
double TT = (JD - 2451545.0) / 36525.0;
double TAI = JD + 32.184 + (0.001657 * pow(TT, 2)) + (0.000024 * pow(TT, 3));
return TAI;
}
int main() {
Time t;
t.year = 2022;
t.month = 1;
t.day = 1;
t.hour = 12;
t.minute = 30;
t.second = 0;
t.millisecond = 0;
double JD = julianDay(t.year, t.month, t.day, t.hour, t.minute, t.second, t.millisecond);
int week;
double second;
weekSecond(JD, week, second);
double TAI = utc2tai(t.year, t.month, t.day, t.hour, t.minute, t.second, t.millisecond);
printf("JD: %.5f\n", JD);
printf("Week: %d\n", week);
printf("Second: %.5f\n", second);
printf("TAI: %.5f\n", TAI);
return 0;
}
```