From f5287b7c3c2fe5af26b296105153300e247e5133 Mon Sep 17 00:00:00 2001 From: Tibz-Dankan Date: Wed, 14 Aug 2024 17:33:24 +0300 Subject: [PATCH] clean: remove redundant date operation methods --- internal/services/date.go | 109 -------------------------------------- 1 file changed, 109 deletions(-) diff --git a/internal/services/date.go b/internal/services/date.go index 422c5f6..86fdd9a 100644 --- a/internal/services/date.go +++ b/internal/services/date.go @@ -1,10 +1,8 @@ package services import ( - "errors" "fmt" "log" - "regexp" "time" ) @@ -130,113 +128,6 @@ func (d *Date) UTC() (time.Time, error) { return parsedTime, nil } -// Returns the layout in the format -// "2006-01-02 15:04:05.999999999 -0700 MST" -// by extracting offset and time zone abbreviation -// from the ISOString input of the same format -func (d *Date) ISOTimeLayout() (string, error) { - dateStr := "2006-01-02 15:04:05.999999999" - - offset, err := d.extractOffset(d.ISOStringDate) - if err != nil { - fmt.Println("Error Extracting offset:", err) - return "", err - } - - timeZoneAbbreviation, err := d.extractTimeZoneAbbreviation(d.ISOStringDate) - if err != nil { - fmt.Println("Error Extracting time zone abbreviation:", err) - return "", err - } - - ISOString := dateStr + " " + offset + " " + timeZoneAbbreviation - - return ISOString, nil -} - -// Returns the layout in the format -// "2006-Jan-02 15:04:05 -0700" by extracting offset -// from the ISOString input of the same format -func (d *Date) TimeLayout() (string, error) { - hourMinSec := "15:04:05" - currentTime := time.Now() - - var currentTimeDay string - day := currentTime.Day() - if day < 10 { - currentTimeDay = "0" + fmt.Sprint(day) - } else { - currentTimeDay = fmt.Sprint(day) - } - - currentTimeYear := fmt.Sprint(currentTime.Year()) - currentTimeMonth := fmt.Sprint(currentTime.Month()) - offset, err := d.extractOffset(d.ISOStringDate) - if err != nil { - fmt.Println("Error Extracting offset:", err) - return "", err - } - - date := currentTimeYear + "-" + currentTimeMonth + "-" + currentTimeDay - layoutStr := date + " " + hourMinSec + " " + offset - - return layoutStr, nil -} - -// Returns the layout in the format -// "2006-01-02T15:04:05.999999999+03:00" by extracting offset -// from the RFC3339Nano input of the same format -func (d *Date) RFC3339NanoLayout() (string, error) { - dateStr := "2006-01-02T15:04:05.999999999" - - offset, err := d.extractOffsetFromRFC3339Nano(d.ISOStringDate) - if err != nil { - fmt.Println("Error Extracting offset:", err) - return "", err - } - RFC3339NanoString := dateStr + offset - - return RFC3339NanoString, nil -} - -// extractOffset extracts the -// offset from a given time string of format -// "2006-01-02 15:04:05.999999999 -0700 MST" or -// "2006-Jan-02 15:04:05 -0700" using string matching. -func (d *Date) extractOffset(timeStr string) (string, error) { - re := regexp.MustCompile(`[-+]\d{4}`) - offset := re.FindString(timeStr) - if offset == "" { - return "", errors.New("offset not found in time string") - } - return offset, nil -} - -// extractTimeZoneAbbreviation extracts the -// time zone abbreviation from a given time string -// of format "2006-01-02 15:04:05.999999999 -0700 MST" -// using string matching. -func (d *Date) extractTimeZoneAbbreviation(timeStr string) (string, error) { - re := regexp.MustCompile(`[A-Z]{3,4}$`) - abbr := re.FindString(timeStr) - if abbr == "" { - return "", errors.New("time zone abbreviation not found in time string") - } - return abbr, nil -} - -// extractOffsetFromRFC3339Nano extracts -// the offset from a given RFC3339Nano formatted time -// string "2006-01-02T15:04:05.999999999+03:00" using string matching. -func (d *Date) extractOffsetFromRFC3339Nano(timeStr string) (string, error) { - re := regexp.MustCompile(`[-+]\d{2}:\d{2}`) - offset := re.FindString(timeStr) - if offset == "" { - return "", errors.New("offset not found in time string") - } - return offset, nil -} - // Takes in an input date string such // as "2024-07-28T12:05:00.137685Z" coming from clients //