main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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{"1/2/2006", "1/2/2006 15:4:5", "2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "1-2", "15:4:5", "15:4", "15", "15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST"}
  16. // Now now struct
  17. type Now struct {
  18. time.Time
  19. }
  20. // New initialize Now with time
  21. func New(t time.Time) *Now {
  22. return &Now{t}
  23. }
  24. // BeginningOfMinute beginning of minute
  25. func BeginningOfMinute() time.Time {
  26. return New(time.Now()).BeginningOfMinute()
  27. }
  28. // BeginningOfHour beginning of hour
  29. func BeginningOfHour() time.Time {
  30. return New(time.Now()).BeginningOfHour()
  31. }
  32. // BeginningOfDay beginning of day
  33. func BeginningOfDay() time.Time {
  34. return New(time.Now()).BeginningOfDay()
  35. }
  36. // BeginningOfWeek beginning of week
  37. func BeginningOfWeek() time.Time {
  38. return New(time.Now()).BeginningOfWeek()
  39. }
  40. // BeginningOfMonth beginning of month
  41. func BeginningOfMonth() time.Time {
  42. return New(time.Now()).BeginningOfMonth()
  43. }
  44. // BeginningOfQuarter beginning of quarter
  45. func BeginningOfQuarter() time.Time {
  46. return New(time.Now()).BeginningOfQuarter()
  47. }
  48. // BeginningOfYear beginning of year
  49. func BeginningOfYear() time.Time {
  50. return New(time.Now()).BeginningOfYear()
  51. }
  52. // EndOfMinute end of minute
  53. func EndOfMinute() time.Time {
  54. return New(time.Now()).EndOfMinute()
  55. }
  56. // EndOfHour end of hour
  57. func EndOfHour() time.Time {
  58. return New(time.Now()).EndOfHour()
  59. }
  60. // EndOfDay end of day
  61. func EndOfDay() time.Time {
  62. return New(time.Now()).EndOfDay()
  63. }
  64. // EndOfWeek end of week
  65. func EndOfWeek() time.Time {
  66. return New(time.Now()).EndOfWeek()
  67. }
  68. // EndOfMonth end of month
  69. func EndOfMonth() time.Time {
  70. return New(time.Now()).EndOfMonth()
  71. }
  72. // EndOfQuarter end of quarter
  73. func EndOfQuarter() time.Time {
  74. return New(time.Now()).EndOfQuarter()
  75. }
  76. // EndOfYear end of year
  77. func EndOfYear() time.Time {
  78. return New(time.Now()).EndOfYear()
  79. }
  80. // Monday monday
  81. func Monday() time.Time {
  82. return New(time.Now()).Monday()
  83. }
  84. // Sunday sunday
  85. func Sunday() time.Time {
  86. return New(time.Now()).Sunday()
  87. }
  88. // EndOfSunday end of sunday
  89. func EndOfSunday() time.Time {
  90. return New(time.Now()).EndOfSunday()
  91. }
  92. // Parse parse string to time
  93. func Parse(strs ...string) (time.Time, error) {
  94. return New(time.Now()).Parse(strs...)
  95. }
  96. // ParseInLocation parse string to time in location
  97. func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error) {
  98. return New(time.Now().In(loc)).Parse(strs...)
  99. }
  100. // MustParse must parse string to time or will panic
  101. func MustParse(strs ...string) time.Time {
  102. return New(time.Now()).MustParse(strs...)
  103. }
  104. // MustParseInLocation must parse string to time in location or will panic
  105. func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
  106. return New(time.Now().In(loc)).MustParse(strs...)
  107. }
  108. // Between check now between the begin, end time or not
  109. func Between(time1, time2 string) bool {
  110. return New(time.Now()).Between(time1, time2)
  111. }