Skip to content

Improve comments and code style #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ type Config struct {
// DefaultConfig default config
var DefaultConfig *Config

// New initialize Now based on configuration
// With initializes Now based on configuration
func (config *Config) With(t time.Time) *Now {
return &Now{Time: t, Config: config}
}

// Parse parse string to time based on configuration
// Parse parses string to time based on configuration
func (config *Config) Parse(strs ...string) (time.Time, error) {
if config.TimeLocation == nil {
return config.With(time.Now()).Parse(strs...)
Expand Down Expand Up @@ -153,13 +153,12 @@ func EndOfYear() time.Time {
return With(time.Now()).EndOfYear()
}

// Monday monday

// Monday returns the time.Time value of Monday
func Monday(strs ...string) time.Time {
return With(time.Now()).Monday(strs...)
}

// Sunday sunday
// Sunday returns the time.Time value of Sunday
func Sunday(strs ...string) time.Time {
return With(time.Now()).Sunday(strs...)
}
Expand Down
4 changes: 3 additions & 1 deletion now.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (now *Now) Monday() time.Time {
}
*/

// Monday returns the Monday Date of the week specified by now
func (now *Now) Monday(strs ...string) time.Time {
var parseTime time.Time
var err error
Expand All @@ -137,6 +138,7 @@ func (now *Now) Monday(strs ...string) time.Time {
return parseTime.AddDate(0, 0, -weekday+1)
}

// Sunday returns the Sunday Date of the week specified by now
func (now *Now) Sunday(strs ...string) time.Time {
var parseTime time.Time
var err error
Expand Down Expand Up @@ -180,7 +182,7 @@ func (now *Now) parseWithFormat(str string, location *time.Location) (t time.Tim
var hasTimeRegexp = regexp.MustCompile(`(\s+|^\s*|T)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))(\s*$|[Z+-])`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, 2021-07-20T00:59:10Z, 2021-07-20T00:59:10+08:00, 2021-07-20T00:00:10-07:00 etc
var onlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc

// Parse parse string to time
// Parse parses string to time
func (now *Now) Parse(strs ...string) (t time.Time, err error) {
var (
setCurrentTime bool
Expand Down
12 changes: 6 additions & 6 deletions now_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,32 +292,32 @@ func TestParse(t *testing.T) {

assert(With(n).MustParse("2002-10-12T00:14:56+08:00"), "2002-10-12 00:14:56", "Parse 2002-10-12T00:14:56+08:00")
_, off := With(n).MustParse("2002-10-12T00:14:56+08:00").Zone()
if (off != 28800) {
if off != 28800 {
t.Errorf("Parse 2002-10-12T00:14:56+08:00 shouldn't lose time zone offset")
}
assert(With(n).MustParse("2002-10-12T00:00:56-07:00"), "2002-10-12 00:00:56", "Parse 2002-10-12T00:00:56-07:00")
_, off2 := With(n).MustParse("2002-10-12T00:00:56-07:00").Zone()
if (off2 != -25200){
if off2 != -25200 {
t.Errorf("Parse 2002-10-12T00:00:56-07:00 shouldn't lose time zone offset")
}
assert(With(n).MustParse("2002-10-12T00:01:12.333+0200"), "2002-10-12 00:01:12.333", "Parse 2002-10-12T00:01:12.333+0200")
_, off3 := With(n).MustParse("2002-10-12T00:01:12.333+0200").Zone()
if (off3 != 7200){
if off3 != 7200 {
t.Errorf("Parse 2002-10-12T00:01:12.333+0200 shouldn't lose time zone offset")
}
assert(With(n).MustParse("2002-10-12T00:00:56.999999999+08:00"), "2002-10-12 00:00:56.999999999", "Parse 2002-10-12T00:00:56.999999999+08:00")
_, off4 := With(n).MustParse("2002-10-12T00:14:56.999999999+08:00").Zone()
if (off4 != 28800) {
if off4 != 28800 {
t.Errorf("Parse 2002-10-12T00:14:56.999999999+08:00 shouldn't lose time zone offset")
}
assert(With(n).MustParse("2002-10-12T00:00:56.666666-07:00"), "2002-10-12 00:00:56.666666", "Parse 2002-10-12T00:00:56.666666-07:00")
_, off5 := With(n).MustParse("2002-10-12T00:00:56.666666-07:00").Zone()
if (off5 != -25200){
if off5 != -25200 {
t.Errorf("Parse 2002-10-12T00:00:56.666666-07:00 shouldn't lose time zone offset")
}
assert(With(n).MustParse("2002-10-12T00:01:12.999999999-06"), "2002-10-12 00:01:12.999999999", "Parse 2002-10-12T00:01:12.999999999-06")
_, off6 := With(n).MustParse("2002-10-12T00:01:12.999999999-06").Zone()
if (off6 != -21600){
if off6 != -21600 {
t.Errorf("Parse 2002-10-12T00:01:12.999999999-06 shouldn't lose time zone offset")
}

Expand Down