Last active
August 29, 2015 14:15
-
-
Save michaelrepper/859f367a3fac10ba7770 to your computer and use it in GitHub Desktop.
r/Dailyprogrammer Challenge #201 Easy - Rust Date Math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/////////////////////////////////////////////////// | |
// r/Dailyprogrammer #201 Easy // | |
// coded by Michael Repper // | |
// query [a * t] gridoodle [ d * o * t ] com // | |
/////////////////////////////////////////////////// | |
extern crate time; | |
use std::num::SignedInt; | |
//// | |
// Takes 2 dates and returns the difference in days. | |
// Dates should be passed as arrays in the form: | |
// [mm, dd, YYYY] | |
//// | |
fn days_diff(date_1:&[int, ..3], date_2:&[int, ..3] ) | |
{ | |
let proc_1 = 365 * date_1[2] + date_1[2] / 4 - date_1[2] / 100 + date_1[2] / 400 + date_1[1] + (153 * date_1[0] + 8) / 5; | |
let proc_2 = 365 * date_2[2] + date_2[2] / 4 - date_2[2] / 100 + date_2[2] / 400 + date_2[1] + (153 * date_2[0] + 8) / 5; | |
let diff = (proc_1 - proc_2).abs(); | |
println!("{} days from {}-{}-{} to {}-{}-{}", diff, date_1[0], date_1[1], date_1[2], date_2[0], date_2[1], date_2[2]); | |
} | |
fn main() | |
{ | |
let date_1:[int, ..3]; | |
let date_2:[int, ..3]; | |
date_1 = [01, 01, 1901]; | |
date_2 = [01, 01, 3001]; | |
days_diff(&date_1, &date_2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment