-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6082555
commit e982f05
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |