9 stable releases
new 2025.2.1 | Jan 10, 2025 |
---|---|
2025.2.0 | Dec 25, 2024 |
2025.1.0 | Dec 25, 2024 |
1.2025.4 | Dec 25, 2024 |
1.2015.1 | Dec 25, 2024 |
#97 in Date and time
705 downloads per month
19KB
269 lines
Trade Date Library
公共方法
is_work_day
pub fn is_work_day(date: impl types::DateTrait) -> bool
判断给定日期是否是工作日。 输入格式:20240101_i64, 20240101_i32, NaiveDate, NaiveDateTime
is_work_time_now
pub fn is_work_time_now(befor_time: (u32, u32), after_time: (u32, u32)) -> bool
判断当前时间是否在工作时间内。
is_work_day_now
pub fn is_work_day_now() -> bool
判断当前日期是否是工作日。
get_trade_day
pub fn get_trade_day(befor_time: (u32, u32), after_time: (u32, u32)) -> i64
获得交易日。如果当天是交易日,返回当天;如果当天超过交易时间,返回下一个交易日。如果当天不是交易日,返回下一个交易日。如果当天是周末,返回下一个交易日。
get_trade_day_now
pub fn get_trade_day_now() -> i64
获得当前交易日。如果当天是交易日,返回当天;如果当天不是交易日,返回下一个交易日。
get_market_day
pub fn get_market_day(start_time: (u32, u32)) -> i64
获得行情日期: 如果今天行情已经启动,返回今天;如果今天行情未启动,返回上一个交易日。
get_trade_day_offset
pub fn get_trade_day_offset(date: i64, mut offset: i64) -> i64
获得输入的值交易日的上(下)一个交易日 如果输入的值是交易日,返回此交易的上一个交易日;如果当天不是交易日,返回上一个交易日的上一个交易日 v: 前几个交易日 测试用例:
#[test]
fn test_get_trade_day_offset() {
// 2024-12-25 start
assert_eq!(get_trade_day_offset(20241225, 0), 20241225);
assert_eq!(get_trade_day_offset(20241225, -1), 20241224);
assert_eq!(get_trade_day_offset(20241225, -2), 20241223);
// 2024-12-22 and 2024-12-21 is a weekend; is skip
assert_eq!(get_trade_day_offset(20241225, -3), 20241220);
assert_eq!(get_trade_day_offset(20241225, -4), 20241219);
assert_eq!(get_trade_day_offset(20241225, -5), 20241218);
assert_eq!(get_trade_day_offset(20241225, -6), 20241217);
assert_eq!(get_trade_day_offset(20241225, -7), 20241216);
// 2024-12-15 and 2024-12-14 is a holiday; is skip
assert_eq!(get_trade_day_offset(20241225, -8), 20241213);
assert_eq!(get_trade_day_offset(20241225, -9), 20241212);
assert_eq!(get_trade_day_offset(20241225, -10), 20241211);
// 2024-12-22 start
assert_eq!(get_trade_day_offset(20241222, 0), 20241223);
assert_eq!(get_trade_day_offset(20241222, -1), 20241220);
assert_eq!(get_trade_day_offset(20241222, -2), 20241219);
assert_eq!(get_trade_day_offset(20241222, -3), 20241218);
// 2024-12-20 start
assert_eq!(get_trade_day_offset(20241220, 0), 20241220);
// 2024-12-21 and 2024-12-22 is a weekend; is skip
assert_eq!(get_trade_day_offset(20241220, 1), 20241223);
assert_eq!(get_trade_day_offset(20241220, 2), 20241224);
assert_eq!(get_trade_day_offset(20241220, 3), 20241225);
assert_eq!(get_trade_day_offset(20241220, 4), 20241226);
// 2024-12-21 start
assert_eq!(get_trade_day_offset(20241221, 0), 20241223);
assert_eq!(get_trade_day_offset(20241221, 1), 20241223);
assert_eq!(get_trade_day_offset(20241221, 2), 20241224);
assert_eq!(get_trade_day_offset(20241221, 3), 20241225);
assert_eq!(get_trade_day_offset(20241221, 4), 20241226);
assert_eq!(get_trade_day_offset(20241221, 5), 20241227);
// 2024-12-28 and 2024-12-29 is a weekend; is skip
assert_eq!(get_trade_day_offset(20241221, 6), 20241230);
assert_eq!(get_trade_day_offset(20241221, 7), 20241231);
// 2025-01-01 is a holiday; is skip
assert_eq!(get_trade_day_offset(20241221, 8), 20250102);
assert_eq!(get_trade_day_offset(20241221, 9), 20250103);
// 2025-01-04 and 2025-01-05 is a weekend; is skip
assert_eq!(get_trade_day_offset(20241221, 10), 20250106);
// 2024-12-25 start
assert_eq!(get_trade_day_offset(20241225, 0), 20241225);
assert_eq!(get_trade_day_offset(20241225, 1), 20241226);
assert_eq!(get_trade_day_offset(20241225, 2), 20241227);
// 2024-12-28 and 2024-12-29 is a weekend; is skip
assert_eq!(get_trade_day_offset(20241225, 3), 20241230);
}
Dependencies
~1MB
~18K SLoC