Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(components): Replacing package luxon by day.js on roDateTime and roTimespan #28 #29

Merged
merged 2 commits into from
Nov 21, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 11 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
],
"dependencies": {
"commander": "^2.12.2",
"dayjs": "^1.11.10",
"decompress": "^4.2.1",
"fast-glob": "^3.0.1",
"long": "^3.2.0",
"luxon": "^1.8.3",
"memory-fs": "^0.4.1",
"nanomatch": "^1.2.13",
"p-settle": "^2.1.0",
Expand All @@ -45,7 +45,6 @@
"@types/istanbul-lib-coverage": "^2.0.3",
"@types/lolex": "^2.1.3",
"@types/long": "^3.0.32",
"@types/luxon": "^1.4.1",
"@types/memory-fs": "^0.3.2",
"@types/node": "^8.5.2",
"@types/p-settle": "^2.0.1",
Expand Down
17 changes: 10 additions & 7 deletions src/brsTypes/components/RoDateTime.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { BrsValue, ValueKind, BrsString, BrsInvalid, BrsBoolean } from "../BrsType";
import { BrsComponent } from "./BrsComponent";
import { BrsType } from "..";
import { BrsType, ValidDateFormats } from "..";
import { Callable, StdlibArgument } from "../Callable";
import { Interpreter } from "../../interpreter";
import { Int32 } from "../Int32";
import * as luxon from "luxon";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import customParseFormat from "dayjs/plugin/customParseFormat";

export class RoDateTime extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
private markTime = Date.now();

constructor() {
super("roDateTime");
dayjs.extend(utc);
dayjs.extend(customParseFormat);
this.registerMethods({
ifDateTime: [
this.mark,
Expand Down Expand Up @@ -208,12 +212,11 @@ export class RoDateTime extends BrsComponent implements BrsValue {
returns: ValueKind.Void,
},
impl: (_: Interpreter, dateString: BrsString) => {
let dateToParse = luxon.DateTime.fromISO(dateString.value, { zone: "utc" });
if (dateToParse.isValid) {
this.markTime = Date.parse(dateToParse.toISO());
} else {
this.markTime = 0;
let dateParsed = dayjs(dateString.value, ValidDateFormats, true).utc(true);
if (!dateParsed.isValid()) {
dateParsed = dayjs(0);
}
this.markTime = dateParsed.toDate().valueOf();
return BrsInvalid.Instance;
},
});
Expand Down
22 changes: 11 additions & 11 deletions src/brsTypes/components/Timespan.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { BrsValue, ValueKind, BrsString, BrsInvalid, BrsBoolean } from "../BrsType";
import { BrsComponent } from "./BrsComponent";
import { BrsType } from "..";
import { BrsType, ValidDateFormats } from "..";
import { Callable, StdlibArgument } from "../Callable";
import { Interpreter } from "../../interpreter";
import { Int32 } from "../Int32";
import * as luxon from "luxon";
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import customParseFormat from "dayjs/plugin/customParseFormat";

export class Timespan extends BrsComponent implements BrsValue {
readonly kind = ValueKind.Object;
private markTime = Date.now();

constructor() {
super("roTimespan");
dayjs.extend(utc);
dayjs.extend(customParseFormat);
this.registerMethods({
ifTimespan: [
this.mark,
Expand Down Expand Up @@ -79,16 +83,12 @@ export class Timespan extends BrsComponent implements BrsValue {
returns: ValueKind.Int32,
},
impl: (_: Interpreter, date: BrsString) => {
let dateAsSeconds;
let now = Date.now();
let dateToParse = luxon.DateTime.fromISO(date.value, { zone: "utc" });

if (dateToParse.isValid) {
dateAsSeconds = Math.round((Date.parse(dateToParse.toISO()) - now) / 1000);
} else {
dateAsSeconds = 2077252342;
let dateAsSeconds = 2077252342;
TwitchBronBron marked this conversation as resolved.
Show resolved Hide resolved
const now = Date.now();
const dateParsed = dayjs(date.value, ValidDateFormats, true).utc(true);
if (dateParsed.isValid()) {
dateAsSeconds = Math.round((dateParsed.toDate().valueOf() - now) / 1000);
}

return new Int32(dateAsSeconds);
},
});
Expand Down
19 changes: 19 additions & 0 deletions src/brsTypes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,22 @@ export type AllComponents = { kind: ValueKind.Object } & BrsComponent & BrsValue

/** The set of all supported types in BrightScript. */
export type BrsType = BrsPrimitive | Iterable | Callable | AllComponents | Uninitialized;

/** The valid ISO Date formats for roDateTime and roTimeSpan parsing */
export const ValidDateFormats = [
"YYYY-MM-DDTHH:mm:ss.SSS[Z]",
"YYYY-MM-DDTHH:mm:ss.SSS",
"YYYY-MM-DDTHH:mm:ss[Z]",
"YYYY-MM-DDTHH:mm:ss",
"YYYY-MM-DDTHH:mm[Z]",
"YYYY-MM-DDTHH:mm",
"YYYY-MM-DDTHH[Z]",
"YYYY-MM-DDTHH",
"YYYY-MM-DDT",
"YYYY-MM-DD[Z]",
"YYYY-MM-DD",
"YYYY-MM[Z]",
"YYYY-MM",
"YYYY[Z]",
"YYYY",
];
1 change: 0 additions & 1 deletion src/parser/Statement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as Expr from "./Expression";
import { Token, Identifier, Location, Lexeme } from "../lexer";
import { BrsType, BrsInvalid } from "../brsTypes";
import { InvalidZone } from "luxon";
import { AstNode } from "./AstNode";

/** A set of reasons why a `Block` stopped executing. */
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/BrsComponents.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ describe("end to end brightscript functions", () => {
expect(allArgs(outputStreams.stdout.write).filter((arg) => arg !== "\n")).toEqual([
"can return seconds from date until now: ",
"373447701",
"can return seconds from date until now: ",
"373447701",
"can return seconds from date until now: ",
"373447649",
"can return seconds from date until now: ",
"373444829",
"can return seconds from date until now: ",
"373426829",
"can return seconds from date until now: ",
"373426829",
"can return seconds from date until now: ",
"372649229",
"can return seconds from date until now: ",
"346383629",
"can return 2077252342 for date that can't be parsed: ",
"2077252342",
]);
Expand Down
7 changes: 7 additions & 0 deletions test/e2e/resources/components/roTimespan.brs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ sub main()
ts = createObject("roTimespan")

print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10T05:47:52Z")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10T05:47:52")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10T05:47")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10T05")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10T")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11-10")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030-11")
print "can return seconds from date until now: " ts.getsecondstoiso8601date("2030")
print "can return 2077252342 for date that can't be parsed: " ts.getsecondstoiso8601date("14 Jun 2017 00:00:00 PDT")
end sub