-
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
4 changed files
with
95 additions
and
1 deletion.
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
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,30 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
func convertUnixTimestamp(timestamp int64, format string) (string, error) { | ||
var t time.Time | ||
|
||
// Determine the precision based on the length of the timestamp | ||
switch { | ||
case timestamp >= 1e18: | ||
// Nanoseconds | ||
t = time.Unix(0, timestamp) | ||
case timestamp >= 1e15: | ||
// Microseconds | ||
t = time.UnixMicro(timestamp) | ||
case timestamp >= 1e12: | ||
// Milliseconds | ||
t = time.UnixMilli(timestamp) | ||
case timestamp >= 1e9: | ||
// Seconds | ||
t = time.Unix(timestamp, 0) | ||
default: | ||
return "", fmt.Errorf("invalid timestamp: %d", timestamp) | ||
} | ||
|
||
return t.Format(format), nil | ||
} |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestConvertUnixTimestamp(t *testing.T) { | ||
format := "2006-01-02 15:04:05.000000000" | ||
tests := []struct { | ||
timestamp int64 | ||
expected string | ||
}{ | ||
{1627670400005, "2021-07-30 20:40:00.005000000"}, | ||
{1627670400000005, "2021-07-30 20:40:00.000005000"}, | ||
{1627670400000000005, "2021-07-30 20:40:00.000000005"}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run("", func(t *testing.T) { | ||
result, err := convertUnixTimestamp(tt.timestamp, format) | ||
if err != nil { | ||
t.Errorf("unexpected error: %v", err) | ||
} | ||
if result != tt.expected { | ||
t.Errorf("convertUnixTimestamp(%d, %q) = %q; want %q", tt.timestamp, format, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestConvertUnixTimestampInvalid(t *testing.T) { | ||
format := "2006-01-02 15:04:05.999999999" | ||
_, err := convertUnixTimestamp(1627670, format) | ||
if err == nil { | ||
t.Error("expected error for invalid timestamp, got nil") | ||
} | ||
} |