@@ -179,6 +179,109 @@ final class DateNiceValueTests: XCTestCase {
179179 )
180180 }
181181
182+ func testCalendricalDateRoundUp_seconds( ) throws {
183+ let ( formatter, now) = formatterAndNow ( )
184+ // starting position for comparison
185+ XCTAssertEqual ( formatter. string ( from: now) , " 2022-08-08T22:43:09.011Z " )
186+
187+ let result_1 = now. roundUp ( magnitude: . seconds, calendar: testCal, stepSize: 1 )
188+ // step size of 1 is same result as not providing it for seconds
189+ XCTAssertEqual ( formatter. string ( from: result_1!) , " 2022-08-08T22:43:10.000Z " )
190+
191+ let result_2 = now. roundUp ( magnitude: . seconds, calendar: testCal, stepSize: 2 )
192+ XCTAssertEqual ( formatter. string ( from: result_2!) , " 2022-08-08T22:43:10.000Z " )
193+
194+ let result_5 = now. roundUp ( magnitude: . seconds, calendar: testCal, stepSize: 5 )
195+ XCTAssertEqual ( formatter. string ( from: result_5!) , " 2022-08-08T22:43:10.000Z " )
196+ }
197+
198+ func testCalendricalDateRoundUp_minutes( ) throws {
199+ let ( formatter, now) = formatterAndNow ( )
200+ // starting position for comparison
201+ XCTAssertEqual ( formatter. string ( from: now) , " 2022-08-08T22:43:09.011Z " )
202+
203+ XCTAssertEqual (
204+ formatter. string ( from: now. roundUp ( magnitude: . minutes, calendar: testCal, stepSize: 1 * 60 ) !) ,
205+ " 2022-08-08T22:44:00.000Z "
206+ )
207+
208+ XCTAssertEqual (
209+ formatter. string ( from: now. roundUp ( magnitude: . minutes, calendar: testCal, stepSize: 2 * 60 ) !) ,
210+ " 2022-08-08T22:44:00.000Z "
211+ )
212+
213+ XCTAssertEqual (
214+ formatter. string ( from: now. roundUp ( magnitude: . minutes, calendar: testCal, stepSize: 5 * 60 ) !) ,
215+ " 2022-08-08T22:45:00.000Z "
216+ )
217+ }
218+
219+ func testCalendricalDateRoundUp_hours( ) throws {
220+ let ( formatter, now) = formatterAndNow ( )
221+ // starting position for comparison
222+ XCTAssertEqual ( formatter. string ( from: now) , " 2022-08-08T22:43:09.011Z " )
223+
224+ XCTAssertEqual (
225+ formatter. string ( from: now. roundUp ( magnitude: . hours, calendar: testCal, stepSize: 1 * 60 * 60 ) !) ,
226+ " 2022-08-08T23:00:00.000Z "
227+ )
228+
229+ XCTAssertEqual (
230+ formatter. string ( from: now. round ( magnitude: . hours, calendar: testCal, stepSize: 2 * 60 * 60 ) !) ,
231+ " 2022-08-09T00:00:00.000Z "
232+ )
233+
234+ XCTAssertEqual (
235+ formatter. string ( from: now. round ( magnitude: . hours, calendar: testCal, stepSize: 6 * 60 * 60 ) !) ,
236+ " 2022-08-09T00:00:00.000Z "
237+ )
238+ }
239+
240+ func testCalendricalDateRoundUp_days( ) throws {
241+ let ( formatter, now) = formatterAndNow ( )
242+ // starting position for comparison
243+ XCTAssertEqual ( formatter. string ( from: now) , " 2022-08-08T22:43:09.011Z " )
244+
245+ XCTAssertEqual (
246+ formatter. string ( from: now. round ( magnitude: . days, calendar: testCal, stepSize: 1 * 24 * 60 * 60 ) !) ,
247+ " 2022-08-09T00:00:00.000Z "
248+ )
249+
250+ XCTAssertEqual (
251+ formatter. string ( from: now. round ( magnitude: . days, calendar: testCal, stepSize: 2 * 24 * 60 * 60 ) !) ,
252+ " 2022-08-10T00:00:00.000Z "
253+ )
254+
255+ // NOTE(heckj): This is rounding down to the 7th day of the month rather than to a specific "day of week"
256+ // and we might want to choose the other path in terms of "nice values" that are spanning 7 day (week)
257+ // increments.
258+ XCTAssertEqual (
259+ formatter. string ( from: now. round ( magnitude: . days, calendar: testCal, stepSize: 7 * 24 * 60 * 60 ) !) ,
260+ " 2022-08-15T00:00:00.000Z "
261+ )
262+ }
263+
264+ func testCalendricalDateRoundUp_months( ) throws {
265+ let ( formatter, now) = formatterAndNow ( )
266+ // starting position for comparison
267+ XCTAssertEqual ( formatter. string ( from: now) , " 2022-08-08T22:43:09.011Z " )
268+
269+ XCTAssertEqual (
270+ formatter. string ( from: now. round ( magnitude: . months, calendar: testCal, stepSize: 1 * 28 * 24 * 60 * 60 ) !) ,
271+ " 2022-09-01T00:00:00.000Z "
272+ )
273+
274+ XCTAssertEqual (
275+ formatter. string ( from: now. round ( magnitude: . months, calendar: testCal, stepSize: 2 * 28 * 24 * 60 * 60 ) !) ,
276+ " 2022-09-01T00:00:00.000Z "
277+ )
278+
279+ XCTAssertEqual (
280+ formatter. string ( from: now. round ( magnitude: . months, calendar: testCal, stepSize: 6 * 28 * 24 * 60 * 60 ) !) ,
281+ " 2022-12-01T00:00:00.000Z "
282+ )
283+ }
284+
182285 func testNiceStepForDateMagnitudes_subseconds( ) throws {
183286 XCTAssertEqual ( Date . niceStepForMagnitude ( step: 0.10 , magnitude: . subsecond) , 0.1 )
184287 XCTAssertEqual ( Date . niceStepForMagnitude ( step: 0.11 , magnitude: . subsecond) , 0.2 )
0 commit comments