diff --git a/build/strings/en/documentation.json b/build/strings/en/documentation.json index 323b409..5e5d688 100644 --- a/build/strings/en/documentation.json +++ b/build/strings/en/documentation.json @@ -14,6 +14,16 @@ "Array_IsArray_Value": "The value to check.", "Clock": "This class provides access to the system clock.", "Clock_Time": "Gets the current system time.", + "Clock_Date": "Gets the current system date.", + "Clock_Year": "Gets the current year.", + "Clock_Month": "Gets the current Month.", + "Clock_Day": "Gets the current day of the month.", + "Clock_WeekDay": "Gets the current day of the week.", + "Clock_Hour": "Gets the current Hour.", + "Clock_Minute": "Gets the current Minute.", + "Clock_Second": "Gets the current Second.", + "Clock_Millisecond": "Gets the current Millisecond.", + "Clock_ElapsedMilliseconds": "Gets the number of milliseconds that have elapsed since 1900.", "Controls": "The Controls object allows you to add, move and interact with controls.", "Controls_LastClickedButton": "Gets the last Button that was clicked on the Graphics Window.", "Controls_LastTypedTextBox": "Gets the last TextBox, text was typed into.", @@ -194,4 +204,4 @@ "Turtle_Turn_Angle": "The angle to turn the turtle.", "Turtle_TurnLeft": "Turns the turtle 90 degrees to the left.", "Turtle_TurnRight": "Turns the turtle 90 degrees to the right." -} \ No newline at end of file +} diff --git a/src/compiler/runtime/libraries-metadata.ts b/src/compiler/runtime/libraries-metadata.ts index e2583eb..107bb6e 100644 --- a/src/compiler/runtime/libraries-metadata.ts +++ b/src/compiler/runtime/libraries-metadata.ts @@ -77,13 +77,23 @@ export class LibrariesMetadata { // No Methods }, { - Time: new PropertyMetadata("Clock", "Time", true, false) + Time: new PropertyMetadata("Clock", "Time", true, false), + Date: new PropertyMetadata("Clock", "Date", true, false), + Year: new PropertyMetadata("Clock", "Year", true, false), + Month: new PropertyMetadata("Clock", "Month", true, false), + Day: new PropertyMetadata("Clock", "Day", true, false), + WeekDay: new PropertyMetadata("Clock", "WeekDay", true, false), + Hour: new PropertyMetadata("Clock", "Hour", true, false), + Minute: new PropertyMetadata("Clock", "Minute", true, false), + Second: new PropertyMetadata("Clock", "Second", true, false), + Millisecond: new PropertyMetadata("Clock", "Millisecond", true, false), + ElapsedMilliseconds: new PropertyMetadata("Clock", "ElapsedMilliseconds", true, false) }, { // No Events }); - /* // TODO: + /* // TODO: public readonly Controls: TypeMetadata = new TypeMetadata("Controls", { AddButton: new MethodMetadata("Controls", "AddButton", true, ["Caption", "Left", "Top"]), diff --git a/src/compiler/runtime/libraries/clock.ts b/src/compiler/runtime/libraries/clock.ts index 0c67909..23aa8cd 100644 --- a/src/compiler/runtime/libraries/clock.ts +++ b/src/compiler/runtime/libraries/clock.ts @@ -8,10 +8,82 @@ export class ClockLibrary implements LibraryTypeInstance { return new StringValue(time); } + private getDate(): BaseValue { + const year = this.getYear().toValueString(); + const month = this.getMonth().toValueString(); + const day = this.getDay().toValueString(); + + const date = month + "/" + day + "/" + year; + return new StringValue(date); + } + + private getYear(): BaseValue { + const timestamp = new Date(); + const formattedYear = timestamp.getFullYear().toString(); + return new StringValue(formattedYear); + } + + private getMonth(): BaseValue { + const timestamp = new Date(); + const formattedMonth = ("0" + (timestamp.getMonth() + 1)).slice(-2); + return new StringValue(formattedMonth); + } + + private getDay(): BaseValue { + const timestamp = new Date(); + const formattedDate = ("0" + timestamp.getDate()).slice(-2); + return new StringValue(formattedDate); + } + + private getWeekDay(): BaseValue { + const timestamp = new Date(); + const formattedWeekDay = ("0" + (timestamp.getDay() + 1)).slice(-2); + return new StringValue(formattedWeekDay); + } + + private getHour(): BaseValue { + const timestamp = new Date(); + const formattedHour = ("0" + timestamp.getHours()).slice(-2); + return new StringValue(formattedHour); + } + + private getMinute(): BaseValue { + const timestamp = new Date(); + const formattedMinute = ("0" + timestamp.getMinutes()).slice(-2); + return new StringValue(formattedMinute); + } + + private getSecond(): BaseValue { + const timestamp = new Date(); + const formattedSecond = ("0" + timestamp.getSeconds()).slice(-2); + return new StringValue(formattedSecond); + } + + private getMillisecond(): BaseValue { + const timestamp = new Date(); + const formattedMillisecond = ("0" + timestamp.getMilliseconds()).slice(-3); + return new StringValue(formattedMillisecond); + } + + private getElapsedMilliseconds(): BaseValue { + const unixepoch = (new Date).getTime(); + return new StringValue(unixepoch.toString()); + } + public readonly methods: { readonly [name: string]: LibraryMethodInstance } = {}; public readonly properties: { readonly [name: string]: LibraryPropertyInstance } = { - Time: { getter: this.getTime.bind(this) } + Time : { getter: this.getTime.bind(this) }, + Date : { getter: this.getDate.bind(this) }, + Year : { getter: this.getYear.bind(this) }, + Month : { getter: this.getMonth.bind(this) }, + Day : { getter: this.getDay.bind(this) }, + WeekDay : { getter: this.getWeekDay.bind(this) }, + Hour : { getter: this.getHour.bind(this) }, + Minute : { getter: this.getMinute.bind(this) }, + Second : { getter: this.getSecond.bind(this) }, + Millisecond : { getter: this.getMillisecond.bind(this) }, + ElapsedMilliseconds: { getter: this.getElapsedMilliseconds.bind(this) } }; public readonly events: { readonly [name: string]: LibraryEventInstance } = {}; diff --git a/src/strings/documentation.ts b/src/strings/documentation.ts index dfc5cd5..1e12bcd 100644 --- a/src/strings/documentation.ts +++ b/src/strings/documentation.ts @@ -16,6 +16,16 @@ export module DocumentationResources { export const Array_IsArray_Value = "The value to check."; export const Clock = "This class provides access to the system clock."; export const Clock_Time = "Gets the current system time."; + export const Clock_Date = "Gets the current system date."; + export const Clock_Year = "Gets the current year."; + export const Clock_Month = "Gets the current Month."; + export const Clock_Day = "Gets the current day of the month."; + export const Clock_WeekDay = "Gets the current day of the week."; + export const Clock_Hour = "Gets the current Hour."; + export const Clock_Minute = "Gets the current Minute."; + export const Clock_Second = "Gets the current Second."; + export const Clock_Millisecond = "Gets the current Millisecond."; + export const Clock_ElapsedMilliseconds = "Gets the number of milliseconds that have elapsed since 1900."; export const Controls = "The Controls object allows you to add, move and interact with controls."; export const Controls_LastClickedButton = "Gets the last Button that was clicked on the Graphics Window."; export const Controls_LastTypedTextBox = "Gets the last TextBox, text was typed into."; diff --git a/tests/compiler/runtime/libraries/clock.ts b/tests/compiler/runtime/libraries/clock.ts index ae38e1e..9415d78 100644 --- a/tests/compiler/runtime/libraries/clock.ts +++ b/tests/compiler/runtime/libraries/clock.ts @@ -11,7 +11,149 @@ x = Clock.Time`); engine.execute(ExecutionMode.RunToEnd); const value = engine.memory.values["x"]; - expect(value.toValueString()).toMatch(/[0-9]{2}:[0-9]{2}:[0-9]{2}/); + expect(value.toValueString()).toMatch(/[0-9]{2}:[0-9]{2}:[0-9]{2} A|PM/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Date", () => { + const compilation = new Compilation(` +x = Clock.Date`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[01][0-9]\/[0-3][0-9]\/2[0-9]{3}/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Year", () => { + const compilation = new Compilation(` +x = Clock.Year`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/2[0-9]{3}/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Month", () => { + const compilation = new Compilation(` +x = Clock.Month`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[01][0-9]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Day of the month", () => { + const compilation = new Compilation(` +x = Clock.Day`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-3][0-9]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Day of the Week", () => { + const compilation = new Compilation(` +x = Clock.WeekDay`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-7]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Hour", () => { + const compilation = new Compilation(` +x = Clock.Hour`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-2][0-9]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Minute", () => { + const compilation = new Compilation(` +x = Clock.Minute`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-6][0-9]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Second", () => { + const compilation = new Compilation(` +x = Clock.Second`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-6][0-9]/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive current Millisecond", () => { + const compilation = new Compilation(` +x = Clock.Millisecond`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + expect(value.toValueString()).toMatch(/[0-9]{1,4}/); + + expect(engine.state).toBe(ExecutionState.Terminated); + expect(engine.exception).toBeUndefined(); + }); + + it("can retreive Elapsed Millisecond since 1970", () => { + const compilation = new Compilation(` +x = Clock.ElapsedMilliseconds`); + + const engine = new ExecutionEngine(compilation); + engine.execute(ExecutionMode.RunToEnd); + + const value = engine.memory.values["x"]; + const intval = parseInt(value.toValueString()); + const epochAtTheTimeOfWritingThis = 1540613687913; + expect(intval).toBeGreaterThan(epochAtTheTimeOfWritingThis); expect(engine.state).toBe(ExecutionState.Terminated); expect(engine.exception).toBeUndefined();