summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <sokolyuk@gmail.com>2024-05-31 23:01:55 +0200
committerDimitri Sokolyuk <sokolyuk@gmail.com>2024-05-31 23:01:55 +0200
commit0e14bbe57d66756ec08cc6e943bd730a30bcea15 (patch)
treed687e4d4c3fc3cbc0d8b44d46162e63d5831aa25
parent9eeeba3d42d44aeb2c4b7f4bafeee4ddbcbc7656 (diff)
Add gigasecond
-rw-r--r--rust/gigasecond/.exercism/config.json45
-rw-r--r--rust/gigasecond/.exercism/metadata.json1
-rw-r--r--rust/gigasecond/.gitignore8
-rw-r--r--rust/gigasecond/Cargo.toml7
-rw-r--r--rust/gigasecond/HELP.md86
-rw-r--r--rust/gigasecond/README.md76
-rw-r--r--rust/gigasecond/src/lib.rs6
-rw-r--r--rust/gigasecond/tests/gigasecond.rs52
8 files changed, 281 insertions, 0 deletions
diff --git a/rust/gigasecond/.exercism/config.json b/rust/gigasecond/.exercism/config.json
new file mode 100644
index 0000000..d3e038d
--- /dev/null
+++ b/rust/gigasecond/.exercism/config.json
@@ -0,0 +1,45 @@
+{
+ "authors": [
+ "IanWhitney"
+ ],
+ "contributors": [
+ "andy5995",
+ "ashleygwilliams",
+ "cbzehner",
+ "coriolinus",
+ "cwhakes",
+ "EduardoBautista",
+ "efx",
+ "ErikSchierboom",
+ "houhoulis",
+ "IanWhitney",
+ "janczer",
+ "leoyvens",
+ "lutostag",
+ "mkantor",
+ "nfiles",
+ "NieDzejkob",
+ "ocstl",
+ "petertseng",
+ "rofrol",
+ "sacherjj",
+ "stringparser",
+ "xakon",
+ "ZapAnton"
+ ],
+ "files": {
+ "solution": [
+ "src/lib.rs",
+ "Cargo.toml"
+ ],
+ "test": [
+ "tests/gigasecond.rs"
+ ],
+ "example": [
+ ".meta/example.rs"
+ ]
+ },
+ "blurb": "Given a moment, determine the moment that would be after a gigasecond has passed.",
+ "source": "Chapter 9 in Chris Pine's online Learn to Program tutorial.",
+ "source_url": "https://pine.fm/LearnToProgram/?Chapter=09"
+}
diff --git a/rust/gigasecond/.exercism/metadata.json b/rust/gigasecond/.exercism/metadata.json
new file mode 100644
index 0000000..8032402
--- /dev/null
+++ b/rust/gigasecond/.exercism/metadata.json
@@ -0,0 +1 @@
+{"track":"rust","exercise":"gigasecond","id":"69c72d1b1a944e2b8c849b2d9b491265","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"dim13","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/rust/gigasecond/.gitignore b/rust/gigasecond/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/gigasecond/.gitignore
@@ -0,0 +1,8 @@
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+**/*.rs.bk
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
+Cargo.lock
diff --git a/rust/gigasecond/Cargo.toml b/rust/gigasecond/Cargo.toml
new file mode 100644
index 0000000..da110b7
--- /dev/null
+++ b/rust/gigasecond/Cargo.toml
@@ -0,0 +1,7 @@
+[package]
+edition = "2021"
+name = "gigasecond"
+version = "2.0.0"
+
+[dependencies]
+time = "0.3"
diff --git a/rust/gigasecond/HELP.md b/rust/gigasecond/HELP.md
new file mode 100644
index 0000000..67add76
--- /dev/null
+++ b/rust/gigasecond/HELP.md
@@ -0,0 +1,86 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ cargo test
+```
+
+All but the first test have been ignored. After you get the first test to
+pass, open the tests source file which is located in the `tests` directory
+and remove the `#[ignore]` flag from the next test and get the tests to pass
+again. Each separate test is a function with `#[test]` flag above it.
+Continue, until you pass every test.
+
+If you wish to run _only ignored_ tests without editing the tests source file, use:
+
+```bash
+$ cargo test -- --ignored
+```
+
+If you are using Rust 1.51 or later, you can run _all_ tests with
+
+```bash
+$ cargo test -- --include-ignored
+```
+
+To run a specific test, for example `some_test`, you can use:
+
+```bash
+$ cargo test some_test
+```
+
+If the specific test is ignored, use:
+
+```bash
+$ cargo test some_test -- --ignored
+```
+
+To learn more about Rust tests refer to the online [test documentation][rust-tests].
+
+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
+- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
+- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+## Rust Installation
+
+Refer to the [exercism help page][help-page] for Rust installation and learning
+resources.
+
+## Submitting the solution
+
+Generally 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.
+
+## Feedback, Issues, Pull Requests
+
+The GitHub [track repository][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!
+
+If you want to know more about Exercism, take a look at the [contribution guide].
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
+[help-page]: https://exercism.org/tracks/rust/learning
+[github]: https://github.com/exercism/rust
+[contribution guide]: https://exercism.org/docs/community/contributors \ No newline at end of file
diff --git a/rust/gigasecond/README.md b/rust/gigasecond/README.md
new file mode 100644
index 0000000..acb6be1
--- /dev/null
+++ b/rust/gigasecond/README.md
@@ -0,0 +1,76 @@
+# Gigasecond
+
+Welcome to Gigasecond on Exercism's Rust Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Introduction
+
+The way we measure time is kind of messy.
+We have 60 seconds in a minute, and 60 minutes in an hour.
+This comes from ancient Babylon, where they used 60 as the basis for their number system.
+We have 24 hours in a day, 7 days in a week, and how many days in a month?
+Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in.
+
+What if, instead, we only use seconds to express time intervals?
+Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities.
+
+- A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that's two thousand seconds).
+- Perhaps you and your family would travel to somewhere exotic for two megaseconds (that's two million seconds).
+- And if you and your spouse were married for _a thousand million_ seconds, you would celebrate your one gigasecond anniversary.
+
+~~~~exercism/note
+If we ever colonize Mars or some other planet, measuring time is going to get even messier.
+If someone says "year" do they mean a year on Earth or a year on Mars?
+
+The idea for this exercise came from the science fiction novel ["A Deepness in the Sky"][vinge-novel] by author Vernor Vinge.
+In it the author uses the metric system as the basis for time measurements.
+
+[vinge-novel]: https://www.tor.com/2017/08/03/science-fiction-with-something-for-everyone-a-deepness-in-the-sky-by-vernor-vinge/
+~~~~
+
+## Instructions
+
+Your task is to determine the date and time one gigasecond after a certain date.
+
+A gigasecond is one thousand million seconds.
+That is a one with nine zeros after it.
+
+If you were born on _January 24th, 2015 at 22:00 (10:00:00pm)_, then you would be a gigasecond old on _October 2nd, 2046 at 23:46:40 (11:46:40pm)_.
+
+If you're unsure what operations you can perform on `PrimitiveDateTime` take a look at the [time crate](https://docs.rs/time) which is listed as a dependency in the `Cargo.toml` file for this exercise.
+
+## Source
+
+### Created by
+
+- @IanWhitney
+
+### Contributed to by
+
+- @andy5995
+- @ashleygwilliams
+- @cbzehner
+- @coriolinus
+- @cwhakes
+- @EduardoBautista
+- @efx
+- @ErikSchierboom
+- @houhoulis
+- @IanWhitney
+- @janczer
+- @leoyvens
+- @lutostag
+- @mkantor
+- @nfiles
+- @NieDzejkob
+- @ocstl
+- @petertseng
+- @rofrol
+- @sacherjj
+- @stringparser
+- @xakon
+- @ZapAnton
+
+### Based on
+
+Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/?Chapter=09 \ No newline at end of file
diff --git a/rust/gigasecond/src/lib.rs b/rust/gigasecond/src/lib.rs
new file mode 100644
index 0000000..2dbb9ab
--- /dev/null
+++ b/rust/gigasecond/src/lib.rs
@@ -0,0 +1,6 @@
+use time::PrimitiveDateTime as DateTime;
+
+// Returns a DateTime one billion seconds after start.
+pub fn after(start: DateTime) -> DateTime {
+ todo!("What time is a gigasecond later than {start}");
+}
diff --git a/rust/gigasecond/tests/gigasecond.rs b/rust/gigasecond/tests/gigasecond.rs
new file mode 100644
index 0000000..52237cf
--- /dev/null
+++ b/rust/gigasecond/tests/gigasecond.rs
@@ -0,0 +1,52 @@
+use time::PrimitiveDateTime as DateTime;
+
+/// Create a datetime from the given numeric point in time.
+///
+/// Panics if any field is invalid.
+fn dt(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime {
+ use time::{Date, Time};
+
+ DateTime::new(
+ Date::from_calendar_date(year, month.try_into().unwrap(), day).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]
+#[ignore]
+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]
+#[ignore]
+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]
+#[ignore]
+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]
+#[ignore]
+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));
+}