Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

adds missing clock properties #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion build/strings/en/documentation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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."
}
}
14 changes: 12 additions & 2 deletions src/compiler/runtime/libraries-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
74 changes: 73 additions & 1 deletion src/compiler/runtime/libraries/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {};
Expand Down
10 changes: 10 additions & 0 deletions src/strings/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
144 changes: 143 additions & 1 deletion tests/compiler/runtime/libraries/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Copy link
Member

@OmarTawfik OmarTawfik Oct 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that adding the AM/PM made the tests fail. Why did we do that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange. On the contrary, tests were failing on my system without it. Maybe it depends on the locale? In that case we should manually construct the string?

Copy link
Collaborator Author

@anuragpeshne anuragpeshne Nov 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can easily reproduce this, just try:
LANG=en_US node -e 'console.log((new Date()).toLocaleString())'


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();
Expand Down