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