From 61a51996992facd1f115bff535f8d101a7a8a35b Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 14 Mar 2025 17:39:42 -0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20Timestamp=20Gen?= =?UTF-8?q?erator=20to=20single=20function=20with=20options?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gen_ts.go | 59 +++++++++++++++++++++++++++++++++++++++++ gen_ts_examples_test.go | 38 ++++++++++++++++++++++++++ gen_ts_test.go | 15 +++++++++++ generators.go | 42 ----------------------------- generators_test.go | 26 +----------------- 5 files changed, 113 insertions(+), 67 deletions(-) create mode 100644 gen_ts.go create mode 100644 gen_ts_examples_test.go create mode 100644 gen_ts_test.go diff --git a/gen_ts.go b/gen_ts.go new file mode 100644 index 0000000..4d0e13d --- /dev/null +++ b/gen_ts.go @@ -0,0 +1,59 @@ +package nomino + +import "time" + +// FileTimestamp is the default format for WithTimestamp and WithTime +const FileTimestamp string = "2006-01-02T15-04-05-0700" + +// FileTimestampNoTZ is the default format for WithTimestampUTC and WithTimeUTC +const FileTimestampNoTZ string = "2006-01-02T15-04-05" + +type timestampConf struct { + format string + ts time.Time + utc bool +} + +// TimestampOption provides options for the Timestamp Generator +type TimestampOption func(c *timestampConf) + +// Timestamp generates a a date and time. By default, it uses the current time, and will. +// be formatted accourding to FileTimestamp +func Timestamp(opts ...TimestampOption) Generator { + c := timestampConf{format: FileTimestamp, ts: time.Now()} + for _, opt := range opts { + opt(&c) + } + + if c.utc { + c.ts = c.ts.UTC() + } + + return func(*Config) (string, error) { + return c.ts.Format(c.format), nil + } +} + +// TSFormat sets the format for the generated name. +// Consult time.Time.Format for details on the format. +func TSFormat(format string) TimestampOption { + return func(c *timestampConf) { + c.format = format + } +} + +// TSTime sets the time for the generated name. +// By default, it uses the current time. +func TSTime(t time.Time) TimestampOption { + return func(c *timestampConf) { + c.ts = t + } +} + +// TSUTC uses the time in UTC, while also stripping the timezone from the format. +func TSUTC() TimestampOption { + return func(c *timestampConf) { + c.utc = true + c.format = FileTimestampNoTZ + } +} diff --git a/gen_ts_examples_test.go b/gen_ts_examples_test.go new file mode 100644 index 0000000..0dcc0b6 --- /dev/null +++ b/gen_ts_examples_test.go @@ -0,0 +1,38 @@ +package nomino_test + +import ( + "fmt" + "time" + + "codeberg.org/danjones000/nomino" +) + +func ExampleTimestamp_withTime() { + tz, _ := time.LoadLocation("America/New_York") + ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz) + gen := nomino.Timestamp(nomino.TSTime(ts)) + conf := nomino.NewConfig(nomino.WithGenerator(gen)) + s, _ := nomino.Make(conf) + fmt.Println(s) + // Output: 2009-01-20T12-05-00-0500.txt +} + +func ExampleTimestamp_withFormat() { + tz, _ := time.LoadLocation("America/New_York") + ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz) + gen := nomino.Timestamp(nomino.TSTime(ts), nomino.TSFormat("2006#01#02<>15|04|05-0700")) + conf := nomino.NewConfig(nomino.WithGenerator(gen)) + s, _ := nomino.Make(conf) + fmt.Println(s) + // Output: 2009#01#20<>12|05|00-0500.txt +} + +func ExampleTimestamp_withUTC() { + tz, _ := time.LoadLocation("America/New_York") + ts := time.Date(2009, time.January, 20, 12, 5, 0, 0, tz) + gen := nomino.Timestamp(nomino.TSTime(ts), nomino.TSUTC()) + conf := nomino.NewConfig(nomino.WithGenerator(gen)) + s, _ := nomino.Make(conf) + fmt.Println(s) + // Output: 2009-01-20T17-05-00.txt +} diff --git a/gen_ts_test.go b/gen_ts_test.go new file mode 100644 index 0000000..2997702 --- /dev/null +++ b/gen_ts_test.go @@ -0,0 +1,15 @@ +package nomino + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestTimestamp(t *testing.T) { + n := time.Now() + st, err := Timestamp()(nil) + assert.NoError(t, err) + assert.Equal(t, n.Format(FileTimestamp), st) +} diff --git a/generators.go b/generators.go index 8685662..a1b2b52 100644 --- a/generators.go +++ b/generators.go @@ -8,7 +8,6 @@ import ( "fmt" "hash" "strconv" - "time" "github.com/google/uuid" "github.com/gosimple/slug" @@ -65,46 +64,6 @@ func UUID() Generator { return uuidGen } -// FileTimestamp is the default format for WithTimestamp and WithTime -const FileTimestamp string = "2006-01-02_03-05-06-0700" - -// Timestamp generates a a date and time for the current time. -// It is formatted accourding to FileTimestamp -func Timestamp() Generator { - return TimestampWithFormat(FileTimestamp) -} - -// TimestampWithFormat generates a date and time for the current time with the supplied format. -func TimestampWithFormat(f string) Generator { - return FormattedTime(time.Now(), FileTimestamp) -} - -// Time generates a date and time for the supplied time. -// It is formatted accourding to FileTimestamp -func Time(t time.Time) Generator { - return FormattedTime(t, FileTimestamp) -} - -// FormattedTime generates a date and time for the supplied time with the supplied format. -func FormattedTime(t time.Time, f string) Generator { - return func(*Config) (string, error) { - return t.Format(f), nil - } -} - -// FileTimestampNoTZ is the default format for WithTimestampUTC and WithTimeUTC -const FileTimestampNoTZ string = "2006-01-02_03-05-06" - -// TimestampUTC generates a date and time for the current time in UTC without a timezone in the format. -func TimestampUTC() Generator { - return TimeUTC(time.Now()) -} - -// TimeUTC generates a date and time for the supplied time in UTC without a timezone in the format. -func TimeUTC(t time.Time) Generator { - return FormattedTime(t.UTC(), FileTimestampNoTZ) -} - // Incremental generates a name that is a series of integers starting at 0 func Incremental() Generator { return IncrementalWithStartAndStep(0, 1) @@ -136,7 +95,6 @@ func IncrementalWithStep(step int) Generator { func IncrementalFormatWithStep(step int, format string) Generator { return IncrementalFormatWithStartAndStep(0, step, format) } - func incrementalHelper(start, step int, cb func(int) string) Generator { next := start return func(*Config) (string, error) { diff --git a/generators_test.go b/generators_test.go index c6b3bbe..330b169 100644 --- a/generators_test.go +++ b/generators_test.go @@ -2,9 +2,7 @@ package nomino import ( "errors" - "fmt" "testing" - "time" "github.com/google/uuid" "github.com/stretchr/testify/assert" @@ -85,28 +83,6 @@ func TestUUIDFail(t *testing.T) { assert.Equal(t, errors.New("sorry"), err) } -func TestTimestamp(t *testing.T) { - n := time.Now() - st, err := Timestamp()(nil) - assert.NoError(t, err) - assert.Equal(t, n.Format(FileTimestamp), st) -} - -func TestTime(t *testing.T) { - d := time.Date(1986, time.March, 28, 12, 0, 0, 0, time.UTC) - - st, err := Time(d)(nil) - assert.NoError(t, err) - assert.Equal(t, d.Format(FileTimestamp), st) -} - -func TestTimestampUTC(t *testing.T) { - n := time.Now() - st, err := TimestampUTC()(nil) - assert.NoError(t, err) - assert.Equal(t, n.UTC().Format(FileTimestampNoTZ), st) -} - func TestSlugMissingFilename(t *testing.T) { conf := NewConfig(WithGenerator(Slug())) st, err := conf.generator(&conf) @@ -138,6 +114,6 @@ func TestHashMissingOriginal(t *testing.T) { } func TestHashTypeStringer(t *testing.T) { - s := fmt.Sprintf("%s", MD5) + s := MD5.String() assert.Equal(t, "MD5", s) }