2025-03-14 17:39:42 -05:00
|
|
|
package nomino
|
|
|
|
|
|
|
|
|
|
import "time"
|
|
|
|
|
|
2025-03-31 12:10:05 -05:00
|
|
|
// FileTimestamp is the default format for [Timestamp].
|
2025-03-14 17:39:42 -05:00
|
|
|
const FileTimestamp string = "2006-01-02T15-04-05-0700"
|
|
|
|
|
|
2025-03-31 12:10:05 -05:00
|
|
|
// FileTimestampNoTZ is the default format when using the [TimestampUTC] [TimestampOption].
|
2025-03-14 17:39:42 -05:00
|
|
|
const FileTimestampNoTZ string = "2006-01-02T15-04-05"
|
|
|
|
|
|
|
|
|
|
type timestampConf struct {
|
|
|
|
|
format string
|
|
|
|
|
ts time.Time
|
|
|
|
|
utc bool
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-31 12:10:05 -05:00
|
|
|
// TimestampOption provides options for the [Timestamp] [Generator].
|
2025-03-14 17:39:42 -05:00
|
|
|
type TimestampOption func(c *timestampConf)
|
|
|
|
|
|
|
|
|
|
// Timestamp generates a a date and time. By default, it uses the current time, and will.
|
2025-03-19 18:05:16 -05:00
|
|
|
// be formatted accourding to FileTimestamp.
|
2025-03-14 17:39:42 -05:00
|
|
|
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.
|
2025-03-31 12:10:05 -05:00
|
|
|
// Consult [time.Time.Format] for details on the format.
|
2025-03-14 18:09:07 -05:00
|
|
|
func TimestampFormat(format string) TimestampOption {
|
2025-03-14 17:39:42 -05:00
|
|
|
return func(c *timestampConf) {
|
|
|
|
|
c.format = format
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-14 18:09:07 -05:00
|
|
|
// TimestampTime sets the time for the generated name.
|
2025-03-31 12:10:05 -05:00
|
|
|
// By default, [Timestamp] uses the current time.
|
2025-03-14 18:09:07 -05:00
|
|
|
func TimestampTime(t time.Time) TimestampOption {
|
2025-03-14 17:39:42 -05:00
|
|
|
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 {
|
2025-03-14 17:39:42 -05:00
|
|
|
return func(c *timestampConf) {
|
|
|
|
|
c.utc = true
|
|
|
|
|
c.format = FileTimestampNoTZ
|
|
|
|
|
}
|
|
|
|
|
}
|