将公历日期转换为儒略日期,然后再次返回(随时间) [英] Converting Gregorian date to Julian Date and then back again (with time)

查看:94
本文介绍了将公历日期转换为儒略日期,然后再次返回(随时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,该程序必须将当前的格里高利日期和时间转换为儒略日期,然后再返回到格里高利门.最终,我将需要添加能够添加年,月,日,小时,分钟和秒的功能,但是我需要首先解决这一问题.

I'm writing a program that has to convert the current gregorian date and time to a Julian Date and then back to Gregorian gate. Eventually I will need to add the functionality of being able to add years, months, days, hours, minutes and seconds, but I need to get this part out of the way first.

现在我已经从公历日期转换为朱利安日期日期,因此从逻辑上讲,我觉得我应该能够以某种方式将等式逆转,那将是相当简单的.但是,我正在执行两步过程,首先将公历日期转换为儒略日数,然后将其转换为儒略日(区别是天数不包括时间).因此,将其转换回来应该只意味着我必须从方程式中减去小时,分钟和秒,然后将儒略日数单独转换回公历.我想认为这是一个简单的过程,可以将小时,分钟和秒划分和修改3次,通常,我对数学非常了解,并且可以通过逻辑思考这些事情,但是我的大脑根本无法正常工作.

Right now I have the conversion from Gregorian Date to Julian Date, so logically I feel like I should simply be able to reverse the equation somehow and that that would be fairly simple. However I'm doing a two step process where I first convert the Gregorian Date to a Julian Day Number, and then to a Julian Date (difference being day number doesn't include time). So converting it back should just mean that I have to get the hours, minutes and seconds back from the equation, and then do the seperate conversion for Julian Day Number back to Gregorian date. I would like to think it's simple process of dividing and moding 3 times for hours, minutes and seconds, and normally I'm pretty good with math and thinking these things through logically, but my brain is simply not functioning on this one.

jdn_t gregorian_to_jd(year_t year, month_t month, day_t day, hour_t hour, minute_t     minute, second_t second)
{ 
//implement the conversion from gregorian to jdn
long long a = (14 - month)/12;
long long y = year + 4800 - a;
long long m = month + 12*a - 3;

jdn_t jdn = day + (153 * m + 2)/5 + 365*y + y/4 - y/100 + y/400 - 32045 - 0.5;
jdnt_t jdnt = jdn + (hour-12)/24 + minute/1440 + second/86400;
}

void jdn_to_gregorianTime(jdnt_t jdnt,year_t & year, month_t & month, day_t & day,   hour_t & hour, minute_t & minute, second_t & second)
{
    long long j = static_cast<long long>(jdnt + 0.5) + 32044;
    long long g = j / 146097;
    long long dg = j % 146097;
    long long c = (dg / 36524 + 1) * 3 / 4;
    long long dc = dg - c * 36524;
    long long b = dc / 1461;
    long long db = dc % 1461;
    long long a = (db / 365 + 1) *3 / 4;
    long long da = db - a * 365;
    long long y = g * 400 + c * 100 + b * 4 + a;
    long long m = (da * 5 + 308) / 153 - 2;
    long long d = da - (m+4) * 153 / 5 + 122;
    year = y - 4800 + (m + 2) / 12;
    month = (m + 2) % 12 + 1;
    day = static_cast<day_t>(d + 1);

下半部分是计算出小时,分钟和秒后将需要的计算.他们所要做的就是将儒略日数放回公历.

The bottom half there are the calculations I'll need once I've been able to get out my hours, minutes and seconds. All they do Is put the Julian Day Number back to Gregorian Date.

维基页面为那些不懂农法的人解释了整个儒略日的事情: http://en.wikipedia.org/wiki/Julian_day

The wiki page explains the whole julian date thing for those who aren't farmiliar: http://en.wikipedia.org/wiki/Julian_day

我希望我已经解释了我足够需要的东西!感谢您提供的任何帮助!

I hope I've explained what I need well enough! Thanks for any help you guys can offer!

推荐答案

免费,开源C ++ 11/14日期/时间库使用< chrono> 基础,通过设置 all 的转换来促进 any 两个日历之间的转换 Unix时间的日历.

This free, open source C++11/14 date/time library uses the <chrono> foundation to facilitate conversions between any two calendars by setting up conversions from all calendars to and from Unix Time.

它恰好具有儒略历和格里高利历的两个变体({年,月,日}和{年,月,周日,索引}),基于ISO周的日历和一个(不完美的)日历.)伊斯兰日历.日历相对容易添加,添加后,日历可以与所有其他日历以及< chrono> system_clock :: time_point 互操作.

It happens to have a Julian calendar as well as two variants of the Gregorian calendar ({year, month, day} and {year, month, weekday, index}), the ISO week-based calendar, and an (imperfect) Islamic calendar. Calendars are relatively easily added, and once added a calendar is interoperable with all other calendars, and <chrono>'s system_clock::time_point at any precision.

示例代码:

#include "date.h"
#include "julian.h"
#include <iostream>

int
main()
{
    using namespace date::literals;
    auto ymd = 2016_y/oct/11;
    auto jymd = julian::year_month_day{ymd};
    auto ymd2 = date::year_month_day{jymd};
    std::cout << ymd << '\n';
    std::cout << jymd << '\n';
    auto ymd2 = date::year_month_weekday{jymd};
}

输出:

2016-10-11
2016-09-28
2016/Oct/Tue[2]

如果您想了解有关基础算法的更多详细信息,请在此处进行讨论(并证明):

If you would like more details about the underlying algorithms, they are discussed (and proven) here:

http://howardhinnant.github.io/date_algorithms.html

这篇关于将公历日期转换为儒略日期,然后再次返回(随时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆