use chrono::NaiveDate;
use jp_holidays_lib::{client::Client, error::Error};
static CACHE: tokio::sync::OnceCell<Client> = tokio::sync::OnceCell::const_new();
async fn get_client() -> Result<&'static Client, Error> {
CACHE.get_or_try_init(Client::init).await
}
async fn execute() -> Result<(), Box<dyn std::error::Error>> {
let client = get_client().await?;
let date = NaiveDate::from_ymd_opt(1955, 11, 23).ok_or("存在しない日付です".to_string())?;
let maybe_holiday = client.get_holiday(date);
match maybe_holiday {
Some(holiday) => println!("1955年 11月 23日 は{}", holiday),
None => println!("1955年 11月 23日 は祝日ではありません"),
};
let date = NaiveDate::from_ymd_opt(1956, 3, 21).ok_or("存在しない日付です".to_string())?;
let is_holiday = client.is_holiday(date);
println!(
"1956 3月 21日 は{}",
if is_holiday {
"祝日です"
} else {
"祝日ではありません"
}
);
Ok(())
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
for i in 0..5 {
let start = std::time::Instant::now();
execute().await?;
let duration = start.elapsed();
println!("{}回目の実行時間: {:?}\n", i + 1, duration);
}
Ok(())
}