Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 07b74a7

Browse files
authored
slim date object (#643)
1 parent a0bc949 commit 07b74a7

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed

packages/core/src/date.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/** Enables basic storage and retrieval of dates and times. */
2+
export class Date {
3+
private _year: number
4+
private _monthIndex: number
5+
private _date: number
6+
private _hours: number
7+
private _minutes: number
8+
private _seconds: number
9+
private _ms: number
10+
11+
/**
12+
* Creates a new Date.
13+
* @param year The full year designation is required for cross-century date accuracy. If year is between 0 and 99 is used, then year is assumed to be 1900 + year.
14+
* @param monthIndex The month as a number between 0 and 11 (January to December).
15+
* @param date The date as a number between 1 and 31.
16+
* @param hours Must be supplied if minutes is supplied. A number from 0 to 23 (midnight to 11pm) that specifies the hour.
17+
* @param minutes Must be supplied if seconds is supplied. A number from 0 to 59 that specifies the minutes.
18+
* @param seconds Must be supplied if milliseconds is supplied. A number from 0 to 59 that specifies the seconds.
19+
* @param ms A number from 0 to 999 that specifies the milliseconds.
20+
*/
21+
constructor(
22+
year: number,
23+
monthIndex: number,
24+
date?: number,
25+
hours?: number,
26+
minutes?: number,
27+
seconds?: number,
28+
ms?: number
29+
) {
30+
this._year = year
31+
this._monthIndex = monthIndex
32+
this._date = date || 1
33+
this._hours = hours || 0
34+
this._minutes = minutes || 0
35+
this._seconds = seconds || 0
36+
this._ms = ms || 0
37+
}
38+
39+
/** Gets the year, using local time. */
40+
getFullYear(): number {
41+
return this._year
42+
}
43+
/** Gets the month, using local time. */
44+
getMonth(): number {
45+
return this._monthIndex
46+
}
47+
/** Gets the day-of-the-month, using local time. */
48+
getDate(): number {
49+
return this._date
50+
}
51+
/** Gets the hours in a date, using local time. */
52+
getHours(): number {
53+
return this._hours
54+
}
55+
/** Gets the minutes of a Date object, using local time. */
56+
getMinutes(): number {
57+
return this._minutes
58+
}
59+
/** Gets the seconds of a Date object, using local time. */
60+
getSeconds(): number {
61+
return this._seconds
62+
}
63+
/** Gets the milliseconds of a Date, using local time. */
64+
getMilliseconds(): number {
65+
return this._ms
66+
}
67+
68+
/**
69+
* Sets the milliseconds value in the Date object using local time.
70+
* @param ms A numeric value equal to the millisecond value.
71+
*/
72+
setMilliseconds(ms: number): void {
73+
this._ms = ms
74+
}
75+
/**
76+
* Sets the seconds value in the Date object using local time.
77+
* @param sec A numeric value equal to the seconds value.
78+
* @param ms A numeric value equal to the milliseconds value.
79+
*/
80+
setSeconds(sec: number, ms?: number) {
81+
this._seconds = sec
82+
if (!isNaN(ms)) this._ms = ms
83+
}
84+
/**
85+
* Sets the minutes value in the Date object using local time.
86+
* @param min A numeric value equal to the minutes value.
87+
* @param sec A numeric value equal to the seconds value.
88+
* @param ms A numeric value equal to the milliseconds value.
89+
*/
90+
setMinutes(min: number, sec?: number, ms?: number) {
91+
this._minutes = min
92+
if (!isNaN(sec)) {
93+
this.setSeconds(sec, ms)
94+
}
95+
}
96+
/**
97+
* Sets the hour value in the Date object using local time.
98+
* @param hours A numeric value equal to the hours value.
99+
* @param min A numeric value equal to the minutes value.
100+
* @param sec A numeric value equal to the seconds value.
101+
* @param ms A numeric value equal to the milliseconds value.
102+
*/
103+
setHours(hours: number, min?: number, sec?: number, ms?: number) {
104+
this._hours = hours
105+
if (!isNaN(min)) this.setMinutes(min, sec, ms)
106+
}
107+
/**
108+
* Sets the numeric day-of-the-month value of the Date object using local time.
109+
* @param date A numeric value equal to the day of the month.
110+
*/
111+
setDate(date: number) {
112+
this._date = date
113+
}
114+
/**
115+
* Sets the month value in the Date object using local time.
116+
* @param month A numeric value equal to the month. The value for January is 0, and other month values follow consecutively.
117+
* @param date A numeric value representing the day of the month. If this value is not supplied, the value from a call to the getDate method is used.
118+
*/
119+
setMonth(month: number, date?: number) {
120+
this._monthIndex = month
121+
if (!isNaN(date)) this.setDate(date)
122+
}
123+
/**
124+
* Sets the year of the Date object using local time.
125+
* @param year A numeric value for the year.
126+
* @param month A zero-based numeric value for the month (0 for January, 11 for December). Must be specified if numDate is specified.
127+
* @param date A numeric value equal for the day of the month.
128+
*/
129+
setFullYear(year: number, month?: number, date?: number) {
130+
this._year = year
131+
if (!isNaN(month)) this.setMonth(month, date)
132+
}
133+
}

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "./buffer"
66
import "./timeouts"
77
import "./array"
88
import "./string"
9+
import "./date"
910
import "./events"
1011
import "./jacdac"
1112
import "./lightbulb"

0 commit comments

Comments
 (0)