fix(boundary): preserve instants at minute boundaries
same bug as #377, one level up: StartOfMinute/EndOfMinute rebuilt the time with time.Date, so in a repeated hour new york 01:34:49 EST got a minute ending before it. they now copy the instance and subtract the local seconds, keeping the instant, location and offset (not Truncate(time.Minute), which breaks lmt offsets with seconds).
This commit is contained in:
1 parent
3dbfef56fe
commit
ddbdba233d
2 files changed
+41
-4
No files matched your search
+8
-4
@@ -153,8 +153,10 @@ func (c *Carbon) StartOfMinute() *Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
year, month, day, hour, minute, _ := c.DateTime()
|
||||
return c.create(year, month, day, hour, minute, MinSecond, MinNanosecond)
|
||||
elapsed := time.Duration(c.Second())*time.Second + time.Duration(c.Nanosecond())
|
||||
result := c.Copy()
|
||||
result.time = result.time.Add(-elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
// EndOfMinute returns a Carbon instance for end of the minute.
|
||||
@@ -162,8 +164,10 @@ func (c *Carbon) EndOfMinute() *Carbon {
|
||||
if c.IsInvalid() {
|
||||
return c
|
||||
}
|
||||
year, month, day, hour, minute, _ := c.DateTime()
|
||||
return c.create(year, month, day, hour, minute, MaxSecond, MaxNanosecond)
|
||||
elapsed := time.Duration(c.Second())*time.Second + time.Duration(c.Nanosecond())
|
||||
result := c.Copy()
|
||||
result.time = result.time.Add(time.Minute - time.Nanosecond - elapsed)
|
||||
return result
|
||||
}
|
||||
|
||||
// StartOfSecond returns a Carbon instance for start of the second.
|
||||
|
||||
Reference in new issue
Block a user