-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShrink+Date.swift
More file actions
52 lines (43 loc) · 1.41 KB
/
Copy pathShrink+Date.swift
File metadata and controls
52 lines (43 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Shrink+Date.swift
// PropertyBased
//
// Created by Lennard Sprong on 30/05/2025.
//
#if canImport(FoundationEssentials)
import FoundationEssentials
#elseif canImport(Foundation)
import Foundation
#endif
#if canImport(FoundationEssentials) || canImport(Foundation)
extension Shrink {
/// Shrink a TimeInterval by rounding to seconds, then minutes, then 15 minute intervals.
public struct TruncateTime: Sequence, IteratorProtocol, Sendable, BitwiseCopyable {
public init(_ value: TimeInterval, roundUp: Bool) {
self.value = value
self.roundUp = roundUp
}
public var value: TimeInterval
public let roundUp: Bool
public mutating func next() -> TimeInterval? {
let rounding: FloatingPointRoundingRule = roundUp ? .up : .down
let withoutMillis = value.rounded(rounding)
if withoutMillis != value {
value = withoutMillis
return withoutMillis
}
let withoutSeconds = (value / 60).rounded(rounding) * 60
if withoutSeconds != value {
value = withoutSeconds
return withoutSeconds
}
let quarter = (value / (60 * 15)).rounded(rounding) * (60 * 15)
if quarter != value {
value = quarter
return quarter
}
return nil
}
}
}
#endif