← 모든 예제
예제 04
Time buckets
Time without a clock: every instant comes from the data. Bucketing
- 분류
- Pure: 어디서나 실행
- 권한
- 없음
- 언어
>=0.1.1, <0.2.0
소스
// Category: pure // Permissions: none // Time without a clock: every instant comes from the data. Bucketing // trade times into 15-minute bars, counting per bar, and durations as // exact seconds. lang ">=0.1.1, <0.2.0"; import std.time; import std.list; fn main() { let day = timestamp("2024-03-01T00:00:00Z"); let offsets = [3min, 9min, 14min + 59s, 15min, 31min, 44min, 46min]; let mut buckets: List<Timestamp> = []; let mut counts: List<Int> = []; for offset in offsets { let bar = time.floor(day + offset, 15min); match buckets.last() { Some(last) => { if last == bar { let n = counts.len() - 1; let current = counts.get(n).expect("one count per bucket"); counts = list.concat(list.take(counts, n), [current + 1]); } else { buckets.push(bar); counts.push(1); } } None => { buckets.push(bar); counts.push(1); } } } let mut i = 0; while i < buckets.len() { print(to_string(buckets[i]) + " " + to_string(counts[i]) + " trade(s)"); i = i + 1; } let span = (day + 46min) - (day + 3min); print("first to last: " + to_string(time.seconds(span)) + " s"); print("as unix ms: " + to_string(time.unix_ms(day))); print(time.from_seconds(0.25)); } // expect-output: 2024-03-01T00:00:00Z 3 trade(s) // expect-output: 2024-03-01T00:15:00Z 1 trade(s) // expect-output: 2024-03-01T00:30:00Z 2 trade(s) // expect-output: 2024-03-01T00:45:00Z 1 trade(s) // expect-output: first to last: 2580 s // expect-output: as unix ms: 1709251200000 // expect-output: 250ms