| 1 | // Copyright 2015 Rick Beton. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package timespanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 9 | "timescip-go gomod github.com/golang/go/src go1.17 time/" |
| 10 | |
| 11 | "github.com/rickb777/datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/" |
| 12 | "github.com/rickb777/date/periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/" |
| 13 | ) |
| 14 | |
| 15 | const minusOneNanoscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/minusOneNano. timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# = -1 |
| 16 | |
| 17 | // DateRange carries a date and a number of days and describes a range between two dates. |
| 18 | type DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# struct { |
| 19 | markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark. datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date# |
| 20 | daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays# |
| 21 | } |
| 22 | |
| 23 | // NewDateRangeOf assembles a new date range from a start time and a duration, discarding |
| 24 | // the precise time-of-day information. The start time includes a location, which is not |
| 25 | // necessarily UTC. The duration can be negative. |
| 26 | func NewDateRangeOfscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewDateRangeOf().(start timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#, duration timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 27 | sd := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.NewAtscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/NewAt().(start) |
| 28 | ed := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.NewAtscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/NewAt().(start.Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(duration)) |
| 29 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{sd, datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(ed.Subscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Sub().(sd))} |
| 30 | } |
| 31 | |
| 32 | // NewDateRange assembles a new date range from two dates. These are half-open, so |
| 33 | // if start and end are the same, the range spans zero (not one) day. Similarly, if they |
| 34 | // are on subsequent days, the range is one date (not two). |
| 35 | // The result is normalised. |
| 36 | func NewDateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewDateRange().(start, end datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 37 | if end.Beforescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Before().(start) { |
| 38 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{end, datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(start.Subscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Sub().(end))} |
| 39 | } |
| 40 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{start, datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(end.Subscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Sub().(start))} |
| 41 | } |
| 42 | |
| 43 | // NewYearOf constructs the range encompassing the whole year specified. |
| 44 | func NewYearOfscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewYearOf().(year int) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 45 | start := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Newscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/New().(year, timescip-go gomod github.com/golang/go/src go1.17 time/.Januaryscip-go gomod github.com/golang/go/src go1.17 time/January., 1) |
| 46 | end := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Newscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/New().(year+1, timescip-go gomod github.com/golang/go/src go1.17 time/.Januaryscip-go gomod github.com/golang/go/src go1.17 time/January., 1) |
| 47 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{start, datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(end.Subscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Sub().(start))} |
| 48 | } |
| 49 | |
| 50 | // NewMonthOf constructs the range encompassing the whole month specified for a given year. |
| 51 | // It handles leap years correctly. |
| 52 | func NewMonthOfscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewMonthOf().(year int, month timescip-go gomod github.com/golang/go/src go1.17 time/.Monthscip-go gomod github.com/golang/go/src go1.17 time/Month#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 53 | start := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Newscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/New().(year, month, 1) |
| 54 | endT := timescip-go gomod github.com/golang/go/src go1.17 time/.Datescip-go gomod github.com/golang/go/src go1.17 time/Date().(year, month+1, 1, 0, 0, 0, 0, timescip-go gomod github.com/golang/go/src go1.17 time/.UTCscip-go gomod github.com/golang/go/src go1.17 time/UTC.) |
| 55 | end := datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.NewAtscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/NewAt().(endT) |
| 56 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{start, datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(end.Subscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Sub().(start))} |
| 57 | } |
| 58 | |
| 59 | // EmptyRange constructs an empty range. This is often a useful basis for |
| 60 | // further operations but note that the end date is undefined. |
| 61 | func EmptyRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/EmptyRange().(day datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 62 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{day, 0} |
| 63 | } |
| 64 | |
| 65 | // OneDayRange constructs a range of exactly one day. This is often a useful basis for |
| 66 | // further operations. Note that the last date is the same as the start date. |
| 67 | func OneDayRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/OneDayRange().(day datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 68 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{day, 1} |
| 69 | } |
| 70 | |
| 71 | // DayRange constructs a range of n days. |
| 72 | // |
| 73 | // Note that n can be negative. In this case, the specified day will be the end day, |
| 74 | // which is outside of the half-open range; the last day will be the day before the |
| 75 | // day specified. |
| 76 | func DayRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DayRange().(day datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#, n datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 77 | if n < 0 { |
| 78 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{day.Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(n), -n} |
| 79 | } |
| 80 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{day, n} |
| 81 | } |
| 82 | |
| 83 | // Days returns the period represented by this range. This will never be negative. |
| 84 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Days().() datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays# { |
| 85 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. < 0 { |
| 86 | return -dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. |
| 87 | } |
| 88 | return dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. |
| 89 | } |
| 90 | |
| 91 | // IsZero returns true if this has a zero start date and the the range is empty. |
| 92 | // Usually this is because the range was created via the zero value. |
| 93 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#IsZero().() bool { |
| 94 | return dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. == 0 && dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#IsZero().() |
| 95 | } |
| 96 | |
| 97 | // IsEmpty returns true if this has a starting date but the range is empty (zero days). |
| 98 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) IsEmptyscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#IsEmpty().() bool { |
| 99 | return dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. == 0 |
| 100 | } |
| 101 | |
| 102 | // Start returns the earliest date represented by this range. |
| 103 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().() datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date# { |
| 104 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. < 0 { |
| 105 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#(1 + dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.)) |
| 106 | } |
| 107 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark. |
| 108 | } |
| 109 | |
| 110 | // Last returns the last date (inclusive) represented by this range. Be careful because |
| 111 | // if the range is empty (i.e. has zero days), then the last is undefined so an empty date |
| 112 | // is returned. Therefore it is often more useful to use End() instead of Last(). |
| 113 | // See also IsEmpty(). |
| 114 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Lastscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Last().() datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date# { |
| 115 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. < 0 { |
| 116 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark. // because mark is at the end |
| 117 | } else if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. == 0 { |
| 118 | return datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#{} |
| 119 | } |
| 120 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. - 1) |
| 121 | } |
| 122 | |
| 123 | // End returns the date following the last date of the range. End can be considered to |
| 124 | // be the exclusive end, i.e. the final value of a half-open range. |
| 125 | // |
| 126 | // If the range is empty (i.e. has zero days), then the start date is returned, this being |
| 127 | // also the (half-open) end value in that case. This is more useful than the undefined result |
| 128 | // returned by Last() for empty ranges. |
| 129 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().() datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date# { |
| 130 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. < 0 { |
| 131 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(1) // because mark is at the end |
| 132 | } |
| 133 | return dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.) |
| 134 | } |
| 135 | |
| 136 | // Normalise ensures that the number of days is zero or positive. |
| 137 | // The normalised date range is returned; |
| 138 | // in this value, the mark date is the same as the start date. |
| 139 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Normalise().() DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 140 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. < 0 { |
| 141 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.), -dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.} |
| 142 | } |
| 143 | return dateRange |
| 144 | } |
| 145 | |
| 146 | // ShiftBy moves the date range by moving both the start and end dates similarly. |
| 147 | // A negative parameter is allowed. |
| 148 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) ShiftByscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#ShiftBy().(days datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 149 | if days == 0 { |
| 150 | return dateRange |
| 151 | } |
| 152 | newMark := dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..Addscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Add().(days) |
| 153 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{newMark, dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.} |
| 154 | } |
| 155 | |
| 156 | // ExtendBy extends (or reduces) the date range by moving the end date. |
| 157 | // A negative parameter is allowed and this may cause the range to become inverted |
| 158 | // (i.e. the mark date becomes the end date instead of the start date). |
| 159 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) ExtendByscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#ExtendBy().(days datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.PeriodOfDaysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/PeriodOfDays#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 160 | if days == 0 { |
| 161 | return dateRange |
| 162 | } |
| 163 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark., dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. + days}.Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Normalise().() |
| 164 | } |
| 165 | |
| 166 | // ShiftByPeriod moves the date range by moving both the start and end dates similarly. |
| 167 | // A negative parameter is allowed. |
| 168 | // |
| 169 | // Any time component is ignored. Therefore, be careful with periods containing |
| 170 | // more that 24 hours in the hours/minutes/seconds fields. These will not be |
| 171 | // normalised for you; if you want this behaviour, call delta.Normalise(false) |
| 172 | // on the input parameter. |
| 173 | // |
| 174 | // For example, PT24H adds nothing, whereas P1D adds one day as expected. To |
| 175 | // convert a period such as PT24H to its equivalent P1D, use |
| 176 | // delta.Normalise(false) as the input. |
| 177 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) ShiftByPeriodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#ShiftByPeriod().(delta periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/.Periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 178 | if delta.IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#IsZero().() { |
| 179 | return dateRange |
| 180 | } |
| 181 | newMark := dateRange.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark..AddPeriodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#AddPeriod().(delta) |
| 182 | //fmt.Printf("mark + %v : %v -> %v", delta, dateRange.mark, newMark) |
| 183 | return DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#{newMark, dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days.} |
| 184 | } |
| 185 | |
| 186 | // ExtendByPeriod extends (or reduces) the date range by moving the end date. |
| 187 | // A negative parameter is allowed and this may cause the range to become inverted |
| 188 | // (i.e. the mark date becomes the end date instead of the start date). |
| 189 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) ExtendByPeriodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#ExtendByPeriod().(delta periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/.Periodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 190 | if delta.IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/period`/Period#IsZero().() { |
| 191 | return dateRange |
| 192 | } |
| 193 | newEnd := dateRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().().AddPeriodscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#AddPeriod().(delta) |
| 194 | //fmt.Printf("%v, end + %v : %v -> %v", dateRange.mark, delta, dateRange.End(), newEnd) |
| 195 | return NewDateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewDateRange().(dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().(), newEnd) |
| 196 | } |
| 197 | |
| 198 | // String describes the date range in human-readable form. |
| 199 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Stringscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#String().() string { |
| 200 | norm := dateRange.Normalisescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Normalise().() |
| 201 | switch norm.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. { |
| 202 | case 0: |
| 203 | return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("0 days at %s", norm.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark.) |
| 204 | case 1: |
| 205 | return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("1 day on %s", norm.markscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#mark.) |
| 206 | default: |
| 207 | return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%d days from %s to %s", norm.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days., norm.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().(), norm.Lastscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Last().()) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // Contains tests whether the date range contains a specified date. |
| 212 | // Empty date ranges (i.e. zero days) never contain anything. |
| 213 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Containsscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Contains().(d datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/.Datescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#) bool { |
| 214 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. == 0 { |
| 215 | return false |
| 216 | } |
| 217 | return !(d.Beforescip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Before().(dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().()) || d.Afterscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#After().(dateRange.Lastscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Last().())) |
| 218 | } |
| 219 | |
| 220 | // StartUTC assumes that the start date is a UTC date and gets the start time of that date, as UTC. |
| 221 | // It returns midnight on the first day of the range. |
| 222 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) StartUTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#StartUTC().() timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# { |
| 223 | return dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().().UTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#UTC().() |
| 224 | } |
| 225 | |
| 226 | // EndUTC assumes that the end date is a UTC date and returns the time a nanosecond after the end time |
| 227 | // in a specified location. Along with StartUTC, this gives a 'half-open' range where the start |
| 228 | // is inclusive and the end is exclusive. |
| 229 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) EndUTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#EndUTC().() timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# { |
| 230 | return dateRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().().UTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#UTC().() |
| 231 | } |
| 232 | |
| 233 | // ContainsTime tests whether a given local time is within the date range. The time range is |
| 234 | // from midnight on the start day to one nanosecond before midnight on the day after the end date. |
| 235 | // Empty date ranges (i.e. zero days) never contain anything. |
| 236 | // |
| 237 | // If a calculation needs to be 'half-open' (i.e. the end date is exclusive), simply use the |
| 238 | // expression 'dateRange.ExtendBy(-1).ContainsTime(t)' |
| 239 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) ContainsTimescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#ContainsTime().(t timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time#) bool { |
| 240 | if dateRange.daysscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#days. == 0 { |
| 241 | return false |
| 242 | } |
| 243 | utc := t.Inscip-go gomod github.com/golang/go/src go1.17 time/Time#In().(timescip-go gomod github.com/golang/go/src go1.17 time/.UTCscip-go gomod github.com/golang/go/src go1.17 time/UTC.) |
| 244 | return !(utc.Beforescip-go gomod github.com/golang/go/src go1.17 time/Time#Before().(dateRange.StartUTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#StartUTC().()) || dateRange.EndUTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#EndUTC().().Addscip-go gomod github.com/golang/go/src go1.17 time/Time#Add().(minusOneNanoscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/minusOneNano.).Beforescip-go gomod github.com/golang/go/src go1.17 time/Time#Before().(utc)) |
| 245 | } |
| 246 | |
| 247 | // Merge combines two date ranges by calculating a date range that just encompasses them both. |
| 248 | // There are two special cases. |
| 249 | // |
| 250 | // Firstly, if one range is entirely contained within the other range, the larger of the two is |
| 251 | // returned. Otherwise, the result is from the start of the earlier one to the end of the later |
| 252 | // one, even if the two ranges don't overlap. |
| 253 | // |
| 254 | // Secondly, if either range is the zero value (see IsZero), it is excluded from the merge and |
| 255 | // the other range is returned unchanged. |
| 256 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Mergescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Merge().(otherRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange# { |
| 257 | if otherRange.IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#IsZero().() { |
| 258 | return dateRange |
| 259 | } |
| 260 | if dateRange.IsZeroscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#IsZero().() { |
| 261 | return otherRange |
| 262 | } |
| 263 | minStart := dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().().Minscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Min().(otherRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().()) |
| 264 | maxEnd := dateRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().().Maxscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#Max().(otherRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().()) |
| 265 | return NewDateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/NewDateRange().(minStart, maxEnd) |
| 266 | } |
| 267 | |
| 268 | // Duration computes the duration (in nanoseconds) from midnight at the start of the date |
| 269 | // range up to and including the very last nanosecond before midnight on the end day. |
| 270 | // The calculation is for UTC, which does not have daylight saving and every day has 24 hours. |
| 271 | // |
| 272 | // If the range is greater than approximately 290 years, the result will hard-limit to the |
| 273 | // minimum or maximum possible duration (see time.Sub(t)). |
| 274 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) Durationscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Duration().() timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# { |
| 275 | return dateRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().().UTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#UTC().().Subscip-go gomod github.com/golang/go/src go1.17 time/Time#Sub().(dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().().UTCscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#UTC().()) |
| 276 | } |
| 277 | |
| 278 | // DurationIn computes the duration (in nanoseconds) from midnight at the start of the date |
| 279 | // range up to and including the very last nanosecond before midnight on the end day. |
| 280 | // The calculation is for the specified location, which may have daylight saving, so not every day |
| 281 | // necessarily has 24 hours. If the date range spans the day the clocks are changed, this is |
| 282 | // taken into account. |
| 283 | // |
| 284 | // If the range is greater than approximately 290 years, the result will hard-limit to the |
| 285 | // minimum or maximum possible duration (see time.Sub(t)). |
| 286 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) DurationInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#DurationIn().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) timescip-go gomod github.com/golang/go/src go1.17 time/.Durationscip-go gomod github.com/golang/go/src go1.17 time/Duration# { |
| 287 | return dateRange.EndTimeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#EndTimeIn().(loc).Subscip-go gomod github.com/golang/go/src go1.17 time/Time#Sub().(dateRange.StartTimeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#StartTimeIn().(loc)) |
| 288 | } |
| 289 | |
| 290 | // StartTimeIn returns the start time in a specified location. |
| 291 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) StartTimeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#StartTimeIn().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# { |
| 292 | return dateRange.Startscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#Start().().Inscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#In().(loc) |
| 293 | } |
| 294 | |
| 295 | // EndTimeIn returns the nanosecond after the end time in a specified location. Along with |
| 296 | // StartTimeIn, this gives a 'half-open' range where the start is inclusive and the end is |
| 297 | // exclusive. |
| 298 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) EndTimeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#EndTimeIn().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) timescip-go gomod github.com/golang/go/src go1.17 time/.Timescip-go gomod github.com/golang/go/src go1.17 time/Time# { |
| 299 | return dateRange.Endscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#End().().Inscip-go gomod github.com/rickb777/date . `github.com/rickb777/date`/Date#In().(loc) |
| 300 | } |
| 301 | |
| 302 | // TimeSpanIn obtains the time span corresponding to the date range in a specified location. |
| 303 | // The result is normalised. |
| 304 | func (dateRange DateRangescip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#) TimeSpanInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#TimeSpanIn().(loc *timescip-go gomod github.com/golang/go/src go1.17 time/.Locationscip-go gomod github.com/golang/go/src go1.17 time/Location#) TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan# { |
| 305 | s := dateRange.StartTimeInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#StartTimeIn().(loc) |
| 306 | d := dateRange.DurationInscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/DateRange#DurationIn().(loc) |
| 307 | return TimeSpanscip-go gomod github.com/rickb777/date . `github.com/rickb777/date/timespan`/TimeSpan#{s, d} |
| 308 | } |