Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/datatypes/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export class DateTime extends AbstractDate {
DateTime.Unit.MILLISECOND
];

static parse(string: any) {
static parse(string: any, defaultTimezoneOffset?: Decimal | number | null) {
if (string === null) {
return null;
}
Expand Down Expand Up @@ -330,6 +330,8 @@ export class DateTime extends AbstractDate {
args.push(matches[17] === '+' ? num : num * -1);
} else if (matches[15] === 'Z') {
args.push(0);
} else if (defaultTimezoneOffset != null) {
args.push(Decimal.from(defaultTimezoneOffset).toNumber());
}
// @ts-ignore
return new DateTime(...args);
Expand Down
2 changes: 1 addition & 1 deletion src/elm/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class ToDateTime extends Expression {
const timezoneOffset = ctx.getExecutionDateTime().timezoneOffset;
return arg.getDateTime(timezoneOffset);
} else {
return DateTime.parse(arg.toString());
return DateTime.parse(arg.toString(), ctx.getExecutionDateTime().timezoneOffset);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/datatypes/datetime-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,20 @@ describe('DateTime', () => {
d.should.eql(new DateTime(2012, 10, 25, 12, 55, 14, 953, 0));
});

it('should use the default timezone offset when the string has none', () => {
// use a quarter-hour offset that no real-world timezone has, so it can
// never coincide with the host machine's offset
const d = DateTime.parse('2012-10-25T12:55', -3.25);
d.timezoneOffset.should.equalDecimal(-3.25);
});

it('should ignore the default timezone offset when the string specifies one', () => {
let d = DateTime.parse('2012-10-25T12:55+05:30', -3.25);
d.timezoneOffset.should.equalDecimal(5.5);
d = DateTime.parse('2012-10-25T12:55Z', -3.25);
d.timezoneOffset.should.equalDecimal(0);
});

it('should toString yyyy', () => {
const d = new DateTime(2012);
d.toString().should.eql('2012');
Expand Down
16 changes: 16 additions & 0 deletions test/elm/convert/convert-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DateTime } from '../../../src/datatypes/datetime';
import { Quantity } from '../../../src/datatypes/quantity';
import { Uncertainty } from '../../../src/datatypes/uncertainty';
import { Decimal } from '../../../src/datatypes/decimal';
import { PatientContext } from '../../../src/runtime/context';

describe('FromString', () => {
beforeEach(function () {
Expand Down Expand Up @@ -92,6 +93,21 @@ describe('FromString', () => {
date.isDateTime.should.equal(true);
});

it("should convert '2015-01-02' to DateTime using the execution timezone offset", async function () {
this.ctx = new PatientContext(
this.ctx.library,
this.ctx.patient,
this.ctx.codeService,
this.ctx.parameters,
// use a quarter-hour offset that no real-world timezone has, so it can
// never coincide with the host machine's offset
DateTime.fromJSDate(new Date(), -3.25)
);
const date = await this.dateTimeStr.exec(this.ctx);
date.timezoneOffset.should.equalDecimal(-3.25);
date.isDateTime.should.equal(true);
});

it("should convert '2015-01-02' to Date", async function () {
const date = await this.dateStr.exec(this.ctx);
date.year.should.equal(2015);
Expand Down
16 changes: 4 additions & 12 deletions test/spec-tests/cql/CqlTypeOperatorsTest.cql
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ define "Convert": Tuple{
output: null
},
"StringToDateTime": Tuple{
skipped: 'Wrong answer (different offsets)'
/*
expression: convert '2014-01-01' to DateTime,
output: @2014-01-01T
*/ },
},
"StringToTime": Tuple{
expression: convert 'T14:30:00.0' to Time,
output: @T14:30:00.000
Expand Down Expand Up @@ -79,23 +77,17 @@ define "ToConcept": Tuple{

define "ToDateTime": Tuple{
"ToDateTime1": Tuple{
skipped: 'Wrong answer (different offsets)'
/*
expression: ToDateTime('2014-01-01'),
output: @2014-01-01T
*/ },
},
"ToDateTime2": Tuple{
skipped: 'Wrong answer (different offsets)'
/*
expression: ToDateTime('2014-01-01T12:05'),
output: @2014-01-01T12:05
*/ },
},
"ToDateTime3": Tuple{
skipped: 'Wrong answer (different offsets)'
/*
expression: ToDateTime('2014-01-01T12:05:05.955'),
output: @2014-01-01T12:05:05.955
*/ },
},
"ToDateTime4": Tuple{
expression: ToDateTime('2014-01-01T12:05:05.955+01:30'),
output: @2014-01-01T12:05:05.955+01:30
Expand Down
Loading
Loading