Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@ public extension DateInRegion {
/// - Returns: rounded date
func dateRoundedAt(_ style: RoundDateMode) -> DateInRegion {
switch style {
case .to1Sec: return dateRoundedAt(.toSecs(1))
case .to1Min: return dateRoundedAt(.toMins(1))
case .to5Mins: return dateRoundedAt(.toMins(5))
case .to10Mins: return dateRoundedAt(.toMins(10))
case .to30Mins: return dateRoundedAt(.toMins(30))
Expand Down Expand Up @@ -352,6 +354,24 @@ public extension DateInRegion {
let value = -((Int(1.minutes.timeInterval) * remain) + second)
return dateByAdding(value, .second)


case .toSecs(let secondInterval):
let onesDigit: Int = (second % 10)
if onesDigit < 5 {
return dateRoundedAt(.toFloorSecs(secondInterval))
} else {
return dateRoundedAt(.toCeilSecs(secondInterval))
}

case .toCeilSecs(let secondInterval):
let remain: Int = (second % secondInterval)
let value = (( Int(1.seconds.timeInterval) * (secondInterval - remain)) - nanosecond)
return dateByAdding(value, .nanosecond)

case .toFloorSecs(let secondInterval):
let remain: Int = (second % secondInterval)
let value = -((Int(1.seconds.timeInterval) * remain) + nanosecond)
return dateByAdding(value, .nanosecond)
}
}

Expand Down
5 changes: 5 additions & 0 deletions Sources/SwiftDate/Supports/Commons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,18 +236,23 @@ public extension Calendar.Component {
/// Rounding mode for dates.
/// Round off/up (ceil) or down (floor) target date.
public enum RoundDateMode {
case to1Sec
case to1Min
case to5Mins
case to10Mins
case to30Mins
case toMins(_: Int)
case toSecs(_: Int)
case toCeil5Mins
case toCeil10Mins
case toCeil30Mins
case toCeilMins(_: Int)
case toCeilSecs(_: Int)
case toFloor5Mins
case toFloor10Mins
case toFloor30Mins
case toFloorMins(_: Int)
case toFloorSecs(_: Int)
}

/// Related type enum to get derivated date from a receiver date.
Expand Down