Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust gigasecond #221

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
- [simple-linked-list](./simple-linked-list/README.md)
- [binary-search](./binary-search/README.md)
- [grains](./grains/README.md)
- [gigasecond](./gigasecond/README.md)
45 changes: 45 additions & 0 deletions rust/gigasecond/.exercism/config.json
Original file line number Diff line number Diff line change
@@ -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": "http://pine.fm/LearnToProgram/?Chapter=09"
}
1 change: 1 addition & 0 deletions rust/gigasecond/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"rust","exercise":"gigasecond","id":"03c05905bbd248af9510534592d03c9e","url":"https://exercism.org/tracks/rust/exercises/gigasecond","handle":"vpayno","is_requester":true,"auto_approve":false}
8 changes: 8 additions & 0 deletions rust/gigasecond/.gitignore
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions rust/gigasecond/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
edition = "2021"
name = "gigasecond"
version = "2.0.0"

[dependencies]
time = "0.3"
85 changes: 85 additions & 0 deletions rust/gigasecond/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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)
- [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
81 changes: 81 additions & 0 deletions rust/gigasecond/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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. - http://pine.fm/LearnToProgram/?Chapter=09

### My Solution

- [my solution](./src/lib.rs)
- [run-tests](./run-tests-rust.txt)
35 changes: 35 additions & 0 deletions rust/gigasecond/coverage-annotations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
1| |//! Exercise Url: <https://exercism.org/tracks/rust/exercises/gigasecond>
2| |
3| |use time::Duration;
4| |
5| |use time::PrimitiveDateTime as DateTime;
6| |
7| |const GIGASECOND: i64 = 1_000_000_000;
8| |
9| |/// Returns a DateTime one billion seconds after start.
10| |///
11| |/// Example
12| |///
13| |/// ```rust
14| |/// use gigasecond::after;
15| |/// use time::PrimitiveDateTime as DateTime;
16| |/// use time::{Date,Time};
17| |///
18| |/// let want = DateTime::new(Date::from_calendar_date(2055, 5.try_into().unwrap(), 8).unwrap(), Time::from_hms(22, 43, 40).unwrap());
19| |///
20| |/// let dt = DateTime::new(Date::from_calendar_date(2023, 8.try_into().unwrap(), 30).unwrap(), Time::from_hms(20, 57, 00).unwrap());
21| |/// let got = after(dt);
22| |///
23| |/// assert_eq!(got, want);
24| |/// ```
25| 15|pub fn after(start: DateTime) -> DateTime {
26| 15| start + Duration::seconds(GIGASECOND)
27| 15|}
------------------
| gigasecond::after:
| 25| 15|pub fn after(start: DateTime) -> DateTime {
| 26| 15| start + Duration::seconds(GIGASECOND)
| 27| 15|}
------------------
| Unexecuted instantiation: gigasecond::after
------------------
8 changes: 8 additions & 0 deletions rust/gigasecond/coverage-missing-lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Running: cargo llvm-cov --no-clean --all-features --workspace --show-missing-lines

Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/home/vpayno/git_vpayno/exercism-workspace/rust/gigasecond/src/lib.rs 1 0 100.00% 1 0 100.00% 3 0 100.00% 0 0 -
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 1 0 100.00% 1 0 100.00% 3 0 100.00% 0 0 -

15 changes: 15 additions & 0 deletions rust/gigasecond/report.lcov
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SF:/home/vpayno/git_vpayno/exercism-workspace/rust/gigasecond/src/lib.rs
FN:25,_RNvCshHO3K2riiE2_10gigasecond5after
FN:25,_RNvCsaitPXO6a1LQ_10gigasecond5after
FNDA:5,_RNvCshHO3K2riiE2_10gigasecond5after
FNDA:0,_RNvCsaitPXO6a1LQ_10gigasecond5after
FNF:1
FNH:1
DA:25,5
DA:26,5
DA:27,5
BRF:0
BRH:0
LF:3
LH:3
end_of_record
Loading