#date-time #unix-timestamp #julian #day #chrono #convert #fuzzy

julian_day_converter

为chrono::NaiveDateTime提供儒略日转换方法,以及直接转换为和从Unix时间戳的方法,还包括一个补充的模糊ISO日期时间解析器。

1 个不稳定版本

0.3.2 2023年12月5日
0.3.1 2023年12月5日
0.3.0 2023年11月18日
0.2.1 2023年11月9日
0.1.8 2023年11月8日

#169 in 日期和时间


rust_solar中使用

GPL-2.0-or-later WITH Bison-exception-2…

17KB
92

mirror crates.io docs.rs

Chrono的儒略日兼容性方法

该库为使用64位浮点数表示儒略日的天文应用程序提供兼容性。一个儒略日表示自儒略周期开始以来的天数,儒略周期始于公元前4713年11月24日中午(-4713-11-24T12:00:00 UTC)。儒略日便于处理长时间段,不应与儒略历混淆,儒略历影响闰年的分配规则。

该Crate向Rust的标准日期时间库Chrono添加了三个特性和6个实用方法,以及转换为和从Unix时间戳的独立函数。所有日期时间值都是UTC,但可以转换为时区感知的chrono::DateTime

存在类似的julianday crate来处理儒略日作为整数,并将它们转换为chrono::NaiveDate。我开发这个crate的主要目的是确保与天文学API服务器的互操作性,该服务器利用瑞士星历计算引擎。

直接函数

unixtime_to_julian_day(ts: i64) -> f64

将Unix时间戳直接转换为64位浮点数儒略日,与许多天文应用程序兼容。

julian_day_to_unixtime(jd: f64) -> i64

将儒略日作为有符号的64位整数转换。如果需要将时间戳转换为32位整数,则1902年之前和2038年之后的日期将超出范围。

julian_day_to_weekday_index(jd: f64, offset_secs: i32) -> u8

计算星期索引,其中星期日=0,星期一=1,星期六=6。这将适用于任何历史或未来的儒略日,无论是否可以转换为NaiveDateTime对象。

julian_day_to_datetime(jd: f64) -> Result<NaiveDateTime, ParsedError>

返回与Rust其他解析器一致的结果类型,而其实现在chrono::NaiveDateTime返回选项,与其他库中相同的解析方法保持一致。

datetime_to_julian_day(dt_str: &str) -> Result<f64, ParsedError>

将模糊的ISO-8601-like字符串转换为儒略日值。这个函数返回的结果类型与其它Rust解析器一致。大约的YYYY-mm-dd HH:MM:SS日期时间字符串被修正为不带毫秒或时区后缀的普通ISO-8601格式。这相当于从NaiveDateTime::from_fuzzy_iso_string()创建一个NaiveDateTime对象,然后使用date_time.to_jd()方法;

iso_fuzzy_string_to_datetime(dt: &str) -> Result

将模糊的ISO-8601-like字符串转换为NaiveDateTime。这个函数返回的结果类型与其它Rust解析器一致,但其对于chrono::NaiveDateTime的实现返回一个Option,与同一库中的其他构造函数一致。注意:在版本0.3之前,它返回一个Option

特性

儒略日

必须实现

  • to_jd(&self) -> f64
  • from_jd(jd: f64) -> Option<Self>

FromFuzzyISOString

必须实现

  • from_fuzzy_iso_string(&self,dt_str: &str) -> Option<Self>

WeekdayIndex

必须实现

  • weekday_index(&self,offset_secs: i32) -> u8

如果已知太阳时或标准本地时区偏移,此函数计算时区无关DateTime对象的星期索引(星期日=0,星期一=1 ... 星期六=6)。太阳时区偏移秒数可以通过经度计算得出,例如1°=240秒,例如-3°(或3°W)将是-720。

用法

use chrono::NaiveDateTime;
use julian_day_converter::*;

fn main() {
    // Convert a sample Julian Day value to a valid NaiveDateTime object and then use to_jd() for interoperability
    // with astronomical applications
    // The return value is a result consistent with other parse functions
    // julian_day_to_datetime(jd_value) is equivalent to NaiveDateTime::from_jd(jd_value)
    let sample_julian_day = 2459827.25;
    if let Ok(date_time) = julian_day_to_datetime(sample_julian_day) {
      let formatted_date_time_string = date_time.format("%Y-%m-%dT%H:%M:%S").to_string();
      println!("The Julian day {} is {} as ISO date-time", sample_julian_day, formatted_date_time_string);
    }
    
    // Convert an approximate date-time string to a valid NaiveDateTime object and then use to_jd() for interoperability
    // with astronomical applications
    // The return value is a result consistent with other parse functions
    let approx_date_time = "2023-11-09 15"; // 3pm 
    if let Some(date_time) = NaiveDateTime::from_fuzzy_iso_string(approx_date_time) {
      let formatted_date_time_string = date_time.format("%Y-%m-%dT%H:%M:%S").to_string();
      println!("The input time of `{}` is assumed to be {} UTC and in Julian Days is {}", approx_date_time, formatted_date_time_string, date_time.to_jd());
    }
  
    let historical_jd = 2334317.39336563;
    // Convert to a NaiveDateTime object and then apply its format method
    // The return value is an options consistent with other chrono::NaiveDateTime constructors
    if let Some(date_time) = NaiveDateTime::from_jd(historical_jd) {
      let formatted_date_time_string = date_time.format("%Y-%m-%dT%H:%M:%S").to_string();
      println!("The meteorite landed on Earth at {} Julian days or {}", historical_jd, formatted_date_time_string);
    }

    let prehistoric_julian_day = -190338.875;
    // Convert to a NaiveDateTime object and then apply its format method
    // The return value is a result consistent with other parse functions
    if let Some(date_time) = NaiveDateTime::from_jd(prehistoric_julian_day) {
      let formatted_date_time_string = date_time.format("%Y-%m-%dT%H:%M:%S").to_string();
      println!("An asteroid hit the Earth at {} Julian days or {}", historical_jd, formatted_date_time_string);
    }

    let unix_timestamp = 169938309;
    // does not require conversion to a NaiveDateTime object
    let jd = unixtime_to_julian_day(unix_timestamp);
    println!("All valid 64 bit integers can be converted to Julian days, e.g. {} is {} Julian days", unix_timestamp, jd);
    
    let julian_day = 2134937.3937f64;
    // does not require conversion to a NaiveDateTime object
    let ts = julian_day_to_unixtime(julian_day);
    println!("Some historical and far-future Julian days may yield very big timestamp values e.g. {} is {} as a unix timestamp", julian_day, ts);
  
  }

依赖

~1MB
~18K SLoC