Skip to content

Commit

Permalink
Fix missing radix when serializing relative time milliseconds (#1567)
Browse files Browse the repository at this point in the history
* Fix missing radix when serializing relative time milliseconds
  • Loading branch information
cartermak authored Dec 4, 2024
1 parent 452f1f0 commit 19525d0
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 3 deletions.
94 changes: 93 additions & 1 deletion src/utilities/sequence-editor/to-seq-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ C ECHO L01STR
it('local and parameter block', async () => {
const id = 'test.sequence';
const seq = `@ID "test.inline"
@LOCALS_BEGIN
@LOCALS_BEGIN
L00STR
L01 INT
L02ENUM ENUM TEMPERATURE "" "hot, cold"
Expand Down Expand Up @@ -1073,6 +1073,98 @@ G03:00:00 "GroundEpochName" @REQUEST_BEGIN("request2.name")
expect(actual).toEqual(expected);
});

it('should handle all time tag types', async () => {
const seq = `A2029-365T23:20:50 BAKE_BREAD
A2029-365T23:21:51.123 BAKE_BREAD
R00:00:30 BAKE_BREAD
R10 BAKE_BREAD
R00:00:30.500 BAKE_BREAD
E00:06:40.333 BAKE_BREAD
E00:00:10 BAKE_BREAD
E-00:06:40.333 BAKE_BREAD`;
const id = 'test';
const expectedJson = {
id: 'test',
metadata: {},
steps: [
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '2029-365T23:20:50',
type: 'ABSOLUTE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '2029-365T23:21:51.123',
type: 'ABSOLUTE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '00:00:30',
type: 'COMMAND_RELATIVE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '00:00:10',
type: 'COMMAND_RELATIVE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '00:00:30.500',
type: 'COMMAND_RELATIVE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '00:06:40.333',
type: 'EPOCH_RELATIVE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '00:00:10',
type: 'EPOCH_RELATIVE',
},
type: 'command',
},
{
args: [],
stem: 'BAKE_BREAD',
time: {
tag: '-00:06:40.333',
type: 'EPOCH_RELATIVE',
},
type: 'command',
},
],
};
const actual = JSON.parse(await sequenceToSeqJson(SeqLanguage.parser.parse(seq), seq, commandDictionary, id));
expect(actual).toEqual(expectedJson);
});

describe('round trip', () => {
it('should round trip commands', async () => {
const input = `
Expand Down
4 changes: 2 additions & 2 deletions src/utilities/sequence-editor/to-seq-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ function parseTime(commandNode: SyntaxNode, text: string): Time {
const { isNegative, days, hours, minutes, seconds, milliseconds } = getDurationTimeComponents(
parseDurationString(timeTagEpochText, 'seconds'),
);
tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds}`;
tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds ? '.' : ''}${milliseconds}`;
return { tag, type: 'EPOCH_RELATIVE' };
}

Expand All @@ -424,7 +424,7 @@ function parseTime(commandNode: SyntaxNode, text: string): Time {
const { isNegative, days, hours, minutes, seconds, milliseconds } = getDurationTimeComponents(
parseDurationString(timeTagRelativeText, 'seconds'),
);
tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds}`;
tag = `${isNegative}${days}${days ? 'T' : ''}${hours}:${minutes}:${seconds}${milliseconds ? '.' : ''}${milliseconds}`;
return { tag, type: 'COMMAND_RELATIVE' };
}

Expand Down
69 changes: 69 additions & 0 deletions src/utilities/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getDoy,
getDoyTime,
getDoyTimeComponents,
getDurationTimeComponents,
getShortISOForDate,
getTimeAgo,
getUnixEpochTime,
Expand Down Expand Up @@ -468,6 +469,74 @@ test('parseDurationString', () => {
});
});

test('getDurationTimeComponents', () => {
expect(
getDurationTimeComponents({
days: 3,
hours: 10,
isNegative: false,
microseconds: 0,
milliseconds: 0,
minutes: 30,
seconds: 45,
years: 2,
}),
).toEqual({
days: '003',
hours: '10',
isNegative: '',
microseconds: '',
milliseconds: '',
minutes: '30',
seconds: '45',
years: '0002',
});

expect(
getDurationTimeComponents({
days: 300,
hours: 2,
isNegative: true,
microseconds: 1,
milliseconds: 123,
minutes: 1,
seconds: 2,
years: 0,
}),
).toEqual({
days: '300',
hours: '02',
isNegative: '-',
microseconds: '001',
milliseconds: '123',
minutes: '01',
seconds: '02',
years: '0000',
});

expect(
getDurationTimeComponents({
days: 0,
hours: 0,
isNegative: false,
microseconds: 123,
milliseconds: 0,
minutes: 0,
seconds: 2,
years: 0,
}),
).toEqual({
days: '',
hours: '00',
isNegative: '',
microseconds: '123',
milliseconds: '',
minutes: '00',
seconds: '02',
years: '0000',
});
});

test('isTimeBalanced', () => {
expect(isTimeBalanced('2024-001T00:00:00', TimeTypes.ABSOLUTE)).toBe(true);
expect(isTimeBalanced('2024-001T12:90:00', TimeTypes.ABSOLUTE)).toBe(false);
Expand Down

0 comments on commit 19525d0

Please sign in to comment.