-
-
Notifications
You must be signed in to change notification settings - Fork 687
[time-duration] Implement time-duration concept and time-keeper concept exercise #3127
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "blurb": "Learn how concept time and duration is implemented in Go.", | ||
| "authors": [ | ||
| "thibault2705" | ||
| ], | ||
| "contributors": [] | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||
| # About | ||||||
|
|
||||||
| In Go, time-based calculations are handled using the [`time`][time-package] package. | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Make sure there's no double spaces or trailing spaces in any of the files, please. |
||||||
| Two key types used in this exercise are: | ||||||
|
|
||||||
| - [`time.Time`][time]: represents a specific moment in time (a timestamp) | ||||||
| - [`time.Duration`][duration]: represents the amount of time elapsed between two moments | ||||||
|
|
||||||
| --- | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove all of these. They're not helpful for formatting. |
||||||
|
|
||||||
| ## `time.Time` vs `time.Duration` | ||||||
|
|
||||||
| A `time.Time` represents an absolute point in time, such as: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These examples don't illustrate the concept of what a point of time is vs a duration. The code could be helpful but you're introducing a new concept; the prose should explain the concept. |
||||||
|
|
||||||
| ```go | ||||||
| t := time.Now() | ||||||
| ``` | ||||||
|
|
||||||
| A `time.Duration` represents the difference between two points in time: | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be a blank line before codeblocks and lists. |
||||||
| ```go | ||||||
| start := time.Now() | ||||||
| end := start.Add(5 * time.Minute) | ||||||
|
|
||||||
| duration := end.Sub(start) // => 5m0s | ||||||
| ``` | ||||||
|
|
||||||
| In short: | ||||||
| - `time.Time` → “when something happens” | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please fix these quotes |
||||||
| - `time.Duration` → “how long it lasts” | ||||||
|
|
||||||
| ## Measuring Time with `time.Since` | ||||||
| To measure how long something takes (e.g., execution time), you can use `time.Since`[since] | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Methods should be shown with Please explain concepts with English, not just code samples. |
||||||
| ```go | ||||||
| start := time.Now() | ||||||
|
|
||||||
| // some work here | ||||||
|
|
||||||
| elapsed := time.Since(start) | ||||||
| fmt.Println(elapsed) | ||||||
| ``` | ||||||
|
|
||||||
| ## Adding and Subtracting Time | ||||||
| You can manipulate timestamps using: | ||||||
| - [`Time.Add()`][add] → add or subtract a duration | ||||||
| - [`Time.Sub()`][sub] → calculate the difference between two times | ||||||
|
|
||||||
| ## Time Units | ||||||
| Go provides built-in constants for time units: | ||||||
|
|
||||||
| | Unit | Constant | | ||||||
| | ----------- |----------------------| | ||||||
| |Nanosecond | `time.Nanosecond` | | ||||||
| |Microsecond | `time.Microsecond ` | | ||||||
| |Millisecond | `time.Millisecond` | | ||||||
| |Second | `time.Second` | | ||||||
| |Minute | `time.Minute` | | ||||||
| |Hour | `time.Hour` | | ||||||
|
|
||||||
| You can combine them to build durations: | ||||||
| ```go | ||||||
| d := 1*time.Hour + 5*time.Minute + 19*time.Second + 234*time.Millisecond | ||||||
| ``` | ||||||
|
|
||||||
| You can also perform calculations: | ||||||
| ```go | ||||||
| seconds := d.Seconds() // float64 | ||||||
| minutes := d.Minutes() | ||||||
| ``` | ||||||
|
|
||||||
| ## Working with Durations | ||||||
| Durations can be: | ||||||
| - Added to a `time.Time` | ||||||
| - Subtracted from a `time.Time` | ||||||
| - Compared or converted to different units | ||||||
|
|
||||||
| ```go | ||||||
| start := time.Now() | ||||||
| finish := start.Add(30 * time.Second) | ||||||
|
|
||||||
| duration := finish.Sub(start) // => 30s | ||||||
| ``` | ||||||
|
|
||||||
| ## Other resources | ||||||
| - [Go by Example: Time](https://gobyexample.com/time) | ||||||
| - [Go by Example: Time Formatting / Parsing](https://gobyexample.com/time-formatting-parsing) | ||||||
|
|
||||||
| [time-package]: https://pkg.go.dev/time | ||||||
| [time]: https://pkg.go.dev/time#Time | ||||||
| [duration]: https://pkg.go.dev/time#Duration | ||||||
| [since]: https://pkg.go.dev/time#Since | ||||||
| [add]: https://pkg.go.dev/time#Time.Add | ||||||
| [sub]: https://pkg.go.dev/time#Time.Sub | ||||||
| [now]: https://pkg.go.dev/time#Now | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Introduction | ||
|
|
||
| In Go, time-based calculations are handled using the [`time`][time-package] package. | ||
| Two key types used in this exercise are: | ||
|
|
||
| - [`time.Time`][time]: represents a specific moment in time (a timestamp) | ||
| - [`time.Duration`][duration]: represents the amount of time elapsed between two moments | ||
|
|
||
| --- | ||
|
|
||
| ## `time.Time` vs `time.Duration` | ||
|
|
||
| A `time.Time` represents an absolute point in time, such as: | ||
|
|
||
| ```go | ||
| t := time.Now() | ||
| ``` | ||
|
|
||
| A `time.Duration` represents the difference between two points in time: | ||
| ```go | ||
| start := time.Now() | ||
| end := start.Add(5 * time.Minute) | ||
|
|
||
| duration := end.Sub(start) // => 5m0s | ||
| ``` | ||
|
|
||
| In short: | ||
| - `time.Time` → “when something happens” | ||
| - `time.Duration` → “how long it lasts” | ||
|
|
||
| ## Measuring Time with `time.Since` | ||
| To measure how long something takes (e.g., execution time), you can use `time.Since`[since] | ||
| ```go | ||
| start := time.Now() | ||
|
|
||
| // some work here | ||
|
|
||
| elapsed := time.Since(start) | ||
| fmt.Println(elapsed) | ||
| ``` | ||
|
|
||
| ## Adding and Subtracting Time | ||
| You can manipulate timestamps using: | ||
| - [`Time.Add()`][add] → add or subtract a duration | ||
| - [`Time.Sub()`][sub] → calculate the difference between two times | ||
|
|
||
| ## Time Units | ||
| Go provides built-in constants for time units: | ||
|
|
||
| | Unit | Constant | | ||
| | ----------- |----------------------| | ||
| |Nanosecond | `time.Nanosecond` | | ||
| |Microsecond | `time.Microsecond ` | | ||
| |Millisecond | `time.Millisecond` | | ||
| |Second | `time.Second` | | ||
| |Minute | `time.Minute` | | ||
| |Hour | `time.Hour` | | ||
|
|
||
| You can combine them to build durations: | ||
| ```go | ||
| d := 1*time.Hour + 5*time.Minute + 19*time.Second + 234*time.Millisecond | ||
| ``` | ||
|
|
||
| You can also perform calculations: | ||
| ```go | ||
| seconds := d.Seconds() // float64 | ||
| minutes := d.Minutes() | ||
| ``` | ||
|
|
||
| ## Working with Durations | ||
| Durations can be: | ||
| - Added to a `time.Time` | ||
| - Subtracted from a `time.Time` | ||
| - Compared or converted to different units | ||
|
|
||
| ```go | ||
| start := time.Now() | ||
| finish := start.Add(30 * time.Second) | ||
|
|
||
| duration := finish.Sub(start) // => 30s | ||
| ``` | ||
|
|
||
| ## Other resources | ||
| - [Go by Example: Time](https://gobyexample.com/time) | ||
| - [Go by Example: Time Formatting / Parsing](https://gobyexample.com/time-formatting-parsing) | ||
|
|
||
| [time-package]: https://pkg.go.dev/time | ||
| [time]: https://pkg.go.dev/time#Time | ||
| [duration]: https://pkg.go.dev/time#Duration | ||
| [since]: https://pkg.go.dev/time#Since | ||
| [add]: https://pkg.go.dev/time#Time.Add | ||
| [sub]: https://pkg.go.dev/time#Time.Sub | ||
| [now]: https://pkg.go.dev/time#Now | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| [ | ||
| { | ||
| "url": "https://pkg.go.dev/time", | ||
| "description": "Go packages: time package" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Time", | ||
| "description": "Go packages: time.Time type" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Duration", | ||
| "description": "Go packages: time.Duration type" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Since", | ||
| "description": "Go packages: time.Since function" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Time.Add", | ||
| "description": "Go packages: time.Time.Add method" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Time.Sub", | ||
| "description": "Go packages: time.Time.Sub method" | ||
| }, | ||
| { | ||
| "url": "https://pkg.go.dev/time#Now", | ||
| "description": "Go packages: time.Now function" | ||
| }, | ||
| { | ||
| "url": "https://gobyexample.com/time", | ||
| "description": "Go by Example: Time" | ||
| }, | ||
| { | ||
| "url": "https://gobyexample.com/time-formatting-parsing", | ||
| "description": "Go by Example: Time Formatting / Parsing" | ||
| } | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| # Hints | ||
|
|
||
| ## 1. Calculate Duration | ||
|
|
||
| Think about how to measure the time between two moments. | ||
| Go provides a built-in way to compare timestamps directly. | ||
|
|
||
| --- | ||
|
|
||
| ## 2. Handle Different Time Zones | ||
|
|
||
| You don’t need to manually adjust for time zones. | ||
| Focus on comparing the two timestamps as they are. | ||
|
|
||
| --- | ||
|
|
||
| ## 3. Parse Recorded Times | ||
|
|
||
| The input follows a structured format with hours, minutes, and seconds. | ||
| Look for a standard library function that can interpret duration strings. | ||
|
|
||
| --- | ||
|
|
||
| ## 4. Correct Start Time | ||
|
|
||
| You need to adjust a timestamp by moving it backward in time. | ||
| Consider how to shift a time value using a duration. | ||
|
|
||
| --- | ||
|
|
||
| ## 5. Filter Valid Detections | ||
|
|
||
| First determine how much time has passed since the start. | ||
| Then check whether that elapsed time falls within a given range. | ||
|
|
||
| --- | ||
|
|
||
| ## 6. Format Leaderboard Output | ||
|
|
||
| You need to display: | ||
| - the runner’s total time | ||
| - the difference compared to the leader | ||
|
|
||
| Think about: | ||
| - how to convert a duration to a readable string | ||
| - how to express the difference in seconds with precision | ||
|
|
||
| --- | ||
|
|
||
| ## General Advice | ||
|
|
||
| - Prefer built-in time utilities instead of manual calculations | ||
| - Be careful with boundaries when checking ranges | ||
| - Keep your solution simple and precise — timing systems must be reliable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please fix the grammar here