main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Package now is a time toolkit for golang.
  2. //
  3. // More details README here: https://github.com/jinzhu/now
  4. //
  5. // import "github.com/jinzhu/now"
  6. //
  7. // now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
  8. // now.BeginningOfDay() // 2013-11-18 00:00:00 Mon
  9. // now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
  10. package now
  11. import "time"
  12. // WeekStartDay set week start day, default is sunday
  13. var WeekStartDay = time.Sunday
  14. // TimeFormats default time formats will be parsed as
  15. var TimeFormats = []string{
  16. "2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "1-2",
  17. "15:4:5", "15:4", "15",
  18. "15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST", "2006-01-02T15:04:05-07:00",
  19. "2006.1.2", "2006.1.2 15:04:05", "2006.01.02", "2006.01.02 15:04:05", "2006.01.02 15:04:05.999999999",
  20. "1/2/2006", "1/2/2006 15:4:5", "2006/01/02", "2006/01/02 15:04:05",
  21. time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850,
  22. time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano,
  23. time.Kitchen, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano,
  24. }
  25. // Config configuration for now package
  26. type Config struct {
  27. WeekStartDay time.Weekday
  28. TimeLocation *time.Location
  29. TimeFormats []string
  30. }
  31. // DefaultConfig default config
  32. var DefaultConfig *Config
  33. // New initialize Now based on configuration
  34. func (config *Config) With(t time.Time) *Now {
  35. return &Now{Time: t, Config: config}
  36. }
  37. // Parse parse string to time based on configuration
  38. func (config *Config) Parse(strs ...string) (time.Time, error) {
  39. if config.TimeLocation == nil {
  40. return config.With(time.Now()).Parse(strs...)
  41. } else {
  42. return config.With(time.Now().In(config.TimeLocation)).Parse(strs...)
  43. }
  44. }
  45. // MustParse must parse string to time or will panic
  46. func (config *Config) MustParse(strs ...string) time.Time {
  47. if config.TimeLocation == nil {
  48. return config.With(time.Now()).MustParse(strs...)
  49. } else {
  50. return config.With(time.Now().In(config.TimeLocation)).MustParse(strs...)
  51. }
  52. }
  53. // Now now struct
  54. type Now struct {
  55. time.Time
  56. *Config
  57. }
  58. // With initialize Now with time
  59. func With(t time.Time) *Now {
  60. config := DefaultConfig
  61. if config == nil {
  62. config = &Config{
  63. WeekStartDay: WeekStartDay,
  64. TimeFormats: TimeFormats,
  65. }
  66. }
  67. return &Now{Time: t, Config: config}
  68. }
  69. // New initialize Now with time
  70. func New(t time.Time) *Now {
  71. return With(t)
  72. }
  73. // BeginningOfMinute beginning of minute
  74. func BeginningOfMinute() time.Time {
  75. return With(time.Now()).BeginningOfMinute()
  76. }
  77. // BeginningOfHour beginning of hour
  78. func BeginningOfHour() time.Time {
  79. return With(time.Now()).BeginningOfHour()
  80. }
  81. // BeginningOfDay beginning of day
  82. func BeginningOfDay() time.Time {
  83. return With(time.Now()).BeginningOfDay()
  84. }
  85. // BeginningOfWeek beginning of week
  86. func BeginningOfWeek() time.Time {
  87. return With(time.Now()).BeginningOfWeek()
  88. }
  89. // BeginningOfMonth beginning of month
  90. func BeginningOfMonth() time.Time {
  91. return With(time.Now()).BeginningOfMonth()
  92. }
  93. // BeginningOfQuarter beginning of quarter
  94. func BeginningOfQuarter() time.Time {
  95. return With(time.Now()).BeginningOfQuarter()
  96. }
  97. // BeginningOfYear beginning of year
  98. func BeginningOfYear() time.Time {
  99. return With(time.Now()).BeginningOfYear()
  100. }
  101. // EndOfMinute end of minute
  102. func EndOfMinute() time.Time {
  103. return With(time.Now()).EndOfMinute()
  104. }
  105. // EndOfHour end of hour
  106. func EndOfHour() time.Time {
  107. return With(time.Now()).EndOfHour()
  108. }
  109. // EndOfDay end of day
  110. func EndOfDay() time.Time {
  111. return With(time.Now()).EndOfDay()
  112. }
  113. // EndOfWeek end of week
  114. func EndOfWeek() time.Time {
  115. return With(time.Now()).EndOfWeek()
  116. }
  117. // EndOfMonth end of month
  118. func EndOfMonth() time.Time {
  119. return With(time.Now()).EndOfMonth()
  120. }
  121. // EndOfQuarter end of quarter
  122. func EndOfQuarter() time.Time {
  123. return With(time.Now()).EndOfQuarter()
  124. }
  125. // EndOfYear end of year
  126. func EndOfYear() time.Time {
  127. return With(time.Now()).EndOfYear()
  128. }
  129. // Monday monday
  130. func Monday() time.Time {
  131. return With(time.Now()).Monday()
  132. }
  133. // Sunday sunday
  134. func Sunday() time.Time {
  135. return With(time.Now()).Sunday()
  136. }
  137. // EndOfSunday end of sunday
  138. func EndOfSunday() time.Time {
  139. return With(time.Now()).EndOfSunday()
  140. }
  141. // Parse parse string to time
  142. func Parse(strs ...string) (time.Time, error) {
  143. return With(time.Now()).Parse(strs...)
  144. }
  145. // ParseInLocation parse string to time in location
  146. func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error) {
  147. return With(time.Now().In(loc)).Parse(strs...)
  148. }
  149. // MustParse must parse string to time or will panic
  150. func MustParse(strs ...string) time.Time {
  151. return With(time.Now()).MustParse(strs...)
  152. }
  153. // MustParseInLocation must parse string to time in location or will panic
  154. func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
  155. return With(time.Now().In(loc)).MustParse(strs...)
  156. }
  157. // Between check now between the begin, end time or not
  158. func Between(time1, time2 string) bool {
  159. return With(time.Now()).Between(time1, time2)
  160. }