Skip to content
Open
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
7 changes: 7 additions & 0 deletions concepts/time-duration/.meta/config.json
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.",
Copy link
Copy Markdown
Member

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

"authors": [
"thibault2705"
],
"contributors": []
}
94 changes: 94 additions & 0 deletions concepts/time-duration/about.md
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.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In Go, time-based calculations are handled using the [`time`][time-package] package.
In Go, time-based calculations are handled using the [`time`][time-package] package.

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

---
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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”
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Methods should be shown with () to distinguish them from types.

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

94 changes: 94 additions & 0 deletions concepts/time-duration/introduction.md
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

38 changes: 38 additions & 0 deletions concepts/time-duration/links.json
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"
}
]
16 changes: 16 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,17 @@
"methods",
"structs"
]
},
{
"slug": "time-keeper",
"name": "Time Keeper",
"uuid": "82abf57d-891b-484f-b4d8-309e1486a6f5",
"concepts": [
"time-duration"
],
"prerequisites": [
"time"
]
}
],
"practice": [
Expand Down Expand Up @@ -2530,6 +2541,11 @@
"uuid": "f22b0e85-4d86-4ecf-9415-ea27c93ebcd6",
"slug": "error-wrapping",
"name": "Error Wrapping"
},
{
"uuid": "32ee0ee7-7191-4d21-aa8b-f912cae21ca7",
"slug": "time-duration",
"name": "Time Duration"
}
],
"key_features": [
Expand Down
54 changes: 54 additions & 0 deletions exercises/concept/time-keeper/.docs/hints.md
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
Loading
Loading