Skip to content

Commit

Permalink
feat: preset (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytakahashi authored Aug 2, 2024
1 parent 6082555 commit e982f05
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/ytakahashi/alfred-unixtime-converter

go 1.22.5

require golang.org/x/text v0.16.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
18 changes: 18 additions & 0 deletions pkg/date/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package date

import (
"regexp"
"slices"
"strconv"
"strings"
"time"
)

Expand All @@ -15,6 +17,14 @@ type TimeStruct struct {
LocalDateTime string
}

type presetString string

const (
NOW = presetString("now")
TODAY = presetString("today")
)

var preset = []string{string(NOW), string(TODAY)}
var hhmmss = regexp.MustCompile(`^[0-2]?[0-9]:[0-5][0-9]:[0-5][0-9]`)
var hhmm = regexp.MustCompile(`^[0-2]?[0-9]:[0-5][0-9]$`)
var mmdd = regexp.MustCompile(`^[0-1][0-9]\-[0-3][0-9]`)
Expand All @@ -27,6 +37,9 @@ type InputFormatter interface {
ToTimeStruct(t time.Time) TimeStruct
}

type presetFormatter struct {
input string
}
type dateTimeInputFormatter struct {
input string
}
Expand All @@ -44,6 +57,11 @@ type currentTimeFormatter struct{}

// NewInputFormatter creates a new input formatter instance
func NewInputFormatter(input string) InputFormatter {
if slices.Contains(preset, strings.ToLower(input)) {
return presetFormatter{
input: input,
}
}
if mmdd.MatchString(input) {
return dateInputFormatter{
input: input,
Expand Down
25 changes: 25 additions & 0 deletions pkg/date/preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package date

import (
"fmt"
"time"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

func (formatter presetFormatter) ToTime() (time.Time, error) {
now := time.Now()
switch formatter.input {
case string(NOW):
return now, nil
case string(TODAY):
return time.Parse(layout, fmt.Sprintf("%d-%02d-%02dT00:00:00Z", now.Year(), now.Month(), now.Day()))
default:
return now, fmt.Errorf("Error: %s", formatter.input)
}
}

func (formatter presetFormatter) ToTimeStruct(t time.Time) TimeStruct {
return newTimeStruct(cases.Title(language.English).String(formatter.input), t)
}
51 changes: 51 additions & 0 deletions pkg/date/preset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package date

import (
"fmt"
"testing"
"time"
)

func Test_presetFormatter_ToTime_today(t *testing.T) {
sut := presetFormatter{
input: "today",
}

actual, err := sut.ToTime()
if err != nil {
t.Error("error should not be thrown")
}

now := time.Now()
expected := fmt.Sprintf("%d-%02d-%02dT00:00:00Z", now.Year(), now.Month(), now.Day())

format := actual.UTC().Format(time.RFC3339)
if format != expected {
t.Errorf("assert failed. expect:%s, actual:%s", expected, format)
}
}

func Test_presetFormatter_ToTimeStruct(t *testing.T) {
sut := presetFormatter{
input: "today",
}

v := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
actual := sut.ToTimeStruct(v)

if actual.Value != "Today" {
t.Errorf("unexpected Value (%s)", actual.Value)
}
if actual.DateTime != "2009-11-10T23:00:00Z" {
t.Errorf("unexpected DateTimelue (%s)", actual.DateTime)
}
if actual.LocalDateTime == "" {
t.Error("unexpected LocalDateTime")
}
if actual.Unixtime != 1257894000 {
t.Errorf("unexpected Unixtime (%d)", actual.Unixtime)
}
if actual.UnixtimeMillis != 1257894000000 {
t.Errorf("unexpected Unixtime (%d)", actual.UnixtimeMillis)
}
}

0 comments on commit e982f05

Please sign in to comment.