Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::cmp::Ordering;
- #[allow(unused)]
- fn main()
- {
- /*
- // Doesn't run in playground
- let mut one = String::new();
- let mut two = String::new();
- println!("Input should follow mm/dd/yyyy format");
- println!("Please input the starting date ");
- std::io::stdin().read_line(&mut one).unwrap_or(0);
- println!("Please input the ending date ");
- std::io::stdin().read_line(&mut two).unwrap_or(0);
- let one = one.trim().to_string();
- let two = two.trim().to_string();
- */
- /* testing purposes */
- let one = "10/20/200".to_string();
- let two = "2/11/2024".to_string();
- let mut diff = 0;
- // ensure dates differ
- match &one.cmp(&two) {
- Ordering::Equal => diff = 0,
- _ => diff = date_diff(&one, &two),
- }
- if diff == 0 {
- println!("Potential error. Please check input is correct.");
- } else {
- println!("{} day(s) between {} and {}", diff, one, two);
- }
- }
- #[allow(unused)]
- fn date_diff(date1: &String, date2: &String) -> u32
- {
- /* setup for more readable code */
- // zero added to let month index feel more intuitive
- let months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
- let (year, month, day) = (0, 1, 2);
- let mut day_count = 0;
- let mut current_date = Vec::new();
- let mut end_date = Vec::new();
- // mostly error checking
- {
- // hold u16 dates in format of yyyy, mm, dd
- let mut date_a = Vec::new();
- let mut date_b = Vec::new();
- let mut a_first = false;
- // separate each paramater into three string slices
- let start = date1.split('/').collect::<Vec<&str>>();
- let end = date2.split('/').collect::<Vec<&str>>();
- // convert year to number, and push onto vectors
- date_a.push(end[2].parse::<u16>().unwrap_or(0));
- date_b.push(start[2].parse::<u16>().unwrap_or(0));
- // convert and push month onto dates
- date_a.push(end[0].parse::<u16>().unwrap_or(0));
- date_b.push(start[0].parse::<u16>().unwrap_or(0));
- // convert and push day onto dates
- date_a.push(end[1].parse::<u16>().unwrap_or(0));
- date_b.push(start[1].parse::<u16>().unwrap_or(0));
- // error checking for unwrap on date_a and date_b
- if date_a.contains(&0) || date_b.contains(&0) {
- return 0;
- }
- // cut down size of next two code blocks
- let y1 = date_a[year];
- let y2 = date_b[year];
- let m1 = date_b[month];
- let m2 = date_b[month];
- let d1 = date_a[day];
- let d2 = date_b[day];
- /* set flag for which date comes first chronologically */
- if y1 < y2 && m1 < m2 && d1 < d2 {
- a_first = true;
- } else {
- a_first = false; // redundant, not implicit
- }
- /* appropriate bounds for months and days, including leap years */
- // edge case first, check feb in leap year
- if is_leap(y1) && m1 == 2 && d1 > 29
- || is_leap(y2) && m1 == 2 && d1 > 29 {
- return 0;
- // no month greater than 12, no days greater than given month
- } else if m1 > 12 || m2 > 12
- || d1 > months[m1 as usize]
- || d2 > months[m2 as usize] {
- return 0;
- }
- // use well named variables
- if !a_first {
- current_date = date_b;
- end_date = date_a;
- } else {
- current_date = date_a;
- end_date = date_b;
- }
- }
- // increment counter and date until dates match
- while current_date != end_date {
- // same year and month; increment only days.
- if current_date[year..=month] == end_date[year..=month] {
- day_count += u32::from(end_date[day] - current_date[day]);
- current_date[day] = end_date[day];
- // match months, set days to 1
- } else if current_date[year] == end_date[year] {
- if current_date[month] < end_date[month] {
- day_count +=
- u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
- current_date[day] = 1;
- if current_date[month] == 2 && is_leap(current_date[year]) {
- day_count += 1;
- }
- }
- current_date[month] += 1;
- } else if current_date[year] < end_date[year] {
- // add a year worth of days
- if current_date[month] == 1 && current_date[day] == 1 {
- if is_leap(current_date[year]) {
- day_count += 366;
- current_date[year] += 1;
- } else {
- day_count += 365;
- current_date[year] += 1;
- }
- // get current_date to the end of the year
- } else {
- // add days
- if current_date[month] == end_date[month] {
- day_count += u32::from(end_date[day] - current_date[day]);
- current_date[day] = end_date[day];
- // add months
- } else if current_date[month] < 12 {
- day_count +=
- u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
- current_date[day] = 1;
- // feburary in leap years get a extra day
- if current_date[month] == 2 && is_leap(current_date[year]) {
- day_count += 1;
- }
- current_date[month] += 1;
- // add december, set current_date to the first day, first month, and add 1 to year count
- } else {
- day_count +=
- u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
- current_date[day] = 1;
- current_date[month] = 1;
- current_date[year] += 1;
- }
- }
- // something probably went wrong, oh well
- } else {
- return 0;
- }
- }
- // return counter
- day_count
- }
- fn is_leap(year: u16) -> bool
- {
- // every 4th year that is not divisible by 100 or is divisible by 400
- year % 4 == 0 && year % 100 != 0 || year % 400 == 0
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement