-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mango | ||
|
||
// Min determines the minimum among the provided values | ||
func Min(vals ...int) int { | ||
min := vals[0] | ||
for _, val := range vals { | ||
if val < min { | ||
min = val | ||
} | ||
} | ||
return min | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mango | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFindingMin(t *testing.T) { | ||
min := Min(1, 2, 3, -594849) | ||
assert.Equal(t, -594849, min) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package mango | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
const ( | ||
DurationDay = 24 * time.Hour | ||
DurationYear = 365 * DurationDay | ||
) | ||
|
||
func FormatSimpleDate(ts *time.Time) string { | ||
if ts == nil { | ||
return "never" | ||
} | ||
return ts.Format("01-02-06") | ||
} | ||
|
||
func FormatTimeSince(ts *time.Time) string { | ||
if ts == nil { | ||
return "never" | ||
} | ||
now := time.Now() | ||
elapsed := now.Sub(*ts) | ||
return fmt.Sprintf("%v ago", formatDuration(elapsed)) | ||
} | ||
|
||
// imprecise formatting since we only care about the highest possible unit | ||
func formatDuration(d time.Duration) string { | ||
if d >= DurationYear { | ||
years := d / DurationYear | ||
return fmt.Sprintf("%dy", years) | ||
} else if d >= DurationDay { | ||
days := d / DurationDay | ||
return fmt.Sprintf("%dd", days) | ||
} else if d >= time.Hour { | ||
hours := d / time.Hour | ||
return fmt.Sprintf("%dh", hours) | ||
} else if d >= time.Minute { | ||
minutes := d / time.Minute | ||
return fmt.Sprintf("%dm", minutes) | ||
} | ||
seconds := d / time.Second | ||
return fmt.Sprintf("%ds", seconds) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mango | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestFormatSimpleDate(t *testing.T) { | ||
ts := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC) | ||
assert.Equal(t, "01-01-21", FormatSimpleDate(&ts)) | ||
} | ||
|
||
func TestFormatTimeSince(t *testing.T) { | ||
ts := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC) | ||
timeSince := FormatTimeSince(&ts) | ||
assert.NotEmpty(t, timeSince) | ||
} |