Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
.DS_Store
*~
vendor
.idea/
37 changes: 37 additions & 0 deletions convert/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,40 @@ func Float32ToString(f float32) string {
func Float64ToString(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64)
}

func TimeToRFC3339(t time.Time) string {
return t.Format(time.RFC3339)
}

func TimePToRFC3339(t *time.Time) string {
if t == nil {
return ""
}
return TimeToRFC3339(*t)
}

func TimeStrToRFC3339(s string, sourceLayout string) (string, error) {
return ConvertTimeStrToAnotherLayout(s, sourceLayout, time.RFC3339)
}

func TimeToStr(t time.Time, targetLayout string, withTZ bool) string {
if withTZ {
return t.Format(targetLayout)
}
return t.Format("2006-01-02T15:04:05")
}

func ConvertTimeStrToAnotherLayout(s string, sourceLayout string, targetLayout string) (string, error) {
t, err := time.Parse(sourceLayout, s)
if err == nil {
return t.Format(targetLayout), nil
}
return "", err
}

func TimePToStr(t *time.Time, targetLayout string, withTZ bool) string {
if t == nil {
return ""
}
return TimeToStr(*t, targetLayout, withTZ)
}