From 87ec2d6562637c2b1499ec27e32c559b106c7929 Mon Sep 17 00:00:00 2001 From: Omesh Mehta Date: Mon, 29 Sep 2025 16:02:02 +0530 Subject: [PATCH 1/5] added epoch --- internal/exercises/catalog.yaml | 8 ++++++++ .../exercises/templates/36_epoch/epoch.go | 15 +++++++++++++++ .../templates/36_epoch/epoch_test.go | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 internal/exercises/templates/36_epoch/epoch.go create mode 100644 internal/exercises/templates/36_epoch/epoch_test.go diff --git a/internal/exercises/catalog.yaml b/internal/exercises/catalog.yaml index 9ec84ce..f44cfd4 100644 --- a/internal/exercises/catalog.yaml +++ b/internal/exercises/catalog.yaml @@ -177,3 +177,11 @@ projects: hints: - Implement an in-memory key-value store with basic CRUD operations and optional persistence. +- slug: 36_epoch + title: "Epoch Conversion" + difficulty: beginner + topics: ["time", "epoch", "unix"] + hints: + - "Use Go's `time.Unix()` to convert an epoch to time." + - "Use `t.Unix()` to convert time back to epoch." + - "Remember Go’s `time.Parse` can help parse date strings." diff --git a/internal/exercises/templates/36_epoch/epoch.go b/internal/exercises/templates/36_epoch/epoch.go new file mode 100644 index 0000000..d6253ce --- /dev/null +++ b/internal/exercises/templates/36_epoch/epoch.go @@ -0,0 +1,15 @@ +package epoch + +// TODO: Implement these functions so tests pass + +// EpochToTime converts a unix epoch (seconds) into a formatted time string "2006-01-02 15:04:05". +func EpochToTime(epoch int64) string { + // Intentionally wrong to simulate failing exercise + return "" +} + +// TimeToEpoch converts a formatted time string "2006-01-02 15:04:05" into a unix epoch (seconds). +func TimeToEpoch(input string) int64 { + // Intentionally wrong to simulate failing exercise + return 0 +} diff --git a/internal/exercises/templates/36_epoch/epoch_test.go b/internal/exercises/templates/36_epoch/epoch_test.go new file mode 100644 index 0000000..9031ea6 --- /dev/null +++ b/internal/exercises/templates/36_epoch/epoch_test.go @@ -0,0 +1,19 @@ +package epoch + +import "testing" + +func TestEpochToTime(t *testing.T) { + got := EpochToTime(1633024800) // 2021-10-01 00:00:00 UTC + want := "2021-10-01 00:00:00" + if got != want { + t.Fatalf("EpochToTime(1633024800) = %q, want %q", got, want) + } +} + +func TestTimeToEpoch(t *testing.T) { + got := TimeToEpoch("2021-10-01 00:00:00") + want := int64(1633024800) + if got != want { + t.Fatalf("TimeToEpoch(...) = %d, want %d", got, want) + } +} From f58caa6b9c23f3b2cb4e4f680c95798a94543dce Mon Sep 17 00:00:00 2001 From: Omesh Mehta Date: Mon, 29 Sep 2025 16:41:46 +0530 Subject: [PATCH 2/5] updated epoch segment --- .../exercises/templates/36_epoch/epoch_test.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/exercises/templates/36_epoch/epoch_test.go b/internal/exercises/templates/36_epoch/epoch_test.go index 9031ea6..f841834 100644 --- a/internal/exercises/templates/36_epoch/epoch_test.go +++ b/internal/exercises/templates/36_epoch/epoch_test.go @@ -3,17 +3,18 @@ package epoch import "testing" func TestEpochToTime(t *testing.T) { - got := EpochToTime(1633024800) // 2021-10-01 00:00:00 UTC - want := "2021-10-01 00:00:00" + const want = "2021-10-01 00:00:00" + const epoch = int64(1633046400) // 2021-10-01 00:00:00 UTC + got := EpochToTime(epoch) if got != want { - t.Fatalf("EpochToTime(1633024800) = %q, want %q", got, want) + t.Fatalf("EpochToTime(%d) = %q, want %q", epoch, got, want) } } - func TestTimeToEpoch(t *testing.T) { - got := TimeToEpoch("2021-10-01 00:00:00") - want := int64(1633024800) + const input = "2021-10-01 00:00:00" + const want = int64(1633046400) + got := TimeToEpoch(input) if got != want { - t.Fatalf("TimeToEpoch(...) = %d, want %d", got, want) + t.Fatalf("TimeToEpoch(%q) = %d, want %d", input, got, want) } -} +} \ No newline at end of file From 23cb7c0c393e98997d04dabac7cf783e6f1c754d Mon Sep 17 00:00:00 2001 From: Omesh Mehta Date: Mon, 29 Sep 2025 20:57:20 +0530 Subject: [PATCH 3/5] epoch solution added --- .../exercises/solutions/36_epoch/epoch.go | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 internal/exercises/solutions/36_epoch/epoch.go diff --git a/internal/exercises/solutions/36_epoch/epoch.go b/internal/exercises/solutions/36_epoch/epoch.go new file mode 100644 index 0000000..041116b --- /dev/null +++ b/internal/exercises/solutions/36_epoch/epoch.go @@ -0,0 +1,25 @@ +package epoch + +import ( + "time" +) + +// EpochToTime converts a unix epoch (seconds) into a formatted time string "2006-01-02 15:04:05". +func EpochToTime(epoch int64) string { + // Convert the epoch to a time.Time object and set it to UTC before formatting. + t := time.Unix(epoch, 0).UTC() + return t.Format("2006-01-02 15:04:05") +} + +// TimeToEpoch converts a formatted time string "2006-01-02 15:04:05" into a unix epoch (seconds). +// It assumes the input string represents a time in UTC. +func TimeToEpoch(input string) int64 { + // Use time.ParseInLocation to explicitly parse the string as a UTC time. + t, err := time.ParseInLocation("2006-01-02 15:04:05", input, time.UTC) + if err != nil { + // In a real application, you might log the error here. + return 0 + } + // t.Unix() returns the Unix epoch seconds. + return t.Unix() +} \ No newline at end of file From 965651f50d3351a66fafd96e5b937bee93d8df07 Mon Sep 17 00:00:00 2001 From: Omesh Mehta Date: Mon, 29 Sep 2025 23:20:12 +0530 Subject: [PATCH 4/5] ALL THE FORMATS HAVE BEEN ADDED FOR EPOCH MODULE --- .../exercises/solutions/36_epoch/epoch.go | 59 +++++++++++++++++-- 1 file changed, 54 insertions(+), 5 deletions(-) diff --git a/internal/exercises/solutions/36_epoch/epoch.go b/internal/exercises/solutions/36_epoch/epoch.go index 041116b..23e11ea 100644 --- a/internal/exercises/solutions/36_epoch/epoch.go +++ b/internal/exercises/solutions/36_epoch/epoch.go @@ -4,9 +4,40 @@ import ( "time" ) +// --- Getting Current Time --- + +// GetCurrentUnixSeconds returns the current time as a Unix epoch in seconds (int64). +func GetCurrentUnixSeconds() int64 { + return time.Now().Unix() +} + +// GetCurrentUnixMilliseconds returns the current time as a Unix epoch in milliseconds (int64). +func GetCurrentUnixMilliseconds() int64 { + return time.Now().UnixNano() / int64(time.Millisecond) +} + +// GetCurrentUnixNanoseconds returns the current time as a Unix epoch in nanoseconds (int64). +func GetCurrentUnixNanoseconds() int64 { + return time.Now().UnixNano() +} + +// GetCurrentFormattedTime returns the current time in the +// "2006-01-02 15:04:05.000000 +0000 UTC" format (microsecond precision). +func GetCurrentFormattedTime() string { + return time.Now().UTC().Format("2006-01-02 15:04:05.000000 +0000 UTC") +} + +// GetCurrentFormattedTimeSimple returns the current time in the +// "2006-01-02 15:04:05 +0000 UTC" format (no microseconds). +func GetCurrentFormattedTimeSimple() string { + return time.Now().UTC().Format("2006-01-02 15:04:05 +0000 UTC") +} + + + // EpochToTime converts a unix epoch (seconds) into a formatted time string "2006-01-02 15:04:05". +// The resulting time is in UTC. func EpochToTime(epoch int64) string { - // Convert the epoch to a time.Time object and set it to UTC before formatting. t := time.Unix(epoch, 0).UTC() return t.Format("2006-01-02 15:04:05") } @@ -14,12 +45,30 @@ func EpochToTime(epoch int64) string { // TimeToEpoch converts a formatted time string "2006-01-02 15:04:05" into a unix epoch (seconds). // It assumes the input string represents a time in UTC. func TimeToEpoch(input string) int64 { - // Use time.ParseInLocation to explicitly parse the string as a UTC time. t, err := time.ParseInLocation("2006-01-02 15:04:05", input, time.UTC) if err != nil { - // In a real application, you might log the error here. return 0 } - // t.Unix() returns the Unix epoch seconds. return t.Unix() -} \ No newline at end of file +} + + +// NowFormats returns the current time in all supported formats. +func NowFormats() map[string]string { + now := time.Now().UTC() + return map[string]string{ + "UnixSeconds": time.Unix(now.Unix(), 0).Format("2006-01-02 15:04:05") + " (epoch: " + + time.Unix(now.Unix(), 0).UTC().Format("2006-01-02 15:04:05") + ")", + "UnixSecondsRaw": time.Unix(now.Unix(), 0).UTC().Format("2006-01-02 15:04:05"), + "UnixSecondsInt": formatInt(now.Unix()), + "UnixMilliseconds": formatInt(now.UnixMilli()), + "UnixNanoseconds": formatInt(now.UnixNano()), + "FormattedFull": GetCurrentFormattedTime(), + "FormattedSimple": GetCurrentFormattedTimeSimple(), + } +} + +// Helper: format int64 as string +func formatInt(val int64) string { + return time.Unix(0, val).UTC().Format("2006-01-02 15:04:05") +} From 6efa0d8721dbfe96f09289a11b7132ca3aa48d14 Mon Sep 17 00:00:00 2001 From: Omesh Mehta Date: Mon, 29 Sep 2025 23:30:45 +0530 Subject: [PATCH 5/5] epoch formats added --- internal/exercises/solutions/36_epoch/epoch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/exercises/solutions/36_epoch/epoch.go b/internal/exercises/solutions/36_epoch/epoch.go index 23e11ea..a61496f 100644 --- a/internal/exercises/solutions/36_epoch/epoch.go +++ b/internal/exercises/solutions/36_epoch/epoch.go @@ -71,4 +71,4 @@ func NowFormats() map[string]string { // Helper: format int64 as string func formatInt(val int64) string { return time.Unix(0, val).UTC().Format("2006-01-02 15:04:05") -} +} \ No newline at end of file