-
Notifications
You must be signed in to change notification settings - Fork 22
120_time - added time exercise #114
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
Open
Vaibhavsg17
wants to merge
8
commits into
zhravan:main
Choose a base branch
from
Vaibhavsg17:fest/time
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7ce3e8b
added time exercise
Vaibhavsg17 c16ae01
Update catalog.yaml
Vaibhavsg17 8e6222d
Update time.go
Vaibhavsg17 98cf440
Update time.go
Vaibhavsg17 11b5dba
Update time_test.go
Vaibhavsg17 b614259
Update time_test.go
Vaibhavsg17 fd34e82
Merge branch 'main' into fest/time
Vaibhavsg17 981ca75
Update time_test.go
Vaibhavsg17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,50 @@ | ||
| package time | ||
|
|
||
| import "time" | ||
|
|
||
| // FormatDate formats a time.Time into "YYYY-MM-DD". | ||
| func FormatDate(t time.Time) string { | ||
| return t.Format("2006-01-02") | ||
| } | ||
|
|
||
| // FormatDateTime formats a time.Time into "YYYY-MM-DD HH:MM:SS". | ||
| func FormatDateTime(t time.Time) string { | ||
| return t.Format("2006-01-02 15:04:05") | ||
| } | ||
|
|
||
| // DaysBetween returns the number of days between two dates. | ||
| // Positive if end > start, negative if end < start. | ||
| // | ||
| // Note: This implementation divides the duration by 24 hours. | ||
| // When dates span a daylight saving time (DST) transition, the result | ||
| // may be off by ±1 hour. For exact day counts across DST, consider | ||
| // normalizing both dates to UTC or comparing date components at midnight. | ||
| func DaysBetween(start, end time.Time) int { | ||
| return int(end.Sub(start).Hours() / 24) | ||
| } | ||
|
|
||
|
|
||
| // AddDays adds a specified number of days to a date and returns the new date. | ||
| func AddDays(t time.Time, days int) time.Time { | ||
| return t.AddDate(0, 0, days) | ||
| } | ||
|
|
||
| // ParseDate parses a string in "YYYY-MM-DD" format into a time.Time. | ||
| // Returns zero time if parsing fails. | ||
| func ParseDate(s string) time.Time { | ||
| t, err := time.Parse("2006-01-02", s) | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t | ||
| } | ||
|
|
||
| // ParseDateTime parses a string in "YYYY-MM-DD HH:MM:SS" format into a time.Time. | ||
| // Returns zero time if parsing fails. | ||
| func ParseDateTime(s string) time.Time { | ||
| t, err := time.Parse("2006-01-02 15:04:05", s) | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t | ||
| } | ||
This file contains hidden or 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,44 @@ | ||
| package timeutils | ||
|
|
||
| import "time" | ||
|
|
||
| // FormatDate formats a time.Time into "YYYY-MM-DD". | ||
| func FormatDate(t time.Time) string { | ||
| return t.Format("2006-01-02") | ||
| } | ||
|
|
||
| // FormatDateTime formats a time.Time into "YYYY-MM-DD HH:MM:SS". | ||
| func FormatDateTime(t time.Time) string { | ||
| return t.Format("2006-01-02 15:04:05") | ||
| } | ||
|
|
||
| // DaysBetween returns the number of days between two dates. | ||
| // Positive if end > start, negative if end < start. | ||
| func DaysBetween(start, end time.Time) int { | ||
| return int(end.Sub(start).Hours() / 24) | ||
| } | ||
|
|
||
| // AddDays adds a specified number of days to a date and returns the new date. | ||
| func AddDays(t time.Time, days int) time.Time { | ||
| return t.AddDate(0, 0, days) | ||
| } | ||
|
|
||
| // ParseDate parses a string in "YYYY-MM-DD" format into a time.Time. | ||
| // Returns zero time if parsing fails. | ||
| func ParseDate(s string) time.Time { | ||
| t, err := time.Parse("2006-01-02", s) | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t | ||
| } | ||
|
|
||
| // ParseDateTime parses a string in "YYYY-MM-DD HH:MM:SS" format into a time.Time. | ||
| // Returns zero time if parsing fails. | ||
| func ParseDateTime(s string) time.Time { | ||
| t, err := time.Parse("2006-01-02 15:04:05", s) | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t | ||
| } |
This file contains hidden or 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,82 @@ | ||
| package timeutils_test | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| timeutils "github.com/zhravan/golearn/internal/exercises/templates/120_time" | ||
| ) | ||
|
|
||
| // Helper to quickly create a date | ||
| func mustDate(year int, month time.Month, day int) time.Time { | ||
| return time.Date(year, month, day, 0, 0, 0, 0, time.UTC) | ||
| } | ||
|
|
||
| // --- Tests for FormatDate --- | ||
| func TestFormatDate(t *testing.T) { | ||
| d := mustDate(2025, time.October, 2) | ||
| want := "2025-10-02" | ||
| got := timeutils.FormatDate(d) | ||
| if got != want { | ||
| t.Fatalf("FormatDate(%v) = %q; want %q", d, got, want) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for FormatDateTime --- | ||
| func TestFormatDateTime(t *testing.T) { | ||
| d := mustDate(2025, time.October, 2) | ||
| want := "2025-10-02 00:00:00" | ||
| got := timeutils.FormatDateTime(d) | ||
| if got != want { | ||
| t.Fatalf("FormatDateTime(%v) = %q; want %q", d, got, want) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for DaysBetween --- | ||
| func TestDaysBetween(t *testing.T) { | ||
| start := mustDate(2025, time.October, 1) | ||
| end := mustDate(2025, time.October, 11) | ||
|
|
||
| got := timeutils.DaysBetween(start, end) | ||
| want := 10 | ||
| if got != want { | ||
| t.Fatalf("DaysBetween(%v, %v) = %d; want %d", start, end, got, want) | ||
| } | ||
|
|
||
| // negative difference | ||
| got = timeutils.DaysBetween(end, start) | ||
| want = -10 | ||
| if got != want { | ||
| t.Fatalf("DaysBetween(%v, %v) = %d; want %d", end, start, got, want) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for AddDays --- | ||
| func TestAddDays(t *testing.T) { | ||
| d := mustDate(2025, time.October, 1) | ||
| got := timeutils.AddDays(d, 10) | ||
| want := mustDate(2025, time.October, 11) | ||
| if !got.Equal(want) { | ||
| t.Fatalf("AddDays(%v, 10) = %v; want %v", d, got, want) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for ParseDate --- | ||
| func TestParseDate(t *testing.T) { | ||
| input := "2025-10-02" | ||
| want := mustDate(2025, time.October, 2) | ||
| got := timeutils.ParseDate(input) | ||
| if !got.Equal(want) { | ||
| t.Fatalf("ParseDate(%q) = %v; want %v", input, got, want) | ||
| } | ||
| } | ||
|
|
||
| // --- Tests for ParseDateTime --- | ||
| func TestParseDateTime(t *testing.T) { | ||
| input := "2025-10-02 00:00:00" | ||
| want := mustDate(2025, time.October, 2) | ||
| got := timeutils.ParseDateTime(input) | ||
| if !got.Equal(want) { | ||
| t.Fatalf("ParseDateTime(%q) = %v; want %v", input, got, want) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.