1 use { 2 anyhow::Result, 3 std::sync::{Arc, Mutex}, 4 std::{fs::File, io}, 5 }; 6 7 pub struct FileTarget { 8 pub cur_file_handler: Arc<Mutex<File>>, 9 } 10 11 impl FileTarget { new(file: File) -> Result<Self>12 pub fn new(file: File) -> Result<Self> { 13 Ok(Self { 14 cur_file_handler: Arc::new(Mutex::new(file)), 15 }) 16 } 17 } 18 19 impl io::Write for FileTarget { write(&mut self, buf: &[u8]) -> io::Result<usize>20 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 21 match self.cur_file_handler.lock().unwrap().write(buf) { 22 Ok(rv) => Ok(rv), 23 Err(err) => { 24 println!("write err {err}"); 25 Ok(0) 26 } 27 } 28 } flush(&mut self) -> io::Result<()>29 fn flush(&mut self) -> io::Result<()> { 30 println!("flush"); 31 let mut file_handler = self.cur_file_handler.lock().unwrap(); 32 file_handler.flush() 33 } 34 } 35 36 #[cfg(test)] 37 mod tests { 38 39 use chrono::prelude::*; 40 41 #[test] test_chrono()42 fn test_chrono() { 43 // Convert the timestamp string into an i64 44 let timestamp = "1524820690".parse::<i64>().unwrap(); 45 // Create a NaiveDateTime from the timestamp 46 let naive = NaiveDateTime::from_timestamp(timestamp, 0); 47 // Create a normal DateTime from the NaiveDateTime 48 let datetime: DateTime<Utc> = DateTime::from_utc(naive, Utc); 49 // Format the datetime how you want 50 let newdate = datetime.format("%Y-%m-%d %H:%M:%S"); 51 // Print the newly formatted date and time 52 let dt: DateTime<Local> = Local::now(); 53 let cur_number = format!( 54 "{}-{:02}-{:02} {:02}:{:02}:00", 55 dt.year(), 56 dt.month(), 57 dt.day(), 58 dt.hour(), 59 dt.minute() 60 ); 61 62 println!("{newdate}"); 63 println!("{cur_number}"); 64 } 65 66 67 68 // #[test] 69 // fn test_job_scheduler_ng() { 70 // let mut sched = JobScheduler::new(); 71 72 // sched.add(Job::new("0 0 * * * *".parse().unwrap(), || { 73 // let dt: DateTime<Local> = Local::now(); 74 // let cur_number = format!( 75 // "{}-{:02}-{:02} {:02}:{:02}:00", 76 // dt.year(), 77 // dt.month(), 78 // dt.day(), 79 // dt.hour(), 80 // dt.minute() 81 // ); 82 // println!("time number: {cur_number}"); 83 // })); 84 85 // loop { 86 // sched.tick(); 87 // std::thread::sleep(Duration::from_millis(500)); 88 // } 89 // } 90 } 91