From 48a00cb4e12504302a064176d8fbb67ca01b4206 Mon Sep 17 00:00:00 2001 From: Alexandre Primault Date: Thu, 30 May 2024 09:25:56 +0200 Subject: [PATCH 1/2] add Time convert functions --- .gitignore | 1 + convert/conv.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/.gitignore b/.gitignore index 1df19a1..cc7e00e 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ .DS_Store *~ vendor +.idea/ diff --git a/convert/conv.go b/convert/conv.go index fe21366..8d34daf 100644 --- a/convert/conv.go +++ b/convert/conv.go @@ -92,3 +92,50 @@ 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) string { + t, err := time.Parse(time.RFC3339, s) + if err != nil { + t, err = time.Parse("20060102T150405", s) + if err != nil { + return "" + } + } + return TimeToRFC3339(t) +} + +func TimeToStr(t time.Time, withTZ bool) string { + if withTZ { + return t.Format(time.RFC3339) + } + return t.Format("2006-01-02T15:04:05") +} + +func TimeStrToStr(s string, withTZ bool) string { + t, err := time.Parse(time.RFC3339, s) + if err != nil { + t, err = time.Parse("20060102T150405", s) + if err != nil { + return "" + } + } + return TimeToStr(t, withTZ) +} + +func TimePToStr(t *time.Time, withTZ bool) string { + if t == nil { + return "" + } + return TimeToStr(*t, withTZ) +} From 5d3cff39c0f386b1eea28b2959411fda4bdc1265 Mon Sep 17 00:00:00 2001 From: Alexandre Primault Date: Thu, 30 May 2024 11:01:25 +0200 Subject: [PATCH 2/2] Update conv.go --- convert/conv.go | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/convert/conv.go b/convert/conv.go index 8d34daf..c928f8c 100644 --- a/convert/conv.go +++ b/convert/conv.go @@ -104,38 +104,28 @@ func TimePToRFC3339(t *time.Time) string { return TimeToRFC3339(*t) } -func TimeStrToRFC3339(s string) string { - t, err := time.Parse(time.RFC3339, s) - if err != nil { - t, err = time.Parse("20060102T150405", s) - if err != nil { - return "" - } - } - return TimeToRFC3339(t) +func TimeStrToRFC3339(s string, sourceLayout string) (string, error) { + return ConvertTimeStrToAnotherLayout(s, sourceLayout, time.RFC3339) } -func TimeToStr(t time.Time, withTZ bool) string { +func TimeToStr(t time.Time, targetLayout string, withTZ bool) string { if withTZ { - return t.Format(time.RFC3339) + return t.Format(targetLayout) } return t.Format("2006-01-02T15:04:05") } -func TimeStrToStr(s string, withTZ bool) string { - t, err := time.Parse(time.RFC3339, s) - if err != nil { - t, err = time.Parse("20060102T150405", s) - if err != nil { - return "" - } +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 TimeToStr(t, withTZ) + return "", err } -func TimePToStr(t *time.Time, withTZ bool) string { +func TimePToStr(t *time.Time, targetLayout string, withTZ bool) string { if t == nil { return "" } - return TimeToStr(*t, withTZ) + return TimeToStr(*t, targetLayout, withTZ) }