@@ -236,15 +236,15 @@ public enum DateMagnitude: Equatable {
236236 case months
237237 /// Years.
238238 case years
239-
239+
240240 static let subsecondThreshold : PartialRangeUpTo < Double > = ..< 1
241241 static let secondsThreshold : Range < Double > = 1 ..< 60
242242 static let minutesThreshold : Range < Double > = 60 ..< 60 * 60
243243 static let hoursThreshold : Range < Double > = 60 * 60 ..< 60 * 60 * 24
244244 static let daysThreshold : Range < Double > = 60 * 60 * 24 ..< 60 * 60 * 24 * 28
245245 static let monthsThreshold : Range < Double > = 60 * 60 * 24 * 28 ..< 60 * 60 * 24 * 365
246246 static let yearsThreshold : PartialRangeFrom < Double > = ( 60 * 60 * 24 * 365 ) ...
247-
247+
248248 /// The value of the date magnitude in seconds per increment.
249249 public var seconds : Double {
250250 switch self {
@@ -299,6 +299,7 @@ public extension Date {
299299 case . subsecond:
300300 if let asNanoseconds = components. nanosecond {
301301 components. nanosecond = Int . niceVersion ( for: asNanoseconds, trendTowardsZero: true )
302+ assert ( components. isValidDate)
302303 }
303304 assert ( components. isValidDate)
304305 return components. date
@@ -307,6 +308,7 @@ public extension Date {
307308 if let stepSize = stepSize, let seconds = components. second {
308309 let valueRoundedByStep = floor ( Double ( seconds) / stepSize) * stepSize
309310 components. setValue ( Int ( valueRoundedByStep) , for: . second)
311+ assert ( components. isValidDate)
310312 }
311313 assert ( components. isValidDate)
312314 return components. date
@@ -317,6 +319,7 @@ public extension Date {
317319 let stepSizeInMinutes = stepSize / 60
318320 let valueRoundedByStep = floor ( Double ( minutes) / stepSizeInMinutes) * stepSizeInMinutes
319321 components. setValue ( Int ( valueRoundedByStep) , for: . minute)
322+ assert ( components. isValidDate)
320323 }
321324 assert ( components. isValidDate)
322325 return components. date
@@ -328,6 +331,7 @@ public extension Date {
328331 let stepSizeInHours = stepSize / ( 60 * 60 )
329332 let valueRoundedByStep = floor ( Double ( hours) / stepSizeInHours) * stepSizeInHours
330333 components. setValue ( Int ( valueRoundedByStep) , for: . hour)
334+ assert ( components. isValidDate)
331335 }
332336 assert ( components. isValidDate)
333337 return components. date
@@ -339,7 +343,12 @@ public extension Date {
339343 if let stepSize = stepSize, let days = components. day {
340344 let stepSizeInDays = stepSize / ( 24 * 60 * 60 )
341345 let valueRoundedByStep = floor ( Double ( days) / stepSizeInDays) * stepSizeInDays
342- components. setValue ( Int ( valueRoundedByStep) , for: . day)
346+ if valueRoundedByStep < 1.0 {
347+ components. setValue ( 1 , for: . day)
348+ } else {
349+ components. setValue ( Int ( valueRoundedByStep) , for: . day)
350+ }
351+ assert ( components. isValidDate)
343352 }
344353 assert ( components. isValidDate)
345354 return components. date
@@ -352,7 +361,12 @@ public extension Date {
352361 if let stepSize = stepSize, let months = components. month {
353362 let stepSizeInMonths = stepSize / ( 28 * 24 * 60 * 60 )
354363 let valueRoundedByStep = floor ( Double ( months) / stepSizeInMonths) * stepSizeInMonths
355- components. setValue ( Int ( valueRoundedByStep) , for: . month)
364+ if valueRoundedByStep < 1.0 {
365+ components. setValue ( 1 , for: . month)
366+ } else {
367+ components. setValue ( Int ( valueRoundedByStep) , for: . month)
368+ }
369+ assert ( components. isValidDate)
356370 }
357371 assert ( components. isValidDate)
358372 return components. date
@@ -367,7 +381,7 @@ public extension Date {
367381 return components. date
368382 }
369383 }
370-
384+
371385 /// Returns a date based on the current value, rounded 'up' to the next incremental step value.
372386 /// - Parameters:
373387 /// - magnitude: The magnitude to which to round.
@@ -376,13 +390,13 @@ public extension Date {
376390 func roundUp( magnitude: DateMagnitude , calendar: Calendar , stepSize: Double ? = nil ) -> Self ? {
377391 let incDate : Date
378392 if let stepSize = stepSize, stepSize >= magnitude. seconds {
379- incDate = self + stepSize
393+ incDate = self + stepSize
380394 } else {
381- incDate = self + magnitude. seconds
395+ incDate = self + magnitude. seconds
382396 }
383397 return incDate. round ( magnitude: magnitude, calendar: calendar, stepSize: stepSize)
384398 }
385-
399+
386400 /// Returns a nice step size for the magnitude of date range you provide.
387401 /// - Parameters:
388402 /// - step: The step size (in seconds) for the date increment.
0 commit comments