Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
Add Utility to Convert and Format Milisecond Time
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Apr 22, 2020
1 parent acae3bd commit ae9645b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import (
"gopkg.in/yaml.v2"
)

// FormatTimestampToDate takes in a timestamp in milliseconds since epoch and
// converts to a date format
func FormatTimestampToDate(timestamp int64, format string) string {
nanoSeconds := timestamp * int64(time.Millisecond)
tm := time.Unix(0, nanoSeconds)
return tm.Format(format)
}

// IsUserLoggedIn checks whether a firebase refresh token is set in the configurations
func IsUserLoggedIn() bool {
if viper.IsSet(configs.FirebaseRefreshTokenViperConfigKey) && viper.GetString(configs.FirebaseLoggedInUserEmailViperConfigKey) != "" {
Expand Down
44 changes: 44 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,47 @@ func TestUnmarshalFormatFile(t *testing.T) {
})
}
}

func TestFormatTimestampToDate(t *testing.T) {
type args struct {
timestamp int64
format string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test Date Format is Correct",
args: args{
timestamp: int64(1587541614335),
format: "02/01/2006",
},
want: "22/04/2020",
},
{
name: "Test Date Time Format is Correct",
args: args{
timestamp: int64(1587541614335),
format: "02/01/2006 15:04:05 MST",
},
want: "22/04/2020 10:46:54 EAT",
},
{
name: "Test Time Format is Correct",
args: args{
timestamp: int64(1587541614335),
format: "15:04:05 MST",
},
want: "10:46:54 EAT",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FormatTimestampToDate(tt.args.timestamp, tt.args.format); got != tt.want {
t.Errorf("FormatTimestampToDate() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit ae9645b

Please sign in to comment.