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

Precise start and end of day #6

Open
wants to merge 3 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
138 changes: 126 additions & 12 deletions moment.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ var (
ordinal = regexp.MustCompile("([0-9]+)(st|nd|rd|th)")
written = regexp.MustCompile(regex_numbers)
relativediff = regexp.MustCompile(`([\+\-])?([0-9]+),? ?(` + regex_period + `)s?`)
relativetime = regexp.MustCompile(`(?P<hour>\d\d?):(?P<minutes>\d\d?)(:(?P<seconds>\d\d?))?\s?(?P<meridiem>am|pm)?\s?(?P<zone>[a-z]{3,3})?|(?P<relativetime>noon|midnight)`)
relativetime = regexp.MustCompile(`(?P<hour>\d\d?):(?P<minutes>\d\d?)(:(?P<seconds>\d\d?))?(.(?P<nanoseconds>\d{1,9}))?\s?(?P<meridiem>am|pm)?\s?(?P<zone>[a-z]{3,3})?|(?P<relativetime>noon|midnight)`)
yearmonthday = regexp.MustCompile(`(?P<year>\d{4})-(?P<month>\d{1,2})-(?P<day>\d{1,2})`)
relativeperiod = regexp.MustCompile(`(?P<relperiod>this|next|last) (week|month|year)`)
numberRegex = regexp.MustCompile("([0-9]+)(?:<stdOrdinal>)")
Expand Down Expand Up @@ -218,9 +218,10 @@ func (m *Moment) Strtotime(str string) *Moment {

// Try to parse out time from the string
var timeDefaults = map[string]int{
"hour": 0,
"minutes": 0,
"seconds": 0,
"hour": 0,
"minutes": 0,
"seconds": 0,
"nanoseconds": 0,
}

timeMatches := timeDefaults
Expand Down Expand Up @@ -249,7 +250,10 @@ func (m *Moment) Strtotime(str string) *Moment {
timeMatches["hour"] += 12
}

if name == "hour" || name == "minutes" || name == "seconds" {
if name == "hour" || name == "minutes" || name == "seconds" || name == "nanoseconds" {
if name == "nanoseconds" {
match[i] = rightPad2Len(match[i], "0", 9)
}
timeMatches[name], _ = strconv.Atoi(match[i])
}
}
Expand Down Expand Up @@ -434,7 +438,7 @@ func (m *Moment) Strtotime(str string) *Moment {

// @todo deal with timezone
func (m *Moment) strtotimeSetTime(time map[string]int, zone string) {
m.SetHour(time["hour"]).SetMinute(time["minutes"]).SetSecond(time["seconds"])
m.SetHour(time["hour"]).SetMinute(time["minutes"]).SetSecond(time["seconds"]).SetNanoSecond(time["nanoseconds"])
}

func (m *Moment) strtotimeSetDate(date map[string]int) {
Expand All @@ -453,12 +457,26 @@ func (m Moment) Clone() *Moment {
*
*/
// https://groups.google.com/forum/#!topic/golang-nuts/pret7hjDc70
func (m *Moment) Millisecond() {
func (m *Moment) Second() int {
return m.GetTime().Second()
}

func (m *Moment) MilliSecond() int {
return m.GetTime().Nanosecond() / int(time.Millisecond)
}

func (m *Moment) Second() int {
return m.GetTime().Second()
func (m *Moment) MicroSecond(onlyMicro bool) int {
if onlyMicro {
return (m.GetTime().Nanosecond() / int(time.Microsecond)) % 1000
}
return m.GetTime().Nanosecond() / int(time.Microsecond)
}

func (m *Moment) NanoSecond(onlyNano bool) int {
if onlyNano {
return m.GetTime().Nanosecond() % 1000
}
return m.GetTime().Nanosecond()
}

func (m *Moment) Minute() int {
Expand Down Expand Up @@ -593,12 +611,32 @@ func (m *Moment) Add(key string, value int) *Moment {
case "seconds", "second", "s":
m.AddSeconds(value)
case "milliseconds", "millisecond", "ms":
m.AddMilliSeconds(value)
case "microseconds", "microsecond", "us":
m.AddMicroSeconds(value)
case "nanoseconds", "nanosecond", "ns":
m.AddNanoSeconds(value)

}

return m
}

// Carbon
func (m *Moment) AddNanoSeconds(ns int) *Moment {
return m.addTime(time.Nanosecond * time.Duration(ns))
}

// Carbon
func (m *Moment) AddMicroSeconds(us int) *Moment {
return m.addTime(time.Microsecond * time.Duration(us))
}

// Carbon
func (m *Moment) AddMilliSeconds(ms int) *Moment {
return m.addTime(time.Millisecond * time.Duration(ms))
}

// Carbon
func (m *Moment) AddSeconds(seconds int) *Moment {
return m.addTime(time.Second * time.Duration(seconds))
Expand Down Expand Up @@ -668,6 +706,11 @@ func (m *Moment) Subtract(key string, value int) *Moment {
case "seconds", "second", "s":
m.SubSeconds(value)
case "milliseconds", "millisecond", "ms":
m.SubMilliSeconds(value)
case "microseconds", "microsecond", "us":
m.SubMicroSeconds(value)
case "nanoseconds", "nanosecond", "ns":
m.SubNanoSeconds(value)

}

Expand All @@ -679,6 +722,18 @@ func (m *Moment) SubSeconds(seconds int) *Moment {
return m.addTime(time.Second * time.Duration(seconds*-1))
}

func (m *Moment) SubMilliSeconds(ms int) *Moment {
return m.addTime(time.Millisecond * time.Duration(ms*-1))
}

func (m *Moment) SubMicroSeconds(us int) *Moment {
return m.addTime(time.Microsecond * time.Duration(us*-1))
}

func (m *Moment) SubNanoSeconds(ns int) *Moment {
return m.addTime(time.Nanosecond * time.Duration(ns*-1))
}

// Carbon
func (m *Moment) SubMinutes(minutes int) *Moment {
return m.addTime(time.Minute * time.Duration(minutes*-1))
Expand Down Expand Up @@ -743,14 +798,36 @@ func (m *Moment) StartOf(key string) *Moment {
m.SubMinutes(m.Minute())
}

if m.Second() > 0 {
m.SubSeconds(m.Second())
}
m.StartOf("minute")
case "minute", "m":
if m.Second() > 0 {
m.SubSeconds(m.Second())
}

m.StartOf("second")
case "second", "s":
if m.MilliSecond() > 0 {
m.SubMilliSeconds(m.MilliSecond())
}

m.StartOf("millisecond")

case "millisecond", "ms":
if m.MicroSecond(true) > 0 {
m.SubMicroSeconds(m.MicroSecond(true))
}

m.StartOf("microsecond")

case "microsecond", "us":
if m.NanoSecond(true) > 0 {
m.SubNanoSeconds(m.NanoSecond(true))
}

m.StartOf("nanosecond")

case "nanosecond", "ns":
// nothing to do

}

Expand Down Expand Up @@ -804,11 +881,33 @@ func (m *Moment) EndOf(key string) *Moment {
if m.Minute() < 59 {
m.AddMinutes(59 - m.Minute())
}

m.EndOf("minute")
case "minute", "m":
if m.Second() < 59 {
m.AddSeconds(59 - m.Second())
}

m.EndOf("second")
case "second", "s":
if m.MilliSecond() < 999 {
m.AddMilliSeconds(999 - m.MilliSecond())
}

m.EndOf("millisecond")
case "millisecond", "ms":
if m.MicroSecond(true) < 999 {
m.AddMicroSeconds(999 - m.MicroSecond(true))
}

m.EndOf("microsecond")
case "microsecond", "us":
if m.NanoSecond(true) < 999 {
m.AddNanoSeconds(999 - m.NanoSecond(true))
}

m.EndOf("nanosecond")
case "nanosecond", "ns":

}

Expand Down Expand Up @@ -919,6 +1018,14 @@ func (m *Moment) GoBackToMonth(month time.Month, previous bool) *Moment {
return m.SubMonths(diff * -1)
}

func (m *Moment) SetNanoSecond(nanoseconds int) *Moment {
if nanoseconds >= 0 && nanoseconds <= 1000000000 {
return m.AddNanoSeconds(nanoseconds - m.NanoSecond(false))
}

return m
}

func (m *Moment) SetSecond(seconds int) *Moment {
if seconds >= 0 && seconds <= 60 {
return m.AddSeconds(seconds - m.Second())
Expand Down Expand Up @@ -1183,3 +1290,10 @@ func (m *Moment) IsLeapYear() bool {
func (m *Moment) Range(start Moment, end Moment) bool {
return m.IsAfter(start) && m.IsBefore(end)
}

func rightPad2Len(s string, padStr string, overallLen int) string {
var padCountInt int
padCountInt = 1 + ((overallLen - len(padStr)) / len(padStr))
var retStr = s + strings.Repeat(padStr, padCountInt)
return retStr[:overallLen]
}
8 changes: 4 additions & 4 deletions moment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,22 @@ func TestEndOfDay(t *testing.T) {
{
// Clocks +1 hour
before: NewMoment(time.Date(2015, 3, 29, 0, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 3, 29, 23, 59, 0, 0, lon)),
after: NewMoment(time.Date(2015, 3, 29, 23, 59, 59, 999999999, lon)),
},
{
// Clocks -1 hour
before: NewMoment(time.Date(2015, 10, 25, 2, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 59, 999999999, lon)),
},
{
// Clocks -1 hour (same as above)
before: NewMoment(time.Date(2015, 10, 25, 12, 0, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 0, 0, lon)),
after: NewMoment(time.Date(2015, 10, 25, 23, 59, 59, 999999999, lon)),
},
{
// Clocks no change
before: NewMoment(time.Date(2016, 1, 01, 10, 0, 0, 0, lon)),
after: NewMoment(time.Date(2016, 1, 01, 23, 59, 0, 0, lon)),
after: NewMoment(time.Date(2016, 1, 01, 23, 59, 59, 999999999, lon)),
},
}

Expand Down