astro-rust icon indicating copy to clipboard operation
astro-rust copied to clipboard

decimal_day is broken

Open meshula opened this issue 7 years ago • 4 comments

The existing code in decimal_day is incorrect. It doesn't divide minutes by minutes/day, but by minutes per hour, and seconds not by seconds/day but by seconds/minute.

Corrected version:

fn decimal_day(day: &astro::time::DayOfMonth) -> f64 {
    (day.day as f64)
 + (day.hr as f64) / 24.0
  + (day.min as f64) / (60.0 * 24.0)
  + day.sec / (60.0 * 60.0 * 24.0)
  - day.time_zone / 24.0
}

With this fix, the following program will print the correct Julian Ephemeris Date for UTC now.

extern crate astro;
extern crate chrono;

use chrono::prelude::*;

fn decimal_day(day: &astro::time::DayOfMonth) -> f64 {
    (day.day as f64)
  + (day.hr as f64) / 24.0
  + (day.min as f64) / (60.0 * 24.0)
  + day.sec / (60.0 * 60.0 * 24.0)
  - day.time_zone / 24.0
}

fn tm_to_date(now: DateTime<Utc>) -> astro::time::Date
{
    let day_of_month = astro::time::DayOfMonth {
        day : now.day() as u8,
        hr  : now.hour() as u8,
        min : now.minute() as u8,
        sec : 0f64,//now.second() as f64,
        time_zone : 0.0 };

    astro::time::Date {
        year : now.year() as i16,
        month : now.month() as u8,
        decimal_day : decimal_day(&day_of_month),
        cal_type : astro::time::CalType::Gregorian }
}

fn now_utc() -> astro::time::Date
{
    let utc: DateTime<Utc> = Utc::now(); 
    println!("{:?}", utc);
    tm_to_date(utc)
}

fn main() {
    let date = now_utc();
    let julian_day = astro::time::julian_day(&date);
    let delta_t = astro::time::delta_t(date.year as i32, date.month);
    let julian_ephm_day = astro::time::julian_ephemeris_day(julian_day, delta_t);
    println!("{}", julian_ephm_day);
}

meshula avatar Jan 07 '18 01:01 meshula

Confirmed this problem

astrojhgu avatar Mar 10 '18 11:03 astrojhgu

@meshula Thanks for the report! I won't be able to fix this anytime soon, please feel free to open a PR!

saurvs avatar Mar 24 '18 17:03 saurvs

Since this has not yet been fixed, I've submitted a PR (#10).

mhaessig avatar Jul 05 '18 18:07 mhaessig

Thanks!

meshula avatar Jul 06 '18 00:07 meshula

Has this issue been fixed? Or is it open for a reason?

wildwestrom avatar Apr 26 '23 09:04 wildwestrom

I created it, so I'll close it.

meshula avatar Apr 26 '23 16:04 meshula