Skip to content

Commit 6478dd0

Browse files
committed
parse: improved da support
Support leading zeroes in timeslots, absence of time and/or subseconds, and negative years (Before Christ).
1 parent 33e6962 commit 6478dd0

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/da.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,20 @@ export function year(det: Dat) {
9393
* @return {bigint} x The urbit date as bigint
9494
*/
9595
export function parseDa(x: string): bigint {
96-
const [date, time, ms] = x.split('..');
97-
const [yer, month, day] = date.slice(1).split('.');
96+
let pos = true;
97+
let [date, time, ms] = x.split('..');
98+
time = time || '0.0.0';
99+
ms = ms || '0000';
100+
let [yer, month, day] = date.slice(1).split('.');
101+
if (yer.at(-1) === '-') {
102+
yer = yer.slice(0, -1);
103+
pos = false;
104+
}
98105
const [hour, minute, sec] = time.split('.');
99106
const millis = ms.split('.').map((m) => BigInt('0x'+m));
100107

101108
return year({
102-
pos: true,
109+
pos: pos,
103110
year: BigInt(yer),
104111
month: BigInt(month),
105112
time: {

src/parse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type coin = ({ type: 'dime' } & dime)
4848
//TODO rewrite with eye towards capturing groups?
4949
export const regex: { [key in aura]: RegExp } = {
5050
'c': /^~\-((~[0-9a-fA-F]+\.)|(~[~\.])|[0-9a-z\-\._])*$/,
51-
'da': /^~(0|[1-9][0-9]*)\-?\.([1-9]|1[0-2])\.([1-9]|[1-3][0-9])(\.\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\.(\.[0-9a-f]{4})+)?)?$/,
51+
'da': /^~(0|[1-9][0-9]*)\-?\.([1-9]|1[0-2])\.([1-9]|[1-3][0-9])(\.\.([0-9]+)\.([0-9]+)\.([0-9]+)(\.(\.[0-9a-f]{4})+)?)?$/,
5252
'dr': /^~((d|h|m|s)(0|[1-9][0-9]*))(\.(d|h|m|s)(0|[1-9][0-9]*))?(\.(\.[0-9a-f]{4})+)?$/,
5353
'f': /^\.(y|n)$/,
5454
'n': /^~$/,

test/parse.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,14 @@ const DATE_TESTS: {
307307
{ 'n': 170141184492616163062707000439658774528n,
308308
'da': '~2000.1.1..11.11.11..aabb.0000'
309309
},
310+
{ 'n': 170141184492616163062707000439658774528n,
311+
'da': '~2000.1.1..11.11.11..aabb'
312+
},
310313
{ 'n': 170141184492615487727406687186543706111n,
311314
'da': '~2000.1.1..1.1.1..aabb.ccdd.eeff.ffff'
312315
}
313316
];
314-
testAuras('phonetic', DATE_AURAS, DATE_TESTS);
317+
testAuras('date', DATE_AURAS, DATE_TESTS);
315318

316319
describe('string decoding', () => {
317320
it('decodes', () => {

0 commit comments

Comments
 (0)