Skip to content

Commit c7a03a0

Browse files
committed
lib: add some @dr utilities
1 parent f9c0acf commit c7a03a0

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/da.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,22 @@ export function fromUnix(unix: number): bigint {
110110
return DA_UNIX_EPOCH + timeSinceEpoch;
111111
}
112112

113+
/**
114+
* Given a number of seconds, return a bigint representing its `@dr`
115+
*/
116+
export function fromSeconds(seconds: bigint): bigint {
117+
return yule({ day: 0n, hour: 0n, minute: 0n, second: seconds, ms: [] });
118+
}
119+
120+
/**
121+
* Convert a `@dr` to the amount of seconds it represents, dropping sub-
122+
* second precision
123+
*/
124+
export function toSeconds(dr: bigint): bigint {
125+
const { day, hour, minute, second } = yell(dr);
126+
return (((((day * 24n) + hour) * 60n) + minute) * 60n) + second;
127+
}
128+
113129
//
114130
// internals
115131
//

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ export { render, scot, rend } from './render'; //TODO expose encodeString() ?
66

77
// atom utils
88

9-
import { toUnix, fromUnix } from './da';
9+
import { toUnix, fromUnix, fromSeconds, toSeconds } from './da';
1010
export const da = { toUnix, fromUnix };
11+
export const dr = { toSeconds, fromSeconds };
1112

1213
import type * as pt from './p';
1314
import { cite, sein, clan, kind, rankToSize, sizeToRank } from './p';

0 commit comments

Comments
 (0)