Skip to content

Commit

Permalink
Merge pull request #132 from Tibz-Dankan/integrate/mail-jet
Browse files Browse the repository at this point in the history
clean: remove redundant date operation methods
  • Loading branch information
Tibz-Dankan authored Aug 14, 2024
2 parents b9166dd + f5287b7 commit c35655b
Showing 1 changed file with 0 additions and 109 deletions.
109 changes: 0 additions & 109 deletions internal/services/date.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package services

import (
"errors"
"fmt"
"log"
"regexp"
"time"
)

Expand Down Expand Up @@ -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
//
Expand Down

0 comments on commit c35655b

Please sign in to comment.