mirror of
https://codeberg.org/andyscott/exercism.git
synced 2024-12-22 06:43:09 -05:00
Rust: completed Gigasecond again
This commit is contained in:
parent
65cbcd83d8
commit
59a1cb14fa
4 changed files with 57 additions and 46 deletions
|
@ -1 +1 @@
|
||||||
{"track":"rust","exercise":"gigasecond","id":"46cf4dcc2c1d42f48213bc4054d72a04","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
{"track":"rust","exercise":"gigasecond","id":"20e9d27ceb734a788023c5f555e0d88e","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
|
@ -1,6 +1,6 @@
|
||||||
use time::{Duration, PrimitiveDateTime as DateTime};
|
use time::PrimitiveDateTime as DateTime;
|
||||||
|
|
||||||
// Returns a DateTime one billion seconds after start.
|
// Returns a DateTime one billion seconds after start.
|
||||||
pub fn after(start: DateTime) -> DateTime {
|
pub fn after(start: DateTime) -> DateTime {
|
||||||
start + Duration::seconds(1_000_000_000)
|
start + time::Duration::seconds(1_000_000_000)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,48 +1,59 @@
|
||||||
use time::PrimitiveDateTime as DateTime;
|
#[test]
|
||||||
|
fn date_only_specification_of_time() {
|
||||||
|
let start = datetime(2011, 4, 25, 0, 0, 0);
|
||||||
|
let actual = gigasecond::after(start);
|
||||||
|
let expected = datetime(2043, 1, 1, 1, 46, 40);
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a datetime from the given numeric point in time.
|
#[test]
|
||||||
///
|
#[ignore]
|
||||||
/// Panics if any field is invalid.
|
fn second_test_for_date_only_specification_of_time() {
|
||||||
fn dt(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime {
|
let start = datetime(1977, 6, 13, 0, 0, 0);
|
||||||
use time::{Date, Time};
|
let actual = gigasecond::after(start);
|
||||||
|
let expected = datetime(2009, 2, 19, 1, 46, 40);
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
DateTime::new(
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn third_test_for_date_only_specification_of_time() {
|
||||||
|
let start = datetime(1959, 7, 19, 0, 0, 0);
|
||||||
|
let actual = gigasecond::after(start);
|
||||||
|
let expected = datetime(1991, 3, 27, 1, 46, 40);
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn full_time_specified() {
|
||||||
|
let start = datetime(2015, 1, 24, 22, 0, 0);
|
||||||
|
let actual = gigasecond::after(start);
|
||||||
|
let expected = datetime(2046, 10, 2, 23, 46, 40);
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn full_time_with_day_roll_over() {
|
||||||
|
let start = datetime(2015, 1, 24, 23, 59, 59);
|
||||||
|
let actual = gigasecond::after(start);
|
||||||
|
let expected = datetime(2046, 10, 3, 1, 46, 39);
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn datetime(
|
||||||
|
year: i32,
|
||||||
|
month: u8,
|
||||||
|
day: u8,
|
||||||
|
hour: u8,
|
||||||
|
minute: u8,
|
||||||
|
second: u8,
|
||||||
|
) -> time::PrimitiveDateTime {
|
||||||
|
use time::{Date, PrimitiveDateTime, Time};
|
||||||
|
|
||||||
|
PrimitiveDateTime::new(
|
||||||
Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(),
|
Date::from_calendar_date(year, month.try_into().unwrap(), day).unwrap(),
|
||||||
Time::from_hms(hour, minute, second).unwrap(),
|
Time::from_hms(hour, minute, second).unwrap(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn date() {
|
|
||||||
let start_date = dt(2011, 4, 25, 0, 0, 0);
|
|
||||||
|
|
||||||
assert_eq!(gigasecond::after(start_date), dt(2043, 1, 1, 1, 46, 40));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn another_date() {
|
|
||||||
let start_date = dt(1977, 6, 13, 0, 0, 0);
|
|
||||||
|
|
||||||
assert_eq!(gigasecond::after(start_date), dt(2009, 2, 19, 1, 46, 40));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn third_date() {
|
|
||||||
let start_date = dt(1959, 7, 19, 0, 0, 0);
|
|
||||||
|
|
||||||
assert_eq!(gigasecond::after(start_date), dt(1991, 3, 27, 1, 46, 40));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn datetime() {
|
|
||||||
let start_date = dt(2015, 1, 24, 22, 0, 0);
|
|
||||||
|
|
||||||
assert_eq!(gigasecond::after(start_date), dt(2046, 10, 2, 23, 46, 40));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn another_datetime() {
|
|
||||||
let start_date = dt(2015, 1, 24, 23, 59, 59);
|
|
||||||
|
|
||||||
assert_eq!(gigasecond::after(start_date), dt(2046, 10, 3, 1, 46, 39));
|
|
||||||
}
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
{"track":"rust","exercise":"hello-world","id":"8fc7d235b9714b939df049625c414d2c","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
{"track":"rust","exercise":"hello-world","id":"bf8f90455cd64bda84afecac949ecd0d","url":"https://exercism.org/tracks/rust/exercises/hello-world","handle":"Chomp1295","is_requester":true,"auto_approve":false}
|
Loading…
Reference in a new issue