Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. use std::cmp::Ordering;
  2.  
  3. #[allow(unused)]
  4. fn main()
  5. {
  6. /*
  7. // Doesn't run in playground
  8. let mut one = String::new();
  9. let mut two = String::new();
  10.  
  11. println!("Input should follow mm/dd/yyyy format");
  12. println!("Please input the starting date ");
  13. std::io::stdin().read_line(&mut one).unwrap_or(0);
  14.  
  15. println!("Please input the ending date ");
  16. std::io::stdin().read_line(&mut two).unwrap_or(0);
  17.  
  18. let one = one.trim().to_string();
  19. let two = two.trim().to_string();
  20. */
  21.  
  22. /* testing purposes */
  23. let one = "10/20/200".to_string();
  24. let two = "2/11/2024".to_string();
  25.  
  26. let mut diff = 0;
  27.  
  28. // ensure dates differ
  29. match &one.cmp(&two) {
  30. Ordering::Equal => diff = 0,
  31. _ => diff = date_diff(&one, &two),
  32. }
  33.  
  34. if diff == 0 {
  35. println!("Potential error. Please check input is correct.");
  36. } else {
  37. println!("{} day(s) between {} and {}", diff, one, two);
  38. }
  39. }
  40.  
  41. #[allow(unused)]
  42. fn date_diff(date1: &String, date2: &String) -> u32
  43. {
  44. /* setup for more readable code */
  45. // zero added to let month index feel more intuitive
  46. let months = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  47. let (year, month, day) = (0, 1, 2);
  48.  
  49. let mut day_count = 0;
  50. let mut current_date = Vec::new();
  51. let mut end_date = Vec::new();
  52.  
  53. // mostly error checking
  54. {
  55. // hold u16 dates in format of yyyy, mm, dd
  56. let mut date_a = Vec::new();
  57. let mut date_b = Vec::new();
  58. let mut a_first = false;
  59.  
  60. // separate each paramater into three string slices
  61. let start = date1.split('/').collect::<Vec<&str>>();
  62. let end = date2.split('/').collect::<Vec<&str>>();
  63.  
  64. // convert year to number, and push onto vectors
  65. date_a.push(end[2].parse::<u16>().unwrap_or(0));
  66. date_b.push(start[2].parse::<u16>().unwrap_or(0));
  67. // convert and push month onto dates
  68. date_a.push(end[0].parse::<u16>().unwrap_or(0));
  69. date_b.push(start[0].parse::<u16>().unwrap_or(0));
  70. // convert and push day onto dates
  71. date_a.push(end[1].parse::<u16>().unwrap_or(0));
  72. date_b.push(start[1].parse::<u16>().unwrap_or(0));
  73.  
  74. // error checking for unwrap on date_a and date_b
  75. if date_a.contains(&0) || date_b.contains(&0) {
  76. return 0;
  77. }
  78.  
  79. // cut down size of next two code blocks
  80. let y1 = date_a[year];
  81. let y2 = date_b[year];
  82. let m1 = date_b[month];
  83. let m2 = date_b[month];
  84. let d1 = date_a[day];
  85. let d2 = date_b[day];
  86.  
  87. /* set flag for which date comes first chronologically */
  88. if y1 < y2 && m1 < m2 && d1 < d2 {
  89. a_first = true;
  90. } else {
  91. a_first = false; // redundant, not implicit
  92. }
  93.  
  94. /* appropriate bounds for months and days, including leap years */
  95. // edge case first, check feb in leap year
  96. if is_leap(y1) && m1 == 2 && d1 > 29
  97. || is_leap(y2) && m1 == 2 && d1 > 29 {
  98. return 0;
  99. // no month greater than 12, no days greater than given month
  100. } else if m1 > 12 || m2 > 12
  101. || d1 > months[m1 as usize]
  102. || d2 > months[m2 as usize] {
  103. return 0;
  104. }
  105.  
  106. // use well named variables
  107. if !a_first {
  108. current_date = date_b;
  109. end_date = date_a;
  110. } else {
  111. current_date = date_a;
  112. end_date = date_b;
  113. }
  114. }
  115.  
  116. // increment counter and date until dates match
  117. while current_date != end_date {
  118. // same year and month; increment only days.
  119. if current_date[year..=month] == end_date[year..=month] {
  120. day_count += u32::from(end_date[day] - current_date[day]);
  121. current_date[day] = end_date[day];
  122. // match months, set days to 1
  123. } else if current_date[year] == end_date[year] {
  124. if current_date[month] < end_date[month] {
  125. day_count +=
  126. u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
  127. current_date[day] = 1;
  128. if current_date[month] == 2 && is_leap(current_date[year]) {
  129. day_count += 1;
  130. }
  131. }
  132. current_date[month] += 1;
  133. } else if current_date[year] < end_date[year] {
  134. // add a year worth of days
  135. if current_date[month] == 1 && current_date[day] == 1 {
  136. if is_leap(current_date[year]) {
  137. day_count += 366;
  138. current_date[year] += 1;
  139. } else {
  140. day_count += 365;
  141. current_date[year] += 1;
  142. }
  143. // get current_date to the end of the year
  144. } else {
  145. // add days
  146. if current_date[month] == end_date[month] {
  147. day_count += u32::from(end_date[day] - current_date[day]);
  148. current_date[day] = end_date[day];
  149. // add months
  150. } else if current_date[month] < 12 {
  151. day_count +=
  152. u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
  153. current_date[day] = 1;
  154. // feburary in leap years get a extra day
  155. if current_date[month] == 2 && is_leap(current_date[year]) {
  156. day_count += 1;
  157. }
  158. current_date[month] += 1;
  159. // add december, set current_date to the first day, first month, and add 1 to year count
  160. } else {
  161. day_count +=
  162. u32::from(months[current_date[month] as usize] - (current_date[day] - 1));
  163. current_date[day] = 1;
  164. current_date[month] = 1;
  165. current_date[year] += 1;
  166. }
  167. }
  168. // something probably went wrong, oh well
  169. } else {
  170. return 0;
  171. }
  172. }
  173.  
  174. // return counter
  175. day_count
  176. }
  177.  
  178. fn is_leap(year: u16) -> bool
  179. {
  180. // every 4th year that is not divisible by 100 or is divisible by 400
  181. year % 4 == 0 && year % 100 != 0 || year % 400 == 0
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement