Skip to content

Instantly share code, notes, and snippets.

@woxtu
Last active August 29, 2015 14:08
Show Gist options
  • Save woxtu/2b010eb446f46faf8f80 to your computer and use it in GitHub Desktop.
Save woxtu/2b010eb446f46faf8f80 to your computer and use it in GitHub Desktop.
extern crate time;
fn main() {
let now = time::now();
let year = now.tm_year as uint + 1900;
let month = now.tm_mon as uint + 1;
let first_wday = {
let mut t = now.clone();
t.tm_mday = 1;
t.to_local().tm_wday as uint
};
let last_day = match month {
1 | 3 | 5 | 7 | 8 | 10 | 12 => 31u,
4 | 6 | 9 | 11 => 30,
_ if year % 4 == 0 && year % 100 != 0 || year % 400 == 0 => 29,
_ => 28,
};
let xs =
range(0u, first_wday).map(|_| format!(" "))
.chain(range(1u, last_day + 1).map(|x| format!("{:2u}", x)))
.collect::<Vec<_>>()
.as_slice()
.chunks(7)
.map(|x| x.connect(" "))
.collect::<Vec<_>>()
.connect("\n");
println!("{}", xs);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment