-
Notifications
You must be signed in to change notification settings - Fork 20
added epoch #89
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
added epoch #89
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -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() | ||||||||
| } | ||||||||
|
Comment on lines
+47
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error handling creates ambiguity. Returning Consider one of these approaches: Option 1: Return error as second value (idiomatic Go): -func TimeToEpoch(input string) int64 {
+func TimeToEpoch(input string) (int64, error) {
t, err := time.ParseInLocation("2006-01-02 15:04:05", input, time.UTC)
if err != nil {
- return 0
+ return 0, err
}
- return t.Unix()
+ return t.Unix(), nil
}Option 2: Use sentinel value (if constrained by test signature): func TimeToEpoch(input string) int64 {
t, err := time.ParseInLocation("2006-01-02 15:04:05", input, time.UTC)
if err != nil {
- return 0
+ return -1 // or panic/log the error
}
return t.Unix()
}
🤖 Prompt for AI Agents |
||||||||
|
|
||||||||
|
|
||||||||
| // 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(), | ||||||||
| } | ||||||||
| } | ||||||||
|
Comment on lines
+57
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical logic error in NowFormats. The Apply this diff: 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()),
+ "UnixSeconds": now.Format("2006-01-02 15:04:05 +0000 UTC"),
+ "UnixSecondsRaw": now.Format("2006-01-02 15:04:05"),
+ "UnixSecondsInt": fmt.Sprintf("%d", now.Unix()),
+ "UnixMilliseconds": fmt.Sprintf("%d", now.UnixMilli()),
+ "UnixNanoseconds": fmt.Sprintf("%d", now.UnixNano()),
"FormattedFull": GetCurrentFormattedTime(),
"FormattedSimple": GetCurrentFormattedTimeSimple(),
}
}You'll also need to import
🤖 Prompt for AI Agents |
||||||||
|
|
||||||||
| // Helper: format int64 as string | ||||||||
| func formatInt(val int64) string { | ||||||||
| return time.Unix(0, val).UTC().Format("2006-01-02 15:04:05") | ||||||||
| } | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove or fix the formatInt helper. This helper incorrectly treats all int64 values as nanoseconds in -// Helper: format int64 as string
-func formatInt(val int64) string {
- return time.Unix(0, val).UTC().Format("2006-01-02 15:04:05")
-}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add fmt import for numeric string conversions.
To implement the fixes suggested for
NowFormats(), you'll need to add thefmtpackage import.import ( + "fmt" "time" )📝 Committable suggestion
🤖 Prompt for AI Agents