Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

Commit

Permalink
Makes timezone formatting as duration more robust
Browse files Browse the repository at this point in the history
Fixes support of cases like "+00:00"
  • Loading branch information
Tpt authored and rubensworks committed May 24, 2022
1 parent 8713b0f commit e2cc58b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/functions/XPathFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,26 @@ export function formatDayTimeDuration(timezone: string): string | undefined {
if (!timezone) {
return;
}
if (timezone.startsWith('Z')) {
return 'PT0S';
}
// Split string
const [ sign, h1Raw, h2Raw, _, m1Raw, m2Raw ] = timezone;

// Cut of leading zero, set to empty string if 0, and append H;
const h1 = h1Raw !== '0' ? h1Raw : '';
const h2 = h1 || h2Raw !== '0' ? h2Raw : '';
const hours = h1 + h2 ? `${h1 + h2}H` : '';
let sign = '';
let hours = 0;
let minutes = 0;
if (timezone !== 'Z') {
sign = timezone.startsWith('-') ? '-' : '';
hours = Number.parseInt(timezone.slice(1, 3), 10);
minutes = Number.parseInt(timezone.slice(4, 6), 10);
}

// Same as in hours
const m1 = m1Raw !== '0' ? m1Raw : '';
const m2 = m1 || m2Raw !== '0' ? m2Raw : '';
const minutes = m1 + m2 ? `${m1 + m2}M` : '';
if (hours === 0 && minutes === 0) {
return 'PT0S';
}

// Concat sign and time and mandatory separators
const time = `${hours}${minutes}`;
const signNoPlus = sign === '-' ? '-' : '';
return `${signNoPlus}PT${time}`;
let time = `${sign}PT`;
if (hours > 0) {
time += `${hours}H`;
}
if (minutes > 0) {
time += `${minutes}M`;
}
return time;
}
3 changes: 3 additions & 0 deletions test/integration/functions/XPathConstructors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@ describe('evaluation of XPath constructors', () => {
testTable: `
"1999-03-17T06:00:00Z"^^xsd:dateTime = "1999-03-17T06:00:00Z"^^xsd:dateTime
"1999-03-17T06:00:00Z" = "1999-03-17T06:00:00Z"^^xsd:dateTime
"1999-03-17T06:00:00+02:30" = "1999-03-17T06:00:00+02:30"^^xsd:dateTime
"1999-03-17T06:00:00" = "1999-03-17T06:00:00"^^xsd:dateTime
"1999-03-17" = "1999-03-17"^^xsd:dateTime
`,
errorTable: `
"foo" = ''
Expand Down
19 changes: 19 additions & 0 deletions test/integration/functions/op.timezone.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Notation } from '../../util/TestTable';
import { runTestTable } from '../../util/utils';

describe('evaluation of \'timezone\'', () => {
runTestTable({
operation: 'timezone',
arity: 1,
notation: Notation.Function,
testTable: `
"2000-01-01T00:00:00Z"^^xsd:dateTime = "PT0S"^^xsd:dayTimeDuration
"2000-01-01T00:00:00+00:00"^^xsd:dateTime = "PT0S"^^xsd:dayTimeDuration
"2000-01-01T00:00:00-00:00"^^xsd:dateTime = "PT0S"^^xsd:dayTimeDuration
"2000-01-01T00:00:00+00:30"^^xsd:dateTime = "PT30M"^^xsd:dayTimeDuration
"2000-01-01T00:00:00+01:00"^^xsd:dateTime = "PT1H"^^xsd:dayTimeDuration
"2000-01-01T00:00:00+01:30"^^xsd:dateTime = "PT1H30M"^^xsd:dayTimeDuration
"2000-01-01T00:00:00-01:30"^^xsd:dateTime = "-PT1H30M"^^xsd:dayTimeDuration
`,
});
});

0 comments on commit e2cc58b

Please sign in to comment.