aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2020-06-09 19:37:37 +0100
committerFederico Igne <git@federicoigne.com>2021-11-03 18:54:52 +0000
commit12c8aedd5dfae75ec6bde982968bc77b70550374 (patch)
treef838b0bd6d15262a13c95c4115669e34accfb123
parent0fa3d7fe5a27b87bcffb89fb30ffd294a0c2c40a (diff)
downloadexercism-12c8aedd5dfae75ec6bde982968bc77b70550374.tar.gz
exercism-12c8aedd5dfae75ec6bde982968bc77b70550374.zip
[rust] Gigasecond
-rw-r--r--gigasecond/.exercism/metadata.json1
-rw-r--r--gigasecond/.gitignore8
-rw-r--r--gigasecond/Cargo.toml8
-rw-r--r--gigasecond/README.md89
-rw-r--r--gigasecond/src/lib.rs7
-rw-r--r--gigasecond/tests/gigasecond.rs51
6 files changed, 164 insertions, 0 deletions
diff --git a/gigasecond/.exercism/metadata.json b/gigasecond/.exercism/metadata.json
new file mode 100644
index 0000000..7bd17ac
--- /dev/null
+++ b/gigasecond/.exercism/metadata.json
@@ -0,0 +1 @@
{"track":"rust","exercise":"gigasecond","id":"2131caf991cf446292290bef2e6d64da","url":"https://exercism.io/my/solutions/2131caf991cf446292290bef2e6d64da","handle":"dyamon","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/gigasecond/.gitignore b/gigasecond/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/gigasecond/.gitignore
@@ -0,0 +1,8 @@
1# Generated by Cargo
2# will have compiled files and executables
3/target/
4**/*.rs.bk
5
6# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
8Cargo.lock
diff --git a/gigasecond/Cargo.toml b/gigasecond/Cargo.toml
new file mode 100644
index 0000000..1a8fb2c
--- /dev/null
+++ b/gigasecond/Cargo.toml
@@ -0,0 +1,8 @@
1[package]
2edition = "2018"
3name = "gigasecond"
4version = "2.0.0"
5
6[dependencies]
7chrono = "0.4"
8
diff --git a/gigasecond/README.md b/gigasecond/README.md
new file mode 100644
index 0000000..9368f91
--- /dev/null
+++ b/gigasecond/README.md
@@ -0,0 +1,89 @@
1# Gigasecond
2
3Given a moment, determine the moment that would be after a gigasecond
4has passed.
5
6A gigasecond is 10^9 (1,000,000,000) seconds.
7
8If you're unsure what operations you can perform on `DateTime<Utc>` take a look at the [chrono crate](https://docs.rs/chrono) which is listed as a dependency in the `Cargo.toml` file for this exercise.
9
10
11## Rust Installation
12
13Refer to the [exercism help page][help-page] for Rust installation and learning
14resources.
15
16## Writing the Code
17
18Execute the tests with:
19
20```bash
21$ cargo test
22```
23
24All but the first test have been ignored. After you get the first test to
25pass, open the tests source file which is located in the `tests` directory
26and remove the `#[ignore]` flag from the next test and get the tests to pass
27again. Each separate test is a function with `#[test]` flag above it.
28Continue, until you pass every test.
29
30If you wish to run all ignored tests without editing the tests source file, use:
31
32```bash
33$ cargo test -- --ignored
34```
35
36To run a specific test, for example `some_test`, you can use:
37
38```bash
39$ cargo test some_test
40```
41
42If the specific test is ignored use:
43
44```bash
45$ cargo test some_test -- --ignored
46```
47
48To learn more about Rust tests refer to the [online test documentation][rust-tests]
49
50Make sure to read the [Modules][modules] chapter if you
51haven't already, it will help you with organizing your files.
52
53## Further improvements
54
55After you have solved the exercise, please consider using the additional utilities, described in the [installation guide](https://exercism.io/tracks/rust/installation), to further refine your final solution.
56
57To format your solution, inside the solution directory use
58
59```bash
60cargo fmt
61```
62
63To see, if your solution contains some common ineffective use cases, inside the solution directory use
64
65```bash
66cargo clippy --all-targets
67```
68
69## Submitting the solution
70
71Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
72
73## Feedback, Issues, Pull Requests
74
75The [exercism/rust](https://github.com/exercism/rust) repository on GitHub is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
76
77If you want to know more about Exercism, take a look at the [contribution guide](https://github.com/exercism/docs/blob/master/contributing-to-language-tracks/README.md).
78
79[help-page]: https://exercism.io/tracks/rust/learning
80[modules]: https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html
81[cargo]: https://doc.rust-lang.org/book/ch14-00-more-about-cargo.html
82[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
83
84## Source
85
86Chapter 9 in Chris Pine's online Learn to Program tutorial. [http://pine.fm/LearnToProgram/?Chapter=09](http://pine.fm/LearnToProgram/?Chapter=09)
87
88## Submitting Incomplete Solutions
89It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/gigasecond/src/lib.rs b/gigasecond/src/lib.rs
new file mode 100644
index 0000000..0abd90b
--- /dev/null
+++ b/gigasecond/src/lib.rs
@@ -0,0 +1,7 @@
1use chrono::{DateTime, Duration, Utc};
2
3// Returns a Utc DateTime one billion seconds after start.
4pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
5 let gigasecond = Duration::seconds(1_000_000_000);
6 start + gigasecond
7}
diff --git a/gigasecond/tests/gigasecond.rs b/gigasecond/tests/gigasecond.rs
new file mode 100644
index 0000000..4fe139b
--- /dev/null
+++ b/gigasecond/tests/gigasecond.rs
@@ -0,0 +1,51 @@
1use chrono::{TimeZone, Utc};
2
3#[test]
4fn test_date() {
5 let start_date = Utc.ymd(2011, 4, 25).and_hms(0, 0, 0);
6
7 assert_eq!(
8 gigasecond::after(start_date),
9 Utc.ymd(2043, 1, 1).and_hms(1, 46, 40)
10 );
11}
12
13#[test]
14fn test_another_date() {
15 let start_date = Utc.ymd(1977, 6, 13).and_hms(0, 0, 0);
16
17 assert_eq!(
18 gigasecond::after(start_date),
19 Utc.ymd(2009, 2, 19).and_hms(1, 46, 40)
20 );
21}
22
23#[test]
24fn test_third_date() {
25 let start_date = Utc.ymd(1959, 7, 19).and_hms(0, 0, 0);
26
27 assert_eq!(
28 gigasecond::after(start_date),
29 Utc.ymd(1991, 3, 27).and_hms(1, 46, 40)
30 );
31}
32
33#[test]
34fn test_datetime() {
35 let start_date = Utc.ymd(2015, 1, 24).and_hms(22, 0, 0);
36
37 assert_eq!(
38 gigasecond::after(start_date),
39 Utc.ymd(2046, 10, 2).and_hms(23, 46, 40)
40 );
41}
42
43#[test]
44fn test_another_datetime() {
45 let start_date = Utc.ymd(2015, 1, 24).and_hms(23, 59, 59);
46
47 assert_eq!(
48 gigasecond::after(start_date),
49 Utc.ymd(2046, 10, 3).and_hms(1, 46, 39)
50 );
51}