use chrono::NaiveDate;
use std::error::Error;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::str::FromStr; // Add chrono crate for date parsing
fn main() -> Result<(), Box<dyn Error>> {
let file = File::open("employees-with-date.csv")?;
let reader = BufReader::new(file);
let mut lines: Vec<String> = Vec::new();
for line in reader.lines() {
let line = line?;
lines.push(line);
}
let threshold_date = NaiveDate::from_str("2020-01-01")?; // Set the threshold date (Jan 1, 2020)
println!("Lines in the file with date after January 2020:");
for (i, line) in lines.iter().enumerate() {
// Split the line into columns (assume CSV format with commas)
let columns: Vec<&str> = line.split(',').collect();
if columns.len() >= 2 {
// Parse the date from the second column (assuming it's in YYYY-MM-DD format)
if let Ok(date) = NaiveDate::from_str(columns[3]) {
if date > threshold_date {
// If the date is after Jan 1, 2020, print the line
println!("{}: {}", i + 1, line);
// Optionally, debug the line
let mut my_vec: Vec<_> = vec![];
my_vec.push(line);
dbg!(my_vec);
}
}
}
}
Ok(())
}