Skip to content
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

v2: adopt begin/end semantics to half-open interval #70

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 0 additions & 11 deletions .github/workflows/reviewdog.yml

This file was deleted.

32 changes: 8 additions & 24 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,18 @@ name: tests
on:
push:
branches-ignore:
- 'gh-pages'
- "gh-pages"
pull_request:
branches-ignore:
- 'gh-pages'
- "gh-pages"

jobs:
# Label of the container job
sqlite:
strategy:
matrix:
go: ['1.17', '1.16']
platform: [ubuntu-latest] # can not run in windows OS
runs-on: ${{ matrix.platform }}
test:
runs-on: ubuntu-latest

steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: go mod package cache
uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go }}-${{ hashFiles('go.sum') }}

- name: Tests
run: go test
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: Tests
run: go test
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
//
// More details README here: https://github.com/jinzhu/now
//
// import "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
// 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 "time"
Expand Down Expand Up @@ -195,6 +195,6 @@ func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
}

// Between check now between the begin, end time or not
func Between(time1, time2 string) bool {
return With(time.Now()).Between(time1, time2)
func Between(begin, end time.Time) bool {
return With(time.Now()).Between(begin, end)
}
28 changes: 14 additions & 14 deletions now.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,43 +68,43 @@ func (now *Now) BeginningOfYear() time.Time {

// EndOfMinute end of minute
func (now *Now) EndOfMinute() time.Time {
return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
return now.BeginningOfMinute().Add(time.Minute)
}

// EndOfHour end of hour
func (now *Now) EndOfHour() time.Time {
return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
return now.BeginningOfHour().Add(time.Hour)
}

// EndOfDay end of day
func (now *Now) EndOfDay() time.Time {
y, m, d := now.Date()
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), now.Location())
return time.Date(y, m, d, 23, 59, 0, 0, now.Location()).Add(time.Minute)
}

// EndOfWeek end of week
func (now *Now) EndOfWeek() time.Time {
return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
return now.BeginningOfWeek().AddDate(0, 0, 7)
}

// EndOfMonth end of month
func (now *Now) EndOfMonth() time.Time {
return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
return now.BeginningOfMonth().AddDate(0, 1, 0)
}

// EndOfQuarter end of quarter
func (now *Now) EndOfQuarter() time.Time {
return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
return now.BeginningOfQuarter().AddDate(0, 3, 0)
}

// EndOfHalf end of half year
func (now *Now) EndOfHalf() time.Time {
return now.BeginningOfHalf().AddDate(0, 6, 0).Add(-time.Nanosecond)
return now.BeginningOfHalf().AddDate(0, 6, 0)
}

// EndOfYear end of year
func (now *Now) EndOfYear() time.Time {
return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
return now.BeginningOfYear().AddDate(1, 0, 0)
}

// Monday monday
Expand Down Expand Up @@ -177,8 +177,10 @@ func (now *Now) parseWithFormat(str string, location *time.Location) (t time.Tim
return
}

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
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
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
func (now *Now) Parse(strs ...string) (t time.Time, err error) {
Expand Down Expand Up @@ -238,8 +240,6 @@ func (now *Now) MustParse(strs ...string) (t time.Time) {
}

// Between check time between the begin, end time or not
func (now *Now) Between(begin, end string) bool {
beginTime := now.MustParse(begin)
endTime := now.MustParse(end)
return now.After(beginTime) && now.Before(endTime)
func (now *Now) Between(begin, end time.Time) bool {
return !now.Before(begin) && now.Before(end)
}
94 changes: 47 additions & 47 deletions now_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,72 +130,72 @@ func TestEndOf(t *testing.T) {

n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)

assert(With(n).EndOfMinute(), "2013-11-18 17:51:59.999999999", "EndOfMinute")
assert(With(n).EndOfMinute(), "2013-11-18 17:52:00", "EndOfMinute")

assert(With(n).EndOfHour(), "2013-11-18 17:59:59.999999999", "EndOfHour")
assert(With(n).EndOfHour(), "2013-11-18 18:00:00", "EndOfHour")

assert(With(timeCaracas).EndOfHour(), "2016-01-01 12:59:59.999999999", "EndOfHour Caracas")
assert(With(timeCaracas).EndOfHour(), "2016-01-01 13:00:00", "EndOfHour Caracas")

assert(With(n).EndOfDay(), "2013-11-18 23:59:59.999999999", "EndOfDay")
assert(With(n).EndOfDay(), "2013-11-19 00:00:00", "EndOfDay")

dstEndOfDay := time.Date(2017, 10, 29, 1, 0, 0, 0, locationBerlin)
assert(With(dstEndOfDay).EndOfDay(), "2017-10-29 23:59:59.999999999", "EndOfDay DST")
assert(With(dstEndOfDay).EndOfDay(), "2017-10-30 00:00:00", "EndOfDay DST")

WeekStartDay = time.Tuesday
assert(With(n).EndOfWeek(), "2013-11-18 23:59:59.999999999", "EndOfWeek, FirstDayTuesday")
assert(With(n).EndOfWeek(), "2013-11-19 00:00:00", "EndOfWeek, FirstDayTuesday")

WeekStartDay = time.Wednesday
assert(With(n).EndOfWeek(), "2013-11-19 23:59:59.999999999", "EndOfWeek, FirstDayWednesday")
assert(With(n).EndOfWeek(), "2013-11-20 00:00:00", "EndOfWeek, FirstDayWednesday")

WeekStartDay = time.Thursday
assert(With(n).EndOfWeek(), "2013-11-20 23:59:59.999999999", "EndOfWeek, FirstDayThursday")
assert(With(n).EndOfWeek(), "2013-11-21 00:00:00", "EndOfWeek, FirstDayThursday")

WeekStartDay = time.Friday
assert(With(n).EndOfWeek(), "2013-11-21 23:59:59.999999999", "EndOfWeek, FirstDayFriday")
assert(With(n).EndOfWeek(), "2013-11-22 00:00:00", "EndOfWeek, FirstDayFriday")

WeekStartDay = time.Saturday
assert(With(n).EndOfWeek(), "2013-11-22 23:59:59.999999999", "EndOfWeek, FirstDaySaturday")
assert(With(n).EndOfWeek(), "2013-11-23 00:00:00", "EndOfWeek, FirstDaySaturday")

WeekStartDay = time.Sunday
assert(With(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek, FirstDaySunday")
assert(With(n).EndOfWeek(), "2013-11-24 00:00:00", "EndOfWeek, FirstDaySunday")

WeekStartDay = time.Monday
assert(With(n).EndOfWeek(), "2013-11-24 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
assert(With(n).EndOfWeek(), "2013-11-25 00:00:00", "EndOfWeek, FirstDayMonday")

dstEndOfWeek := time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin)
assert(With(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
assert(With(dstEndOfWeek).EndOfWeek(), "2017-10-30 00:00:00", "EndOfWeek, FirstDayMonday")

dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(With(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
assert(With(dstEndOfWeek).EndOfWeek(), "2017-10-30 00:00:00", "EndOfWeek, FirstDayMonday")

WeekStartDay = time.Sunday
assert(With(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek")
assert(With(n).EndOfWeek(), "2013-11-24 00:00:00", "EndOfWeek")

dstEndOfWeek = time.Date(2017, 10, 29, 0, 0, 0, 0, locationBerlin)
assert(With(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
assert(With(dstEndOfWeek).EndOfWeek(), "2017-11-05 00:00:00", "EndOfWeek")

dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(With(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
assert(With(dstEndOfWeek).EndOfWeek(), "2017-11-05 00:00:00", "EndOfWeek")

assert(With(n).EndOfMonth(), "2013-11-30 23:59:59.999999999", "EndOfMonth")
assert(With(n).EndOfMonth(), "2013-12-01 00:00:00", "EndOfMonth")

assert(With(n).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(With(n).EndOfQuarter(), "2014-01-01 00:00:00", "EndOfQuarter")

assert(With(n).EndOfHalf(), "2013-12-31 23:59:59.999999999", "EndOfHalf")
assert(With(n).EndOfHalf(), "2014-01-01 00:00:00", "EndOfHalf")

assert(With(n.AddDate(0, -1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(With(n.AddDate(0, -1, 0)).EndOfQuarter(), "2014-01-01 00:00:00", "EndOfQuarter")

assert(With(n.AddDate(0, 1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(With(n.AddDate(0, 1, 0)).EndOfQuarter(), "2014-01-01 00:00:00", "EndOfQuarter")

assert(With(n.AddDate(0, 1, 0)).EndOfHalf(), "2013-12-31 23:59:59.999999999", "EndOfHalf")
assert(With(n.AddDate(0, 1, 0)).EndOfHalf(), "2014-01-01 00:00:00", "EndOfHalf")

assert(With(n).EndOfYear(), "2013-12-31 23:59:59.999999999", "EndOfYear")
assert(With(n).EndOfYear(), "2014-01-01 00:00:00", "EndOfYear")

n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
assert(With(n1).EndOfMonth(), "2013-02-28 23:59:59.999999999", "EndOfMonth for 2013/02")
assert(With(n1).EndOfMonth(), "2013-03-01 00:00:00", "EndOfMonth for 2013/02")

n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.UTC)
assert(With(n2).EndOfMonth(), "1900-02-28 23:59:59.999999999", "EndOfMonth")
assert(With(n2).EndOfMonth(), "1900-03-01 00:00:00", "EndOfMonth")
}

func TestMondayAndSunday(t *testing.T) {
Expand Down Expand Up @@ -229,11 +229,11 @@ func TestMondayAndSunday(t *testing.T) {

assert(With(nDst).Sunday(), "2017-10-29 00:00:00", "Sunday DST")

assert(With(n).EndOfSunday(), "2013-11-24 23:59:59.999999999", "EndOfSunday")
assert(With(n).EndOfSunday(), "2013-11-25 00:00:00", "EndOfSunday")

assert(With(timeCaracas).EndOfSunday(), "2016-01-03 23:59:59.999999999", "EndOfSunday Caracas")
assert(With(timeCaracas).EndOfSunday(), "2016-01-04 00:00:00", "EndOfSunday Caracas")

assert(With(nDst).EndOfSunday(), "2017-10-29 23:59:59.999999999", "EndOfSunday DST")
assert(With(nDst).EndOfSunday(), "2017-10-30 00:00:00", "EndOfSunday DST")

assert(With(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDayMonday")

Expand Down 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 Expand Up @@ -358,11 +358,11 @@ func TestParse(t *testing.T) {

func TestBetween(t *testing.T) {
tm := time.Date(2015, 06, 30, 17, 51, 49, 123456789, time.Now().Location())
if !With(tm).Between("23:28:9 Dec 19, 2013 PST", "23:28:9 Dec 19, 2015 PST") {
if !With(tm).Between(MustParse("23:28:9 Dec 19, 2013 PST"), MustParse("23:28:9 Dec 19, 2015 PST")) {
t.Errorf("Between")
}

if !With(tm).Between("2015-05-12 12:20", "2015-06-30 17:51:50") {
if !With(tm).Between(MustParse("2015-05-12 12:20"), MustParse("2015-06-30 17:51:50")) {
t.Errorf("Between")
}
}
Expand Down Expand Up @@ -426,24 +426,24 @@ func Example() {
BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
BeginningOfYear() // 2013-01-01 00:00:00 Tue

EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
EndOfMinute() // 2013-11-18 17:52:00 Mon
EndOfHour() // 2013-11-18 18:00:00 Mon
EndOfDay() // 2013-11-19 00:00:00 Tue
EndOfWeek() // 2013-11-24 00:00:00 Sun

WeekStartDay = time.Monday // Set Monday as first day
EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
EndOfWeek() // 2013-11-25 00:00:00 Mon
EndOfMonth() // 2013-12-01 00:00:00 Sun
EndOfQuarter() // 2014-01-01 00:00:00 Wed
EndOfYear() // 2014-01-01 00:00:00 Wed

// Use another time
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
With(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu
With(t).EndOfMonth() // 2013-03-01 00:00:00 Thu

Monday() // 2013-11-18 00:00:00 Mon
Monday("17:44") // 2013-11-18 17:44:00 Mon
Sunday() // 2013-11-24 00:00:00 Sun
Sunday("17:44") // 2013-11-24 17:44:00 Sun
EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun
EndOfSunday() // 2013-11-25 00:00:00 Sun
}