Skip to content

Commit 9009929

Browse files
committed
Getters for nested fields in temporal types
So that it's, for example, possible to obtain year from `LocalDateTime` which is modeled as a combination of `Date` and `LocalTime`.
1 parent e577893 commit 9009929

File tree

4 files changed

+209
-0
lines changed

4 files changed

+209
-0
lines changed

src/v1/temporal-types.js

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,22 @@ export class Time {
124124
Object.freeze(this);
125125
}
126126

127+
get hour() {
128+
return this.localTime.hour;
129+
}
130+
131+
get minute() {
132+
return this.localTime.minute;
133+
}
134+
135+
get second() {
136+
return this.localTime.second;
137+
}
138+
139+
get nanosecond() {
140+
return this.localTime.nanosecond;
141+
}
142+
127143
toString() {
128144
return this.localTime.toString() + timeZoneOffsetToIsoString(this.offsetSeconds);
129145
}
@@ -192,6 +208,34 @@ export class LocalDateTime {
192208
Object.freeze(this);
193209
}
194210

211+
get year() {
212+
return this.localDate.year;
213+
}
214+
215+
get month() {
216+
return this.localDate.month;
217+
}
218+
219+
get day() {
220+
return this.localDate.day;
221+
}
222+
223+
get hour() {
224+
return this.localTime.hour;
225+
}
226+
227+
get minute() {
228+
return this.localTime.minute;
229+
}
230+
231+
get second() {
232+
return this.localTime.second;
233+
}
234+
235+
get nanosecond() {
236+
return this.localTime.nanosecond;
237+
}
238+
195239
toString() {
196240
return `${this.localDate.toString()}T${this.localTime.toString()}`;
197241
}
@@ -225,6 +269,34 @@ export class DateTimeWithZoneOffset {
225269
Object.freeze(this);
226270
}
227271

272+
get year() {
273+
return this.localDateTime.year;
274+
}
275+
276+
get month() {
277+
return this.localDateTime.month;
278+
}
279+
280+
get day() {
281+
return this.localDateTime.day;
282+
}
283+
284+
get hour() {
285+
return this.localDateTime.hour;
286+
}
287+
288+
get minute() {
289+
return this.localDateTime.minute;
290+
}
291+
292+
get second() {
293+
return this.localDateTime.second;
294+
}
295+
296+
get nanosecond() {
297+
return this.localDateTime.nanosecond;
298+
}
299+
228300
toString() {
229301
return this.localDateTime.toString() + timeZoneOffsetToIsoString(this.offsetSeconds);
230302
}
@@ -258,6 +330,34 @@ export class DateTimeWithZoneId {
258330
Object.freeze(this);
259331
}
260332

333+
get year() {
334+
return this.localDateTime.year;
335+
}
336+
337+
get month() {
338+
return this.localDateTime.month;
339+
}
340+
341+
get day() {
342+
return this.localDateTime.day;
343+
}
344+
345+
get hour() {
346+
return this.localDateTime.hour;
347+
}
348+
349+
get minute() {
350+
return this.localDateTime.minute;
351+
}
352+
353+
get second() {
354+
return this.localDateTime.second;
355+
}
356+
357+
get nanosecond() {
358+
return this.localDateTime.nanosecond;
359+
}
360+
261361
toString() {
262362
return `${this.localDateTime.toString()}[${this.zoneId}]`;
263363
}

test/types/v1/temporal-types.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,18 @@ const localTime2Nanosecond1: number = localTime2.nanosecond;
6161
const time1: Time = new Time(localTime1, int(1));
6262
const localTime3: LocalTime = time1.localTime;
6363
const offset1: Integer = time1.offsetSeconds;
64+
const hour1: Integer = time1.hour;
65+
const minute1: Integer = time1.minute;
66+
const second1: Integer = time1.second;
67+
const nanosecond1: Integer = time1.nanosecond;
6468

6569
const time2: Time<number> = new Time(localTime2, 1);
6670
const localTime4: LocalTime<number> = time2.localTime;
6771
const offset2: number = time2.offsetSeconds;
72+
const hour2: number = time2.hour;
73+
const minute2: number = time2.minute;
74+
const second2: number = time2.second;
75+
const nanosecond2: number = time2.nanosecond;
6876

6977
const date1: Date = new Date(int(1), int(1), int(1));
7078
const date1Year1: Integer = date1.year;
@@ -79,18 +87,46 @@ const date2Day1: number = date2.day;
7987
const localDateTime1: LocalDateTime = new LocalDateTime(date1, localTime1);
8088
const date3: Date = localDateTime1.localDate;
8189
const localTime5: LocalTime = localDateTime1.localTime;
90+
const year1: Integer = localDateTime1.year;
91+
const month1: Integer = localDateTime1.month;
92+
const day1: Integer = localDateTime1.day;
93+
const hour3: Integer = localDateTime1.hour;
94+
const minute3: Integer = localDateTime1.minute;
95+
const second3: Integer = localDateTime1.second;
96+
const nanosecond3: Integer = localDateTime1.nanosecond;
8297

8398
const localDateTime2: LocalDateTime<number> = new LocalDateTime(date2, localTime2);
8499
const date4: Date<number> = localDateTime2.localDate;
85100
const localTime6: LocalTime<number> = localDateTime2.localTime;
101+
const year2: number = localDateTime2.year;
102+
const month2: number = localDateTime2.month;
103+
const day2: number = localDateTime2.day;
104+
const hour4: number = localDateTime2.hour;
105+
const minute4: number = localDateTime2.minute;
106+
const second4: number = localDateTime2.second;
107+
const nanosecond4: number = localDateTime2.nanosecond;
86108

87109
const dateTime1: DateTimeWithZoneOffset = new DateTimeWithZoneOffset(localDateTime1, int(1));
88110
const localDateTime3: LocalDateTime = dateTime1.localDateTime;
89111
const offset3: Integer = dateTime1.offsetSeconds;
112+
const year3: Integer = dateTime1.year;
113+
const month3: Integer = dateTime1.month;
114+
const day3: Integer = dateTime1.day;
115+
const hour5: Integer = dateTime1.hour;
116+
const minute5: Integer = dateTime1.minute;
117+
const second5: Integer = dateTime1.second;
118+
const nanosecond5: Integer = dateTime1.nanosecond;
90119

91120
const dateTime2: DateTimeWithZoneOffset<number> = new DateTimeWithZoneOffset(localDateTime2, 1);
92121
const localDateTime4: LocalDateTime<number> = dateTime2.localDateTime;
93122
const offset4: number = dateTime2.offsetSeconds;
123+
const year4: number = dateTime2.year;
124+
const month4: number = dateTime2.month;
125+
const day4: number = dateTime2.day;
126+
const hour6: number = dateTime2.hour;
127+
const minute6: number = dateTime2.minute;
128+
const second6: number = dateTime2.second;
129+
const nanosecond6: number = dateTime2.nanosecond;
94130

95131
const isDurationValue: boolean = isDuration(duration1);
96132
const isLocalTimeValue: boolean = isLocalTime(localTime1);

test/v1/temporal-types.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,54 @@ describe('temporal-types', () => {
376376
expect(dateTimeWithZoneId(248, 12, 30, 23, 59, 59, 3, 'CET').toString()).toEqual('0248-12-30T23:59:59.000000003[CET]');
377377
});
378378

379+
it('should expose local time components in time', () => {
380+
const offsetTime = time(22, 12, 58, 999111222, 42);
381+
382+
expect(offsetTime.hour).toEqual(neo4j.int(22));
383+
expect(offsetTime.minute).toEqual(neo4j.int(12));
384+
expect(offsetTime.second).toEqual(neo4j.int(58));
385+
expect(offsetTime.nanosecond).toEqual(neo4j.int(999111222));
386+
});
387+
388+
it('should expose local date and time components in local date-time', () => {
389+
const dateTime = localDateTime(2025, 9, 18, 23, 22, 21, 2020);
390+
391+
expect(dateTime.year).toEqual(neo4j.int(2025));
392+
expect(dateTime.month).toEqual(neo4j.int(9));
393+
expect(dateTime.day).toEqual(neo4j.int(18));
394+
395+
expect(dateTime.hour).toEqual(neo4j.int(23));
396+
expect(dateTime.minute).toEqual(neo4j.int(22));
397+
expect(dateTime.second).toEqual(neo4j.int(21));
398+
expect(dateTime.nanosecond).toEqual(neo4j.int(2020));
399+
});
400+
401+
it('should expose local date-time components in date-time with zone offset', () => {
402+
const zonedDateTime = dateTimeWithZoneOffset(1799, 5, 19, 18, 37, 59, 875387, 3600);
403+
404+
expect(zonedDateTime.year).toEqual(neo4j.int(1799));
405+
expect(zonedDateTime.month).toEqual(neo4j.int(5));
406+
expect(zonedDateTime.day).toEqual(neo4j.int(19));
407+
408+
expect(zonedDateTime.hour).toEqual(neo4j.int(18));
409+
expect(zonedDateTime.minute).toEqual(neo4j.int(37));
410+
expect(zonedDateTime.second).toEqual(neo4j.int(59));
411+
expect(zonedDateTime.nanosecond).toEqual(neo4j.int(875387));
412+
});
413+
414+
it('should expose local date-time components in date-time with zone ID', () => {
415+
const zonedDateTime = dateTimeWithZoneId(2356, 7, 29, 23, 32, 11, 9346458, 3600, randomZoneId());
416+
417+
expect(zonedDateTime.year).toEqual(neo4j.int(2356));
418+
expect(zonedDateTime.month).toEqual(neo4j.int(7));
419+
expect(zonedDateTime.day).toEqual(neo4j.int(29));
420+
421+
expect(zonedDateTime.hour).toEqual(neo4j.int(23));
422+
expect(zonedDateTime.minute).toEqual(neo4j.int(32));
423+
expect(zonedDateTime.second).toEqual(neo4j.int(11));
424+
expect(zonedDateTime.nanosecond).toEqual(neo4j.int(9346458));
425+
});
426+
379427
function testSendAndReceiveRandomTemporalValues(valueGenerator, done) {
380428
const asyncFunction = (index, callback) => {
381429
const next = () => callback();

types/v1/temporal-types.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ declare class Time<T extends NumberOrInteger = Integer> {
4242

4343
localTime: LocalTime<T>;
4444
offsetSeconds: T;
45+
hour: T;
46+
minute: T;
47+
second: T;
48+
nanosecond: T;
4549

4650
constructor(localTime: LocalTime<T>, offsetSeconds: T);
4751
}
@@ -59,6 +63,13 @@ declare class LocalDateTime<T extends NumberOrInteger = Integer> {
5963

6064
localDate: Date<T>;
6165
localTime: LocalTime<T>;
66+
year: T;
67+
month: T;
68+
day: T;
69+
hour: T;
70+
minute: T;
71+
second: T;
72+
nanosecond: T;
6273

6374
constructor(localDate: Date<T>, localTime: LocalTime<T>);
6475
}
@@ -67,6 +78,13 @@ declare class DateTimeWithZoneOffset<T extends NumberOrInteger = Integer> {
6778

6879
localDateTime: LocalDateTime<T>;
6980
offsetSeconds: T;
81+
year: T;
82+
month: T;
83+
day: T;
84+
hour: T;
85+
minute: T;
86+
second: T;
87+
nanosecond: T;
7088

7189
constructor(localDateTime: LocalDateTime<T>, offsetSeconds: T);
7290
}
@@ -75,6 +93,13 @@ declare class DateTimeWithZoneId<T extends NumberOrInteger = Integer> {
7593

7694
localDateTime: LocalDateTime<T>;
7795
zoneId: string;
96+
year: T;
97+
month: T;
98+
day: T;
99+
hour: T;
100+
minute: T;
101+
second: T;
102+
nanosecond: T;
78103

79104
constructor(localDateTime: LocalDateTime<T>, zoneId: string);
80105
}

0 commit comments

Comments
 (0)