Skip to content

Commit ee83e1f

Browse files
committed
Implement Gigasecond exercise
Adding the [Gigasecond exercise](http://x.exercism.io/problems/gigasecond) for the Rust track. This exercise is a little odd in Rust as the core language has no library for dealing with time. There was the Time crate, but that was pulled from the standard library. Based on [this RFC](rust-lang/rfcs#619) from last year, the Chrono crate appears to be the best current library for dealing with time. I've used Chrono here. Either because I'm still new to Rust, or because of an actual limitation in Chrono, the best way I could write the tests was to have everything be a UTC DateTime. This means that the tests that only use dates still require the slightly weird `.and_hms(0,0,0)` function at the end to make them DateTime-y. I did this because Chrono only seems to support Duration addition for DateTimes, not Dates.
1 parent 5d5265d commit ee83e1f

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

exercises/gigasecond/Cargo.lock

Lines changed: 55 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

exercises/gigasecond/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "gigasecond"
3+
version = "0.0.0"
4+
5+
[dependencies]
6+
chrono = "0.2"
7+

exercises/gigasecond/example.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate chrono;
2+
use chrono::*;
3+
4+
pub fn after(start: DateTime<UTC>) -> DateTime<UTC> {
5+
start + Duration::seconds(1_000_000_000)
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
extern crate gigasecond;
2+
3+
//Note: Your code must also start with the following two lines.
4+
extern crate chrono;
5+
use chrono::*;
6+
7+
#[test]
8+
fn test_date() {
9+
let start_date = UTC.ymd(2011, 4, 25).and_hms(0,0,0);
10+
assert_eq!(gigasecond::after(start_date), UTC.ymd(2043, 1, 1).and_hms(1,46,40));
11+
}
12+
13+
#[test]
14+
#[ignore]
15+
fn test_another_date() {
16+
let start_date = UTC.ymd(1977, 6, 13).and_hms(0,0,0);
17+
assert_eq!(gigasecond::after(start_date), UTC.ymd(2009, 2, 19).and_hms(1,46,40));
18+
}
19+
20+
#[test]
21+
#[ignore]
22+
fn test_third_date() {
23+
let start_date = UTC.ymd(1959, 7, 19).and_hms(0,0,0);
24+
assert_eq!(gigasecond::after(start_date), UTC.ymd(1991, 3, 27).and_hms(1,46,40));
25+
}
26+
27+
#[test]
28+
#[ignore]
29+
fn test_datetime() {
30+
let start_date = UTC.ymd(2015, 1, 24).and_hms(22,0,0);
31+
assert_eq!(gigasecond::after(start_date), UTC.ymd(2046, 10, 2).and_hms(23,46,40));
32+
}
33+
34+
#[test]
35+
#[ignore]
36+
fn test_another_datetime() {
37+
let start_date = UTC.ymd(2015, 1, 24).and_hms(23,59,59);
38+
assert_eq!(gigasecond::after(start_date), UTC.ymd(2046, 10, 3).and_hms(1,46,39));
39+
}

0 commit comments

Comments
 (0)