options.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package strftime
  2. type Option interface {
  3. Name() string
  4. Value() interface{}
  5. }
  6. type option struct {
  7. name string
  8. value interface{}
  9. }
  10. func (o *option) Name() string { return o.name }
  11. func (o *option) Value() interface{} { return o.value }
  12. const optSpecificationSet = `opt-specification-set`
  13. // WithSpecification allows you to specify a custom specification set
  14. func WithSpecificationSet(ds SpecificationSet) Option {
  15. return &option{
  16. name: optSpecificationSet,
  17. value: ds,
  18. }
  19. }
  20. type optSpecificationPair struct {
  21. name byte
  22. appender Appender
  23. }
  24. const optSpecification = `opt-specification`
  25. // WithSpecification allows you to create a new specification set on the fly,
  26. // to be used only for that invocation.
  27. func WithSpecification(b byte, a Appender) Option {
  28. return &option{
  29. name: optSpecification,
  30. value: &optSpecificationPair{
  31. name: b,
  32. appender: a,
  33. },
  34. }
  35. }
  36. // WithMilliseconds is similar to WithSpecification, and specifies that
  37. // the Strftime object should interpret the pattern `%b` (where b
  38. // is the byte that you specify as the argument)
  39. // as the zero-padded, 3 letter milliseconds of the time.
  40. func WithMilliseconds(b byte) Option {
  41. return WithSpecification(b, Milliseconds())
  42. }
  43. // WithMicroseconds is similar to WithSpecification, and specifies that
  44. // the Strftime object should interpret the pattern `%b` (where b
  45. // is the byte that you specify as the argument)
  46. // as the zero-padded, 3 letter microseconds of the time.
  47. func WithMicroseconds(b byte) Option {
  48. return WithSpecification(b, Microseconds())
  49. }
  50. // WithUnixSeconds is similar to WithSpecification, and specifies that
  51. // the Strftime object should interpret the pattern `%b` (where b
  52. // is the byte that you specify as the argument)
  53. // as the unix timestamp in seconds
  54. func WithUnixSeconds(b byte) Option {
  55. return WithSpecification(b, UnixSeconds())
  56. }