nomino/gen_ts.go

60 lines
1.5 KiB
Go
Raw Normal View History

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
}
}
2025-03-14 18:09:07 -05:00
// TimestampFormat sets the format for the generated name.
// Consult time.Time.Format for details on the format.
2025-03-14 18:09:07 -05:00
func TimestampFormat(format string) TimestampOption {
return func(c *timestampConf) {
c.format = format
}
}
2025-03-14 18:09:07 -05:00
// TimestampTime sets the time for the generated name.
// By default, it uses the current time.
2025-03-14 18:09:07 -05:00
func TimestampTime(t time.Time) TimestampOption {
return func(c *timestampConf) {
c.ts = t
}
}
2025-03-14 18:09:07 -05:00
// TimestampUTC uses the time in UTC, while also stripping the timezone from the format.
func TimestampUTC() TimestampOption {
return func(c *timestampConf) {
c.utc = true
c.format = FileTimestampNoTZ
}
}