// Package now is a time toolkit for golang. // // More details README here: https://github.com/jinzhu/now // // import "github.com/jinzhu/now" // // now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon // now.BeginningOfDay() // 2013-11-18 00:00:00 Mon // now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
package now import // WeekStartDay set week start day, default is sunday var WeekStartDay = time.Sunday // TimeFormats default time formats will be parsed as var TimeFormats = []string{ "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", "2006-01-02T15:04:05Z0700", "2006-01-02T15:04:05Z07", "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", "1/2/2006", "1/2/2006 15:4:5", "2006/01/02", "20060102", "2006/01/02 15:04:05", time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850, time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano, time.Kitchen, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano, } // Config configuration for now package type Config struct { WeekStartDay time.Weekday TimeLocation *time.Location TimeFormats []string } // DefaultConfig default config var DefaultConfig *Config // New initialize Now based on configuration func ( *Config) ( time.Time) *Now { return &Now{Time: , Config: } } // Parse parse string to time based on configuration func ( *Config) ( ...string) (time.Time, error) { if .TimeLocation == nil { return .With(time.Now()).Parse(...) } else { return .With(time.Now().In(.TimeLocation)).Parse(...) } } // MustParse must parse string to time or will panic func ( *Config) ( ...string) time.Time { if .TimeLocation == nil { return .With(time.Now()).MustParse(...) } else { return .With(time.Now().In(.TimeLocation)).MustParse(...) } } // Now now struct type Now struct { time.Time *Config } // With initialize Now with time func ( time.Time) *Now { := DefaultConfig if == nil { = &Config{ WeekStartDay: WeekStartDay, TimeFormats: TimeFormats, } } return &Now{Time: , Config: } } // New initialize Now with time func ( time.Time) *Now { return With() } // BeginningOfMinute beginning of minute func () time.Time { return With(time.Now()).BeginningOfMinute() } // BeginningOfHour beginning of hour func () time.Time { return With(time.Now()).BeginningOfHour() } // BeginningOfDay beginning of day func () time.Time { return With(time.Now()).BeginningOfDay() } // BeginningOfWeek beginning of week func () time.Time { return With(time.Now()).BeginningOfWeek() } // BeginningOfMonth beginning of month func () time.Time { return With(time.Now()).BeginningOfMonth() } // BeginningOfQuarter beginning of quarter func () time.Time { return With(time.Now()).BeginningOfQuarter() } // BeginningOfYear beginning of year func () time.Time { return With(time.Now()).BeginningOfYear() } // EndOfMinute end of minute func () time.Time { return With(time.Now()).EndOfMinute() } // EndOfHour end of hour func () time.Time { return With(time.Now()).EndOfHour() } // EndOfDay end of day func () time.Time { return With(time.Now()).EndOfDay() } // EndOfWeek end of week func () time.Time { return With(time.Now()).EndOfWeek() } // EndOfMonth end of month func () time.Time { return With(time.Now()).EndOfMonth() } // EndOfQuarter end of quarter func () time.Time { return With(time.Now()).EndOfQuarter() } // EndOfYear end of year func () time.Time { return With(time.Now()).EndOfYear() } // Monday monday func ( ...string) time.Time { return With(time.Now()).Monday(...) } // Sunday sunday func ( ...string) time.Time { return With(time.Now()).Sunday(...) } // EndOfSunday end of sunday func () time.Time { return With(time.Now()).EndOfSunday() } // Quarter returns the yearly quarter func () uint { return With(time.Now()).Quarter() } // Parse parse string to time func ( ...string) (time.Time, error) { return With(time.Now()).Parse(...) } // ParseInLocation parse string to time in location func ( *time.Location, ...string) (time.Time, error) { return With(time.Now().In()).Parse(...) } // MustParse must parse string to time or will panic func ( ...string) time.Time { return With(time.Now()).MustParse(...) } // MustParseInLocation must parse string to time in location or will panic func ( *time.Location, ...string) time.Time { return With(time.Now().In()).MustParse(...) } // Between check now between the begin, end time or not func (, string) bool { return With(time.Now()).Between(, ) }