|
2 | 2 | //these methods wrap around them. |
3 | 3 | import ms from '../../data/milliseconds.js' |
4 | 4 | import { mapping } from '../../data/months.js' |
5 | | -import monthLength from '../../data/monthLengths.js' |
6 | 5 | import walkTo from './walk.js' |
7 | 6 | import { isLeapYear } from '../../fns.js' |
| 7 | +import { getMonthLength } from './_model.js' |
8 | 8 |
|
9 | 9 | const validate = (n) => { |
10 | 10 | //handle number as a string |
@@ -142,14 +142,9 @@ const time = function (s, str, goFwd) { |
142 | 142 |
|
143 | 143 | const date = function (s, n, goFwd) { |
144 | 144 | n = validate(n) |
145 | | - //avoid setting february 31st |
| 145 | + //avoid setting february 31st (leap-aware, same as add()) |
146 | 146 | if (n > 28) { |
147 | | - const month = s.month() |
148 | | - let max = monthLength[month] |
149 | | - // support leap day in february |
150 | | - if (month === 1 && n === 29 && isLeapYear(s.year())) { |
151 | | - max = 29 |
152 | | - } |
| 147 | + const max = getMonthLength(s.month(), s.year()) |
153 | 148 | if (n > max) { |
154 | 149 | n = max |
155 | 150 | } |
@@ -183,15 +178,16 @@ const month = function (s, n, goFwd) { |
183 | 178 | } |
184 | 179 |
|
185 | 180 | let d = s.date() |
186 | | - //there's no 30th of february, etc. |
187 | | - if (d > monthLength[n]) { |
| 181 | + //there's no 30th of february, etc. (leap-aware, same as add()) |
| 182 | + const max = getMonthLength(n, s.year()) |
| 183 | + if (d > max) { |
188 | 184 | //make it as close as we can.. |
189 | | - d = monthLength[n] |
| 185 | + d = max |
190 | 186 | } |
191 | 187 | const old = s.clone() |
192 | 188 | walkTo(s, { |
193 | 189 | month: n, |
194 | | - d |
| 190 | + date: d |
195 | 191 | }) |
196 | 192 | s = fwdBkwd(s, old, goFwd, 'year') // specify direction |
197 | 193 | return s.epoch |
|
0 commit comments