♻️ Refactor Timestamp Generator to single function with options

This commit is contained in:
Dan Jones 2025-03-14 17:39:42 -05:00
commit 61a5199699
5 changed files with 113 additions and 67 deletions

59
gen_ts.go Normal file
View file

@ -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
}
}

38
gen_ts_examples_test.go Normal file
View file

@ -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
}

15
gen_ts_test.go Normal file
View file

@ -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)
}

View file

@ -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) {

View file

@ -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)
}