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/solutions/36_epoch/epoch.go b/internal/exercises/solutions/36_epoch/epoch.go new file mode 100644 index 0000000..a61496f --- /dev/null +++ b/internal/exercises/solutions/36_epoch/epoch.go @@ -0,0 +1,74 @@ +package epoch + +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 { + 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 { + t, err := time.ParseInLocation("2006-01-02 15:04:05", input, time.UTC) + if err != nil { + return 0 + } + return t.Unix() +} + + +// 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") +} \ No newline at end of file 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..f841834 --- /dev/null +++ b/internal/exercises/templates/36_epoch/epoch_test.go @@ -0,0 +1,20 @@ +package epoch + +import "testing" + +func TestEpochToTime(t *testing.T) { + 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(%d) = %q, want %q", epoch, got, want) + } +} +func TestTimeToEpoch(t *testing.T) { + const input = "2021-10-01 00:00:00" + const want = int64(1633046400) + got := TimeToEpoch(input) + if got != want { + t.Fatalf("TimeToEpoch(%q) = %d, want %d", input, got, want) + } +} \ No newline at end of file