Skip to content

Commit 456ae14

Browse files
committed
Turn all temporal types into flat objects
Previously some temporal types were modeled as a combination of other temporal types. For example `Time` contained an instance of `LocalTime` and an offset in seconds. This made constructors more complicated and getters needed to access nested properties. For example `Time.hour` would need to be accessed as `Time.localTime.hour`. This commit makes `Time`, `LocalDateTime`, `DateTimeWithZoneOffset` and `DateTimeWithZoneId` contain primitive properties and not use other temporal types as building blocks. Also made all properties of spatial and temporal types readonly in TypeScript declarations.
1 parent a600c83 commit 456ae14

File tree

8 files changed

+385
-148
lines changed

8 files changed

+385
-148
lines changed

src/v1/internal/packstream-v2.js

+76-16
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,22 @@ import {
2424
DateTimeWithZoneId,
2525
DateTimeWithZoneOffset,
2626
Duration,
27-
Time,
2827
isDate,
2928
isDateTimeWithZoneId,
3029
isDateTimeWithZoneOffset,
3130
isDuration,
3231
isLocalDateTime,
3332
isLocalTime,
34-
isTime
33+
isTime,
34+
Time
3535
} from '../temporal-types';
3636
import {int} from '../integer';
3737
import {
3838
dateToEpochDay,
39-
localDateTimeToEpochSecond,
40-
localTimeToNanoOfDay,
4139
epochDayToDate,
4240
epochSecondAndNanoToLocalDateTime,
41+
localDateTimeToEpochSecond,
42+
localTimeToNanoOfDay,
4343
nanoOfDayToLocalTime
4444
} from '../internal/temporal-util';
4545

@@ -143,6 +143,12 @@ export class Unpacker extends v1.Unpacker {
143143
}
144144
}
145145

146+
/**
147+
* Pack given 2D or 3D point.
148+
* @param {Point} point the point value to pack.
149+
* @param {Packer} packer the packer to use.
150+
* @param {function} onError the error callback.
151+
*/
146152
function packPoint(point, packer, onError) {
147153
const is2DPoint = point.z === null || point.z === undefined;
148154
if (is2DPoint) {
@@ -152,6 +158,12 @@ function packPoint(point, packer, onError) {
152158
}
153159
}
154160

161+
/**
162+
* Pack given 2D point.
163+
* @param {Point} point the point value to pack.
164+
* @param {Packer} packer the packer to use.
165+
* @param {function} onError the error callback.
166+
*/
155167
function packPoint2D(point, packer, onError) {
156168
const packableStructFields = [
157169
packer.packable(int(point.srid), onError),
@@ -161,6 +173,12 @@ function packPoint2D(point, packer, onError) {
161173
packer.packStruct(POINT_2D, packableStructFields, onError);
162174
}
163175

176+
/**
177+
* Pack given 3D point.
178+
* @param {Point} point the point value to pack.
179+
* @param {Packer} packer the packer to use.
180+
* @param {function} onError the error callback.
181+
*/
164182
function packPoint3D(point, packer, onError) {
165183
const packableStructFields = [
166184
packer.packable(int(point.srid), onError),
@@ -171,6 +189,13 @@ function packPoint3D(point, packer, onError) {
171189
packer.packStruct(POINT_3D, packableStructFields, onError);
172190
}
173191

192+
/**
193+
* Unpack 2D point value using the given unpacker.
194+
* @param {Unpacker} unpacker the unpacker to use.
195+
* @param {number} structSize the retrieved struct size.
196+
* @param {BaseBuffer} buffer the buffer to unpack from.
197+
* @return {Point} the unpacked 2D point value.
198+
*/
174199
function unpackPoint2D(unpacker, structSize, buffer) {
175200
unpacker._verifyStructSize('Point2D', POINT_2D_STRUCT_SIZE, structSize);
176201

@@ -182,6 +207,13 @@ function unpackPoint2D(unpacker, structSize, buffer) {
182207
);
183208
}
184209

210+
/**
211+
* Unpack 3D point value using the given unpacker.
212+
* @param {Unpacker} unpacker the unpacker to use.
213+
* @param {number} structSize the retrieved struct size.
214+
* @param {BaseBuffer} buffer the buffer to unpack from.
215+
* @return {Point} the unpacked 3D point value.
216+
*/
185217
function unpackPoint3D(unpacker, structSize, buffer) {
186218
unpacker._verifyStructSize('Point3D', POINT_3D_STRUCT_SIZE, structSize);
187219

@@ -193,6 +225,12 @@ function unpackPoint3D(unpacker, structSize, buffer) {
193225
);
194226
}
195227

228+
/**
229+
* Pack given duration.
230+
* @param {Duration} value the duration value to pack.
231+
* @param {Packer} packer the packer to use.
232+
* @param {function} onError the error callback.
233+
*/
196234
function packDuration(value, packer, onError) {
197235
const months = int(value.months);
198236
const days = int(value.days);
@@ -208,6 +246,13 @@ function packDuration(value, packer, onError) {
208246
packer.packStruct(DURATION, packableStructFields, onError);
209247
}
210248

249+
/**
250+
* Unpack duration value using the given unpacker.
251+
* @param {Unpacker} unpacker the unpacker to use.
252+
* @param {number} structSize the retrieved struct size.
253+
* @param {BaseBuffer} buffer the buffer to unpack from.
254+
* @return {Duration} the unpacked duration value.
255+
*/
211256
function unpackDuration(unpacker, structSize, buffer) {
212257
unpacker._verifyStructSize('Duration', DURATION_STRUCT_SIZE, structSize);
213258

@@ -219,15 +264,28 @@ function unpackDuration(unpacker, structSize, buffer) {
219264
return new Duration(months, days, seconds, nanoseconds);
220265
}
221266

267+
/**
268+
* Pack given local time.
269+
* @param {LocalTime} value the local time value to pack.
270+
* @param {Packer} packer the packer to use.
271+
* @param {function} onError the error callback.
272+
*/
222273
function packLocalTime(value, packer, onError) {
223-
const nanoOfDay = localTimeToNanoOfDay(value);
274+
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
224275

225276
const packableStructFields = [
226277
packer.packable(nanoOfDay, onError)
227278
];
228279
packer.packStruct(LOCAL_TIME, packableStructFields, onError);
229280
}
230281

282+
/**
283+
* Unpack local time value using the given unpacker.
284+
* @param {Unpacker} unpacker the unpacker to use.
285+
* @param {number} structSize the retrieved struct size.
286+
* @param {BaseBuffer} buffer the buffer to unpack from.
287+
* @return {LocalTime} the unpacked local time value.
288+
*/
231289
function unpackLocalTime(unpacker, structSize, buffer) {
232290
unpacker._verifyStructSize('LocalTime', LOCAL_TIME_STRUCT_SIZE, structSize);
233291

@@ -242,7 +300,7 @@ function unpackLocalTime(unpacker, structSize, buffer) {
242300
* @param {function} onError the error callback.
243301
*/
244302
function packTime(value, packer, onError) {
245-
const nanoOfDay = localTimeToNanoOfDay(value.localTime);
303+
const nanoOfDay = localTimeToNanoOfDay(value.hour, value.minute, value.second, value.nanosecond);
246304
const offsetSeconds = int(value.offsetSeconds);
247305

248306
const packableStructFields = [
@@ -266,7 +324,7 @@ function unpackTime(unpacker, structSize, buffer) {
266324
const offsetSeconds = unpacker.unpack(buffer);
267325

268326
const localTime = nanoOfDayToLocalTime(nanoOfDay);
269-
return new Time(localTime, offsetSeconds);
327+
return new Time(localTime.hour, localTime.minute, localTime.second, localTime.nanosecond, offsetSeconds);
270328
}
271329

272330
/**
@@ -276,7 +334,7 @@ function unpackTime(unpacker, structSize, buffer) {
276334
* @param {function} onError the error callback.
277335
*/
278336
function packDate(value, packer, onError) {
279-
const epochDay = dateToEpochDay(value);
337+
const epochDay = dateToEpochDay(value.year, value.month, value.day);
280338

281339
const packableStructFields = [
282340
packer.packable(epochDay, onError)
@@ -305,8 +363,8 @@ function unpackDate(unpacker, structSize, buffer) {
305363
* @param {function} onError the error callback.
306364
*/
307365
function packLocalDateTime(value, packer, onError) {
308-
const epochSecond = localDateTimeToEpochSecond(value);
309-
const nano = int(value.localTime.nanosecond);
366+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
367+
const nano = int(value.nanosecond);
310368

311369
const packableStructFields = [
312370
packer.packable(epochSecond, onError),
@@ -338,8 +396,8 @@ function unpackLocalDateTime(unpacker, structSize, buffer) {
338396
* @param {function} onError the error callback.
339397
*/
340398
function packDateTimeWithZoneOffset(value, packer, onError) {
341-
const epochSecond = localDateTimeToEpochSecond(value.localDateTime);
342-
const nano = int(value.localDateTime.localTime.nanosecond);
399+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
400+
const nano = int(value.nanosecond);
343401
const offsetSeconds = int(value.offsetSeconds);
344402

345403
const packableStructFields = [
@@ -365,7 +423,8 @@ function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer) {
365423
const offsetSeconds = unpacker.unpack(buffer);
366424

367425
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
368-
return new DateTimeWithZoneOffset(localDateTime, offsetSeconds);
426+
return new DateTimeWithZoneOffset(localDateTime.year, localDateTime.month, localDateTime.day, localDateTime.hour,
427+
localDateTime.minute, localDateTime.second, localDateTime.nanosecond, offsetSeconds);
369428
}
370429

371430
/**
@@ -375,8 +434,8 @@ function unpackDateTimeWithZoneOffset(unpacker, structSize, buffer) {
375434
* @param {function} onError the error callback.
376435
*/
377436
function packDateTimeWithZoneId(value, packer, onError) {
378-
const epochSecond = localDateTimeToEpochSecond(value.localDateTime);
379-
const nano = int(value.localDateTime.localTime.nanosecond);
437+
const epochSecond = localDateTimeToEpochSecond(value.year, value.month, value.day, value.hour, value.minute, value.second, value.nanosecond);
438+
const nano = int(value.nanosecond);
380439
const zoneId = value.zoneId;
381440

382441
const packableStructFields = [
@@ -402,5 +461,6 @@ function unpackDateTimeWithZoneId(unpacker, structSize, buffer) {
402461
const zoneId = unpacker.unpack(buffer);
403462

404463
const localDateTime = epochSecondAndNanoToLocalDateTime(epochSecond, nano);
405-
return new DateTimeWithZoneId(localDateTime, zoneId);
464+
return new DateTimeWithZoneId(localDateTime.year, localDateTime.month, localDateTime.day, localDateTime.hour,
465+
localDateTime.minute, localDateTime.second, localDateTime.nanosecond, zoneId);
406466
}

src/v1/internal/temporal-util.js

+47-38
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ const SECONDS_PER_DAY = 86400;
4343

4444
/**
4545
* Converts given local time into a single integer representing this same time in nanoseconds of the day.
46-
* @param {LocalTime} localTime the time to convert.
46+
* @param {Integer|number|string} hour the hour of the local time to convert.
47+
* @param {Integer|number|string} minute the minute of the local time to convert.
48+
* @param {Integer|number|string} second the second of the local time to convert.
49+
* @param {Integer|number|string} nanosecond the nanosecond of the local time to convert.
4750
* @return {Integer} nanoseconds representing the given local time.
4851
*/
49-
export function localTimeToNanoOfDay(localTime) {
50-
const hour = int(localTime.hour);
51-
const minute = int(localTime.minute);
52-
const second = int(localTime.second);
53-
const nanosecond = int(localTime.nanosecond);
52+
export function localTimeToNanoOfDay(hour, minute, second, nanosecond) {
53+
hour = int(hour);
54+
minute = int(minute);
55+
second = int(second);
56+
nanosecond = int(nanosecond);
5457

5558
let totalNanos = hour.multiply(NANOS_PER_HOUR);
5659
totalNanos = totalNanos.add(minute.multiply(NANOS_PER_MINUTE));
@@ -80,15 +83,18 @@ export function nanoOfDayToLocalTime(nanoOfDay) {
8083

8184
/**
8285
* Converts given local date time into a single integer representing this same time in epoch seconds UTC.
83-
* @param {LocalDateTime} localDateTime the local date time value to convert.
86+
* @param {Integer|number|string} year the year of the local date-time to convert.
87+
* @param {Integer|number|string} month the month of the local date-time to convert.
88+
* @param {Integer|number|string} day the day of the local date-time to convert.
89+
* @param {Integer|number|string} hour the hour of the local date-time to convert.
90+
* @param {Integer|number|string} minute the minute of the local date-time to convert.
91+
* @param {Integer|number|string} second the second of the local date-time to convert.
92+
* @param {Integer|number|string} nanosecond the nanosecond of the local date-time to convert.
8493
* @return {Integer} epoch second in UTC representing the given local date time.
8594
*/
86-
export function localDateTimeToEpochSecond(localDateTime) {
87-
const localDate = localDateTime.localDate;
88-
const localTime = localDateTime.localTime;
89-
90-
const epochDay = dateToEpochDay(localDate);
91-
const localTimeSeconds = localTimeToSecondOfDay(localTime);
95+
export function localDateTimeToEpochSecond(year, month, day, hour, minute, second, nanosecond) {
96+
const epochDay = dateToEpochDay(year, month, day);
97+
const localTimeSeconds = localTimeToSecondOfDay(hour, minute, second);
9298
return epochDay.multiply(SECONDS_PER_DAY).add(localTimeSeconds);
9399
}
94100

@@ -105,18 +111,20 @@ export function epochSecondAndNanoToLocalDateTime(epochSecond, nano) {
105111

106112
const localDate = epochDayToDate(epochDay);
107113
const localTime = nanoOfDayToLocalTime(nanoOfDay);
108-
return new LocalDateTime(localDate, localTime);
114+
return new LocalDateTime(localDate.year, localDate.month, localDate.day, localTime.hour, localTime.minute, localTime.second, localTime.nanosecond);
109115
}
110116

111117
/**
112118
* Converts given local date into a single integer representing it's epoch day.
113-
* @param {Date} date the date to convert.
119+
* @param {Integer|number|string} year the year of the local date to convert.
120+
* @param {Integer|number|string} month the month of the local date to convert.
121+
* @param {Integer|number|string} day the day of the local date to convert.
114122
* @return {Integer} epoch day representing the given date.
115123
*/
116-
export function dateToEpochDay(date) {
117-
const year = int(date.year);
118-
const month = int(date.month);
119-
const day = int(date.day);
124+
export function dateToEpochDay(year, month, day) {
125+
year = int(year);
126+
month = int(month);
127+
day = int(day);
120128

121129
let epochDay = year.multiply(365);
122130

@@ -171,10 +179,10 @@ export function epochDayToDate(epochDay) {
171179

172180
/**
173181
* Format given duration to an ISO 8601 string.
174-
* @param {Integer|number} months the number of months.
175-
* @param {Integer|number} days the number of days.
176-
* @param {Integer|number} seconds the number of seconds.
177-
* @param {Integer|number} nanoseconds the number of nanoseconds.
182+
* @param {Integer|number|string} months the number of months.
183+
* @param {Integer|number|string} days the number of days.
184+
* @param {Integer|number|string} seconds the number of seconds.
185+
* @param {Integer|number|string} nanoseconds the number of nanoseconds.
178186
* @return {string} ISO string that represents given duration.
179187
*/
180188
export function durationToIsoString(months, days, seconds, nanoseconds) {
@@ -187,10 +195,10 @@ export function durationToIsoString(months, days, seconds, nanoseconds) {
187195

188196
/**
189197
* Formats given time to an ISO 8601 string.
190-
* @param {Integer|number} hour the hour value.
191-
* @param {Integer|number} minute the minute value.
192-
* @param {Integer|number} second the second value.
193-
* @param {Integer|number} nanosecond the nanosecond value.
198+
* @param {Integer|number|string} hour the hour value.
199+
* @param {Integer|number|string} minute the minute value.
200+
* @param {Integer|number|string} second the second value.
201+
* @param {Integer|number|string} nanosecond the nanosecond value.
194202
* @return {string} ISO string that represents given time.
195203
*/
196204
export function timeToIsoString(hour, minute, second, nanosecond) {
@@ -203,7 +211,7 @@ export function timeToIsoString(hour, minute, second, nanosecond) {
203211

204212
/**
205213
* Formats given time zone offset in seconds to string representation like '±HH:MM', '±HH:MM:SS' or 'Z' for UTC.
206-
* @param {Integer|number} offsetSeconds the offset in seconds.
214+
* @param {Integer|number|string} offsetSeconds the offset in seconds.
207215
* @return {string} ISO string that represents given offset.
208216
*/
209217
export function timeZoneOffsetToIsoString(offsetSeconds) {
@@ -228,9 +236,9 @@ export function timeZoneOffsetToIsoString(offsetSeconds) {
228236

229237
/**
230238
* Formats given date to an ISO 8601 string.
231-
* @param {Integer|number} year the date year.
232-
* @param {Integer|number} month the date month.
233-
* @param {Integer|number} day the date day.
239+
* @param {Integer|number|string} year the date year.
240+
* @param {Integer|number|string} month the date month.
241+
* @param {Integer|number|string} day the date day.
234242
* @return {string} ISO string that represents given date.
235243
*/
236244
export function dateToIsoString(year, month, day) {
@@ -251,20 +259,21 @@ export function dateToIsoString(year, month, day) {
251259

252260
/**
253261
* Converts given local time into a single integer representing this same time in seconds of the day. Nanoseconds are skipped.
254-
* @param {LocalTime} localTime the time to convert.
262+
* @param {Integer|number|string} hour the hour of the local time.
263+
* @param {Integer|number|string} minute the minute of the local time.
264+
* @param {Integer|number|string} second the second of the local time.
255265
* @return {Integer} seconds representing the given local time.
256266
*/
257-
function localTimeToSecondOfDay(localTime) {
258-
const hour = int(localTime.hour);
259-
const minute = int(localTime.minute);
260-
const second = int(localTime.second);
267+
function localTimeToSecondOfDay(hour, minute, second) {
268+
hour = int(hour);
269+
minute = int(minute);
270+
second = int(second);
261271

262272
let totalSeconds = hour.multiply(SECONDS_PER_HOUR);
263273
totalSeconds = totalSeconds.add(minute.multiply(SECONDS_PER_MINUTE));
264274
return totalSeconds.add(second);
265275
}
266276

267-
268277
/**
269278
* Check if given year is a leap year. Uses algorithm described here {@link https://en.wikipedia.org/wiki/Leap_year#Algorithm}.
270279
* @param {Integer|number|string} year the year to check. Will be converted to {@link Integer} for all calculations.
@@ -313,7 +322,7 @@ function floorMod(x, y) {
313322
}
314323

315324
/**
316-
* @param {Integer|number} num the number to format.
325+
* @param {Integer|number|string} num the number to format.
317326
* @param {number} [stringLength=undefined] the string length to left-pad to.
318327
* @return {string} formatted and possibly left-padded number as string.
319328
*/

0 commit comments

Comments
 (0)