assemblyscript-temporal
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -64,10 +64,10 @@ import { durationSign } from "./utils"; | ||
const date = | ||
toString(this.years, "Y") + | ||
toString(this.months, "M") + | ||
toString(this.weeks, "W") + | ||
toString(this.days, "D"); | ||
toString(abs(this.years), "Y") + | ||
toString(abs(this.months), "M") + | ||
toString(abs(this.weeks), "W") + | ||
toString(abs(this.days), "D"); | ||
const time = | ||
toString(this.hours, "H") + | ||
toString(this.minutes, "M") + | ||
toString(abs(this.hours), "H") + | ||
toString(abs(this.minutes), "M") + | ||
toString( | ||
@@ -90,3 +90,9 @@ // sort in ascending order for better sum precision | ||
function toString<T extends number>(value: T, suffix: string): string { | ||
return value ? value.toString() + suffix : ""; | ||
return value | ||
? (isFloat<T>() ? stringify(value) : value.toString()) + suffix | ||
: ""; | ||
} | ||
function stringify(value: f64): string { | ||
return F64.isSafeInteger(value) ? i64(value).toString() : value.toString(); | ||
} |
@@ -29,3 +29,20 @@ import { RegExp } from "../node_modules/assemblyscript-regex/assembly/index"; | ||
@inline | ||
static fromPlainDate(date: PlainDate): PlainDate { | ||
static from<T = DateLike>(date: T): PlainDate { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>date); | ||
} else { | ||
if (isReference<T>()) { | ||
if (date instanceof PlainDate) { | ||
return this.fromPlainDate(date); | ||
} else if (date instanceof DateLike) { | ||
return this.fromDateLike(date); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
@inline | ||
private static fromPlainDate(date: PlainDate): PlainDate { | ||
return new PlainDate(date.year, date.month, date.day); | ||
@@ -35,3 +52,3 @@ } | ||
@inline | ||
static fromDateLike(date: DateLike): PlainDate { | ||
private static fromDateLike(date: DateLike): PlainDate { | ||
if (date.year == -1 || date.month == -1 || date.day == -1) { | ||
@@ -43,3 +60,3 @@ throw new TypeError("missing required property"); | ||
static fromString(date: string): PlainDate { | ||
private static fromString(date: string): PlainDate { | ||
const dateRegex = new RegExp( | ||
@@ -65,19 +82,2 @@ "^((?:[+-]\\d{6}|\\d{4}))(?:-(\\d{2})-(\\d{2})|(\\d{2})(\\d{2}))(?:(?:T|\\s+)(\\d{2})(?::(\\d{2})(?::(\\d{2})(?:[.,](\\d{1,9}))?)?|(\\d{2})(?:(\\d{2})(?:[.,](\\d{1,9}))?)?)?)?(?:([zZ])|(?:([+-])([01][0-9]|2[0-3])(?::?([0-5][0-9])(?::?([0-5][0-9])(?:[.,](\\d{1,9}))?)?)?)?)(?:\\[((?:(?:\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|_[-A-Za-z._]{0,13}|[a-zA-Z](?:[A-Za-z._][-A-Za-z._]{0,12})?|[a-zA-Z]-(?:[-._][-A-Za-z._]{0,11})?|[a-zA-Z]-[a-zA-Z](?:[-._][-A-Za-z._]{0,10})?|[a-zA-Z]-[a-zA-Z][a-zA-Z](?:[A-Za-z._][-A-Za-z._]{0,9})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-(?:[-._][-A-Za-z._]{0,8})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-[a-zA-Z](?:[-._][-A-Za-z._]{0,7})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z](?:[-._][-A-Za-z._]{0,6})?)(?:\\/(?:\\.[-A-Za-z_]|\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|[A-Za-z_][-A-Za-z._]{0,13}))*|Etc\\/GMT[-+]\\d{1,2}|(?:[+\\u2212-][0-2][0-9](?::?[0-5][0-9](?::?[0-5][0-9](?:[.,]\\d{1,9})?)?)?)))\\])?(?:\\[u-ca-((?:[A-Za-z0-9]{3,8}(?:-[A-Za-z0-9]{3,8})*))\\])?$", | ||
@inline | ||
static from<T = DateLike>(date: T): PlainDate { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>date); | ||
} else { | ||
if (isReference<T>()) { | ||
if (date instanceof PlainDate) { | ||
return this.fromPlainDate(date); | ||
} else if (date instanceof DateLike) { | ||
return this.fromDateLike(date); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
constructor(readonly year: i32, readonly month: i32, readonly day: i32) { | ||
@@ -84,0 +84,0 @@ rejectDate(year, month, day); |
@@ -5,2 +5,3 @@ import { RegExp } from "../node_modules/assemblyscript-regex/assembly/index"; | ||
import { Overflow, TimeComponent } from "./enums"; | ||
import { PlainTime } from "./plaintime"; | ||
import { MICROS_PER_SECOND, MILLIS_PER_SECOND, NANOS_PER_SECOND } from "./constants"; | ||
@@ -33,3 +34,20 @@ import { | ||
@inline | ||
static fromPlainDateTime(date: PlainDateTime): PlainDateTime { | ||
static from<T = DateTimeLike>(date: T): PlainDateTime { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>date); | ||
} else { | ||
if (isReference<T>()) { | ||
if (date instanceof PlainDateTime) { | ||
return this.fromPlainDateTime(date); | ||
} else if (date instanceof DateTimeLike) { | ||
return this.fromDateTimeLike(date); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
@inline | ||
private static fromPlainDateTime(date: PlainDateTime): PlainDateTime { | ||
return new PlainDateTime( | ||
@@ -49,3 +67,3 @@ date.year, | ||
@inline | ||
static fromDateTimeLike(date: DateTimeLike): PlainDateTime { | ||
private static fromDateTimeLike(date: DateTimeLike): PlainDateTime { | ||
if (date.year == -1 || date.month == -1 || date.day == -1) { | ||
@@ -67,3 +85,3 @@ throw new TypeError("missing required property"); | ||
static fromString(date: string): PlainDateTime { | ||
private static fromString(date: string): PlainDateTime { | ||
const dateRegex = new RegExp( | ||
@@ -75,5 +93,8 @@ "^((?:[+-]\\d{6}|\\d{4}))(?:-(\\d{2})-(\\d{2})|(\\d{2})(\\d{2}))(?:(?:T|\\s+)(\\d{2})(?::(\\d{2})(?::(\\d{2})(?:[.,](\\d{1,9}))?)?|(\\d{2})(?:(\\d{2})(?:[.,](\\d{1,9}))?)?)?)?(?:([zZ])|(?:([+-])([01][0-9]|2[0-3])(?::?([0-5][0-9])(?::?([0-5][0-9])(?:[.,](\\d{1,9}))?)?)?)?)(?:\\[((?:(?:\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|_[-A-Za-z._]{0,13}|[a-zA-Z](?:[A-Za-z._][-A-Za-z._]{0,12})?|[a-zA-Z]-(?:[-._][-A-Za-z._]{0,11})?|[a-zA-Z]-[a-zA-Z](?:[-._][-A-Za-z._]{0,10})?|[a-zA-Z]-[a-zA-Z][a-zA-Z](?:[A-Za-z._][-A-Za-z._]{0,9})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-(?:[-._][-A-Za-z._]{0,8})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-[a-zA-Z](?:[-._][-A-Za-z._]{0,7})?|[a-zA-Z]-[a-zA-Z][a-zA-Z]-[a-zA-Z][a-zA-Z](?:[-._][-A-Za-z._]{0,6})?)(?:\\/(?:\\.[-A-Za-z_]|\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|[A-Za-z_][-A-Za-z._]{0,13}))*|Etc\\/GMT[-+]\\d{1,2}|(?:[+\\u2212-][0-2][0-9](?::?[0-5][0-9](?::?[0-5][0-9](?:[.,]\\d{1,9})?)?)?)))\\])?(?:\\[u-ca-((?:[A-Za-z0-9]{3,8}(?:-[A-Za-z0-9]{3,8})*))\\])?$", | ||
if (match != null) { | ||
// see https://github.com/ColinEberhardt/assemblyscript-regex/issues/38 | ||
const fraction = ( | ||
match.matches[7] != "" ? match.matches[7] : match.matches[18] | ||
) + "000000000"; | ||
return new PlainDateTime( | ||
I32.parseInt(match.matches[1]), | ||
// see https://github.com/ColinEberhardt/assemblyscript-regex/issues/38 | ||
I32.parseInt( | ||
@@ -86,7 +107,7 @@ match.matches[2] != "" ? match.matches[2] : match.matches[19] | ||
I32.parseInt(match.matches[4]), | ||
I32.parseInt(match.matches[5]), | ||
I32.parseInt(match.matches[6]), | ||
I32.parseInt(match.matches[7].substring(0, 3)), | ||
I32.parseInt(match.matches[7].substring(3, 6)), | ||
I32.parseInt(match.matches[7].substring(6, 9)) | ||
I32.parseInt(match.matches[5] != "" ? match.matches[5]: match.matches[16]), | ||
I32.parseInt(match.matches[6] != "" ? match.matches[6]: match.matches[17]), | ||
I32.parseInt(fraction.substring(0, 3)), | ||
I32.parseInt(fraction.substring(3, 6)), | ||
I32.parseInt(fraction.substring(6, 9)) | ||
); | ||
@@ -97,19 +118,2 @@ } | ||
@inline | ||
static from<T = DateTimeLike>(date: T): PlainDateTime { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>date); | ||
} else { | ||
if (isReference<T>()) { | ||
if (date instanceof PlainDateTime) { | ||
return this.fromPlainDateTime(date); | ||
} else if (date instanceof DateTimeLike) { | ||
return this.fromDateTimeLike(date); | ||
} | ||
} | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
constructor( | ||
@@ -203,2 +207,13 @@ readonly year: i32, | ||
toPlainTime(): PlainTime { | ||
return new PlainTime( | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond | ||
); | ||
} | ||
static compare(a: PlainDateTime, b: PlainDateTime): i32 { | ||
@@ -205,0 +220,0 @@ if (a === b) return 0; |
@@ -1,5 +0,27 @@ | ||
import { sign, ord, checkRange } from "./utils"; | ||
import { | ||
sign, | ||
ord, | ||
checkRange, | ||
balanceDuration, | ||
addTime, | ||
toPaddedString, | ||
coalesce, | ||
differenceTime, | ||
regulateTime, | ||
} from "./utils"; | ||
import { Duration, DurationLike } from "./duration"; | ||
import { Overflow, TimeComponent } from "./enums"; | ||
import { RegExp } from "../node_modules/assemblyscript-regex/assembly/index"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { DateLike } from "./plaindate"; | ||
export class TimeLike { | ||
hour: i32 = -1; | ||
minute: i32 = -1; | ||
second: i32 = -1; | ||
millisecond: i32 = -1; | ||
microsecond: i32 = -1; | ||
nanosecond: i32 = -1; | ||
} | ||
export class PlainTime { | ||
@inline | ||
@@ -17,24 +39,262 @@ static fromPlainTime(time: PlainTime): PlainTime { | ||
@inline | ||
static fromPlainDateTime(date: PlainDateTime): PlainTime { | ||
return new PlainTime( | ||
date.hour, | ||
date.minute, | ||
date.second, | ||
date.millisecond, | ||
date.microsecond, | ||
date.nanosecond | ||
); | ||
} | ||
@inline | ||
static fromTimeLike(time: TimeLike): PlainTime { | ||
return new PlainTime( | ||
coalesce(time.hour, 0), | ||
coalesce(time.minute, 0), | ||
coalesce(time.second, 0), | ||
coalesce(time.millisecond, 0), | ||
coalesce(time.microsecond, 0), | ||
coalesce(time.nanosecond, 0) | ||
); | ||
} | ||
static fromString(time: string): PlainTime { | ||
const timeRegex = new RegExp( | ||
"^(\\d{2})(?::(\\d{2})(?::(\\d{2})(?:[.,](\\d{1,9}))?)?|(\\d{2})(?:(\\d{2})(?:[.,](\\d{1,9}))?)?)?(?:(?:([zZ])|(?:([+\u2212-])([01][0-9]|2[0-3])(?::?([0-5][0-9])(?::?([0-5][0-9])(?:[.,](\\d{1,9}))?)?)?)?)(?:\\[((?:(?:\\.[-A-Za-z_]|\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|[A-Za-z_][-A-Za-z._]{0,13})(?:\\/(?:\\.[-A-Za-z_]|\\.\\.[-A-Za-z._]{1,12}|\\.[-A-Za-z_][-A-Za-z._]{0,12}|[A-Za-z_][-A-Za-z._]{0,13}))*|Etc\\/GMT[-+]\\d{1,2}|(?:[+\\u2212-][0-2][0-9](?::?[0-5][0-9](?::?[0-5][0-9](?:[.,]\\d{1,9})?)?)?)))\\])?)?(?:\\[u-ca=((?:[A-Za-z0-9]{3,8}(?:-[A-Za-z0-9]{3,8})*))\\])?$", | ||
"i" | ||
); | ||
const match = timeRegex.exec(time); | ||
let hour: i32, | ||
minute: i32, | ||
second: i32, | ||
millisecond: i32, | ||
microsecond: i32, | ||
nanosecond: i32; | ||
if (match != null) { | ||
hour = I32.parseInt(match.matches[1]); | ||
// see https://github.com/ColinEberhardt/assemblyscript-regex/issues/38 | ||
minute = I32.parseInt( | ||
match.matches[2] != "" | ||
? match.matches[2] | ||
: match.matches[5] != "" | ||
? match.matches[5] | ||
: match.matches[13] | ||
); | ||
second = I32.parseInt( | ||
match.matches[3] != "" | ||
? match.matches[3] | ||
: match.matches[6] != "" | ||
? match.matches[6] | ||
: match.matches[14] | ||
); | ||
if (second === 60) second = 59; | ||
const fraction = | ||
(match.matches[4] != "" | ||
? match.matches[4] | ||
: match.matches[7] != "" | ||
? match.matches[7] | ||
: match.matches[15]) + "000000000"; | ||
millisecond = I32.parseInt(fraction.substring(0, 3)); | ||
microsecond = I32.parseInt(fraction.substring(3, 6)); | ||
nanosecond = I32.parseInt(fraction.substring(6, 9)); | ||
return new PlainTime( | ||
hour, | ||
minute, | ||
second, | ||
millisecond, | ||
microsecond, | ||
nanosecond | ||
); | ||
} else { | ||
const dateTime = PlainDateTime.fromString(time); | ||
return new PlainTime( | ||
dateTime.hour, | ||
dateTime.minute, | ||
dateTime.second, | ||
dateTime.millisecond, | ||
dateTime.microsecond, | ||
dateTime.nanosecond | ||
); | ||
} | ||
} | ||
@inline | ||
static from<T = TimeLike>(time: T): PlainTime { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>time); | ||
} else { | ||
if (isReference<T>()) { | ||
if (time instanceof PlainTime) { | ||
return this.fromPlainTime(time); | ||
} else if (time instanceof TimeLike) { | ||
return this.fromTimeLike(time); | ||
} else if (time instanceof PlainDateTime) { | ||
return this.fromPlainDateTime(time); | ||
} | ||
} | ||
throw new TypeError("invalid time type"); | ||
} | ||
} | ||
constructor( | ||
readonly hour: i32 = 0, | ||
readonly minute: i32 = 0, | ||
readonly second: i32 = 0, | ||
readonly hour: i32 = 0, | ||
readonly minute: i32 = 0, | ||
readonly second: i32 = 0, | ||
readonly millisecond: i32 = 0, | ||
readonly microsecond: i32 = 0, | ||
readonly nanosecond: i32 = 0 | ||
readonly nanosecond: i32 = 0 | ||
) { | ||
if (!( | ||
checkRange(hour, 0, 23) && | ||
checkRange(minute, 0, 59) && | ||
checkRange(second, 0, 59) && | ||
checkRange(millisecond, 0, 999) && | ||
checkRange(microsecond, 0, 999) && | ||
checkRange(nanosecond, 0, 999) | ||
)) throw new RangeError("invalid plain time"); | ||
if ( | ||
!( | ||
checkRange(hour, 0, 23) && | ||
checkRange(minute, 0, 59) && | ||
checkRange(second, 0, 59) && | ||
checkRange(millisecond, 0, 999) && | ||
checkRange(microsecond, 0, 999) && | ||
checkRange(nanosecond, 0, 999) | ||
) | ||
) | ||
throw new RangeError("invalid plain time"); | ||
} | ||
with(timeLike: TimeLike): PlainTime { | ||
return new PlainTime( | ||
coalesce(timeLike.hour, this.hour), | ||
coalesce(timeLike.minute, this.minute), | ||
coalesce(timeLike.second, this.second), | ||
coalesce(timeLike.millisecond, this.millisecond), | ||
coalesce(timeLike.microsecond, this.microsecond), | ||
coalesce(timeLike.nanosecond, this.nanosecond) | ||
); | ||
} | ||
toString(): string { | ||
// 22:54:31 | ||
return ( | ||
toPaddedString(this.hour) + | ||
":" + | ||
toPaddedString(this.minute) + | ||
":" + | ||
toPaddedString(this.second) + | ||
((this.nanosecond | this.microsecond | this.millisecond) != 0 | ||
? ( | ||
f64(this.nanosecond) / 1_000_000_000.0 + | ||
f64(this.microsecond) / 1_000_000.0 + | ||
f64(this.millisecond) / 1_000.0 | ||
) | ||
.toString() | ||
.substring(1) | ||
: "") | ||
); | ||
} | ||
toPlainDateTime(dateLike: DateLike | null = null): PlainDateTime { | ||
let year = 0, month = 0, day = 0; | ||
if (dateLike !== null) { | ||
year = coalesce(dateLike.year, 0); | ||
month = coalesce(dateLike.month, 0); | ||
day = coalesce(dateLike.day, 0); | ||
} | ||
return new PlainDateTime( | ||
year, | ||
month, | ||
day, | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond | ||
); | ||
} | ||
until( | ||
other: PlainTime, | ||
largestUnit: TimeComponent = TimeComponent.hours | ||
): Duration { | ||
if ( | ||
largestUnit == TimeComponent.years || | ||
largestUnit == TimeComponent.months || | ||
largestUnit == TimeComponent.weeks || | ||
largestUnit == TimeComponent.days | ||
) { | ||
throw new RangeError("higher units are not allowed"); | ||
} | ||
let diffTime = differenceTime( | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond, | ||
other.hour, | ||
other.minute, | ||
other.second, | ||
other.millisecond, | ||
other.microsecond, | ||
other.nanosecond, | ||
) | ||
return balanceDuration( | ||
// diffTime.days, | ||
0, | ||
diffTime.hours, | ||
diffTime.minutes, | ||
diffTime.seconds, | ||
diffTime.milliseconds, | ||
diffTime.microseconds, | ||
diffTime.nanoseconds, | ||
largestUnit | ||
); | ||
} | ||
since( | ||
other: PlainTime, | ||
largestUnit: TimeComponent = TimeComponent.hours | ||
): Duration { | ||
if ( | ||
largestUnit == TimeComponent.years || | ||
largestUnit == TimeComponent.months || | ||
largestUnit == TimeComponent.weeks || | ||
largestUnit == TimeComponent.days | ||
) { | ||
throw new RangeError("higher units are not allowed"); | ||
} | ||
let diffTime = differenceTime( | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond, | ||
other.hour, | ||
other.minute, | ||
other.second, | ||
other.millisecond, | ||
other.microsecond, | ||
other.nanosecond, | ||
) | ||
return balanceDuration( | ||
// diffTime.days, | ||
0, | ||
-diffTime.hours, | ||
-diffTime.minutes, | ||
-diffTime.seconds, | ||
-diffTime.milliseconds, | ||
-diffTime.microseconds, | ||
-diffTime.nanoseconds, | ||
largestUnit | ||
); | ||
} | ||
equals(other: PlainTime): bool { | ||
if (this === other) return true; | ||
return ( | ||
this.nanosecond == other.nanosecond && | ||
this.nanosecond == other.nanosecond && | ||
this.microsecond == other.microsecond && | ||
@@ -44,3 +304,3 @@ this.millisecond == other.millisecond && | ||
this.minute == other.minute && | ||
this.hour == other.hour | ||
this.hour == other.hour | ||
); | ||
@@ -69,2 +329,84 @@ } | ||
} | ||
add<T = DurationLike>(durationToAdd: T): PlainTime { | ||
const duration = | ||
durationToAdd instanceof DurationLike | ||
? durationToAdd.toDuration() | ||
// @ts-ignore TS2352 | ||
: (durationToAdd as Duration); | ||
const newTime = addTime( | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond, | ||
duration.hours, | ||
duration.minutes, | ||
duration.seconds, | ||
duration.milliseconds, | ||
duration.microseconds, | ||
duration.nanoseconds | ||
); | ||
const regulatedTime = regulateTime( | ||
newTime.hour, | ||
newTime.minute, | ||
newTime.second, | ||
newTime.millisecond, | ||
newTime.microsecond, | ||
newTime.nanosecond, | ||
Overflow.Reject | ||
) | ||
return new PlainTime( | ||
regulatedTime.hour, | ||
regulatedTime.minute, | ||
regulatedTime.second, | ||
regulatedTime.millisecond, | ||
regulatedTime.microsecond, | ||
regulatedTime.nanosecond | ||
); | ||
} | ||
subtract<T = DurationLike>(durationToSubtract: T): PlainTime { | ||
const duration = | ||
durationToSubtract instanceof DurationLike | ||
? durationToSubtract.toDuration() | ||
// @ts-ignore TS2352 | ||
: (durationToSubtract as Duration); | ||
const newTime = addTime( | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond, | ||
-duration.hours, | ||
-duration.minutes, | ||
-duration.seconds, | ||
-duration.milliseconds, | ||
-duration.microseconds, | ||
-duration.nanoseconds | ||
); | ||
const regulatedTime = regulateTime( | ||
newTime.hour, | ||
newTime.minute, | ||
newTime.second, | ||
newTime.millisecond, | ||
newTime.microsecond, | ||
newTime.nanosecond, | ||
Overflow.Reject | ||
) | ||
return new PlainTime( | ||
regulatedTime.hour, | ||
regulatedTime.minute, | ||
regulatedTime.second, | ||
regulatedTime.millisecond, | ||
regulatedTime.microsecond, | ||
regulatedTime.nanosecond | ||
); | ||
} | ||
} |
@@ -31,2 +31,11 @@ // for the proposal-temporal implementation, most of the business logic | ||
export class PT { | ||
hour: i32; | ||
minute: i32; | ||
second: i32; | ||
millisecond: i32; | ||
microsecond: i32; | ||
nanosecond: i32; | ||
} | ||
export class DT { | ||
@@ -182,3 +191,3 @@ year: i32; | ||
// @ts-ignore | ||
return (x >> (sizeof<T>() * 4 - 1)) | 1; | ||
return (x >> (sizeof<T>() * 8 - 1)) | 1; | ||
} | ||
@@ -401,2 +410,10 @@ | ||
let nanosecondsI64: i64 = 0; | ||
let microsecondsI64: i64 = 0; | ||
let millisecondsI64: i64 = 0; | ||
let secondsI64: i64 = 0; | ||
let minutesI64: i64 = 0; | ||
let hoursI64: i64 = 0; | ||
let daysI64: i64 = 0; | ||
if ( | ||
@@ -407,15 +424,11 @@ largestUnit >= TimeComponent.years && | ||
const _ES$NanosecondsToDays = nanosecondsToDays(durationNs); | ||
days = _ES$NanosecondsToDays.days; | ||
nanoseconds = _ES$NanosecondsToDays.nanoseconds; | ||
daysI64 = _ES$NanosecondsToDays.days; | ||
nanosecondsI64 = _ES$NanosecondsToDays.nanoseconds; | ||
} else { | ||
days = 0; | ||
daysI64 = 0; | ||
nanosecondsI64 = durationNs | ||
} | ||
const sig = sign(nanoseconds); | ||
nanoseconds = abs(nanoseconds); | ||
microseconds = 0; | ||
milliseconds = 0; | ||
seconds = 0; | ||
minutes = 0; | ||
hours = 0; | ||
const sig = i32(sign(nanosecondsI64)); | ||
nanosecondsI64 = abs(nanosecondsI64); | ||
@@ -428,54 +441,54 @@ switch (largestUnit) { | ||
case TimeComponent.hours: | ||
microseconds = nanoseconds / 1000; | ||
nanoseconds = nanoseconds % 1000; | ||
microsecondsI64 = nanosecondsI64 / 1000; | ||
nanosecondsI64 = nanosecondsI64 % 1000; | ||
milliseconds = microseconds / 1000; | ||
microseconds = microseconds % 1000; | ||
millisecondsI64 = microsecondsI64 / 1000; | ||
microsecondsI64 = microsecondsI64 % 1000; | ||
seconds = milliseconds / MILLIS_PER_SECOND; | ||
milliseconds = milliseconds % 1000; | ||
secondsI64 = millisecondsI64 / 1000; | ||
millisecondsI64 = millisecondsI64 % 1000; | ||
minutes = seconds / 60; | ||
seconds = seconds % 60; | ||
minutesI64 = secondsI64 / 60; | ||
secondsI64 = secondsI64 % 60; | ||
hours = minutes / 60; | ||
minutes = minutes % 60; | ||
hoursI64 = minutesI64 / 60; | ||
minutesI64 = minutesI64 % 60; | ||
break; | ||
case TimeComponent.minutes: | ||
microseconds = nanoseconds / 1000; | ||
nanoseconds = nanoseconds % 1000; | ||
microsecondsI64 = nanosecondsI64 / 1000; | ||
nanosecondsI64 = nanosecondsI64 % 1000; | ||
milliseconds = microseconds / 1000; | ||
microseconds = microseconds % 1000; | ||
millisecondsI64 = microsecondsI64 / 1000; | ||
microsecondsI64 = microsecondsI64 % 1000; | ||
seconds = milliseconds / MILLIS_PER_SECOND; | ||
milliseconds = milliseconds % 1000; | ||
secondsI64 = millisecondsI64 / 1000; | ||
millisecondsI64 = millisecondsI64 % 1000; | ||
minutes = seconds / 60; | ||
seconds = seconds % 60; | ||
minutesI64 = secondsI64 / 60; | ||
secondsI64 = secondsI64 % 60; | ||
break; | ||
case TimeComponent.seconds: | ||
microseconds = nanoseconds / 1000; | ||
nanoseconds = nanoseconds % 1000; | ||
microsecondsI64 = nanosecondsI64 / 1000; | ||
nanosecondsI64 = nanosecondsI64 % 1000; | ||
milliseconds = microseconds / 1000; | ||
microseconds = microseconds % 1000; | ||
millisecondsI64 = microsecondsI64 / 1000; | ||
microsecondsI64 = microsecondsI64 % 1000; | ||
seconds = milliseconds / MILLIS_PER_SECOND; | ||
milliseconds = milliseconds % 1000; | ||
secondsI64 = millisecondsI64 / 1000; | ||
millisecondsI64 = millisecondsI64 % 1000; | ||
break; | ||
case TimeComponent.milliseconds: | ||
microseconds = nanoseconds / 1000; | ||
nanoseconds = nanoseconds % 1000; | ||
microsecondsI64 = nanosecondsI64 / 1000; | ||
nanosecondsI64 = nanosecondsI64 % 1000; | ||
milliseconds = microseconds / 1000; | ||
microseconds = microseconds % 1000; | ||
millisecondsI64 = microsecondsI64 / 1000; | ||
microsecondsI64 = microsecondsI64 % 1000; | ||
break; | ||
case TimeComponent.microseconds: | ||
microseconds = nanoseconds / 1000; | ||
nanoseconds = nanoseconds % 1000; | ||
microsecondsI64 = nanosecondsI64 / 1000; | ||
nanosecondsI64 = nanosecondsI64 % 1000; | ||
break; | ||
@@ -491,9 +504,9 @@ | ||
0, | ||
days, | ||
hours * sig, | ||
minutes * sig, | ||
seconds * sig, | ||
milliseconds * sig, | ||
microseconds * sig, | ||
nanoseconds * sig | ||
i32(daysI64), | ||
i32(hoursI64) * sig, | ||
i32(minutesI64) * sig, | ||
i32(secondsI64) * sig, | ||
i32(millisecondsI64) * sig, | ||
i32(microsecondsI64) * sig, | ||
i32(nanosecondsI64) * sig | ||
); | ||
@@ -680,3 +693,60 @@ } | ||
// https://github.com/tc39/proposal-temporal/blob/515ee6e339bb4a1d3d6b5a42158f4de49f9ed953/polyfill/lib/ecmascript.mjs#L2874-L2910 | ||
export function differenceTime( | ||
h1: i32, | ||
min1: i32, | ||
s1: i32, | ||
ms1: i32, | ||
µs1:i32, | ||
ns1: i32, | ||
h2: i32, | ||
min2: i32, | ||
s2: i32, | ||
ms2: i32, | ||
µs2: i32, | ||
ns2: i32 | ||
): Duration { | ||
let hours = h2 - h1; | ||
let minutes = min2 - min1; | ||
let seconds = s2 - s1; | ||
let milliseconds = ms2 - ms1; | ||
let microseconds = µs2 - µs1; | ||
let nanoseconds = ns2 - ns1; | ||
const sign = durationSign( | ||
0, | ||
0, | ||
0, | ||
0, | ||
hours, | ||
minutes, | ||
seconds, | ||
milliseconds, | ||
microseconds, | ||
nanoseconds | ||
); | ||
hours *= sign; | ||
minutes *= sign; | ||
seconds *= sign; | ||
milliseconds *= sign; | ||
microseconds *= sign; | ||
nanoseconds *= sign; | ||
let balancedTime = balanceTime(hours, minutes, seconds, milliseconds, microseconds, nanoseconds); | ||
return new Duration( | ||
0, | ||
0, | ||
0, | ||
balancedTime.deltaDays * sign, | ||
balancedTime.hour * sign, | ||
balancedTime.minute * sign, | ||
balancedTime.second * sign, | ||
balancedTime.millisecond * sign, | ||
balancedTime.microsecond * sign, | ||
balancedTime.nanosecond * sign | ||
) | ||
} | ||
export function epochFromParts( | ||
@@ -770,3 +840,4 @@ year: i32, | ||
function addTime( | ||
// https://github.com/tc39/proposal-temporal/blob/515ee6e339bb4a1d3d6b5a42158f4de49f9ed953/polyfill/lib/ecmascript.mjs#L2676-L2684 | ||
export function constrainTime( | ||
hour: i32, | ||
@@ -778,2 +849,55 @@ minute: i32, | ||
nanosecond: i32, | ||
): PT { | ||
hour = clamp(hour, 0, 23); | ||
minute = clamp(minute, 0, 59); | ||
second = clamp(second, 0, 59); | ||
millisecond = clamp(millisecond, 0, 999); | ||
microsecond = clamp(microsecond, 0, 999); | ||
nanosecond = clamp(nanosecond, 0, 999); | ||
return { hour, minute, second, millisecond, microsecond, nanosecond }; | ||
} | ||
// https://github.com/tc39/proposal-temporal/blob/515ee6e339bb4a1d3d6b5a42158f4de49f9ed953/polyfill/lib/ecmascript.mjs#L407-L422 | ||
export function regulateTime( | ||
hour: i32, | ||
minute: i32, | ||
second: i32, | ||
millisecond: i32, | ||
microsecond: i32, | ||
nanosecond: i32, | ||
overflow: Overflow | ||
): PT { | ||
switch (overflow) { | ||
case Overflow.Reject: | ||
// rejectTime(hour, minute, second, millisecond, microsecond, nanosecond); | ||
break; | ||
case Overflow.Constrain: | ||
const time = constrainTime( | ||
hour, | ||
minute, | ||
second, | ||
millisecond, | ||
microsecond, | ||
nanosecond | ||
); | ||
hour = time.hour; | ||
minute = time.minute; | ||
second = time.second; | ||
millisecond = time.millisecond; | ||
microsecond = time.microsecond; | ||
nanosecond = time.nanosecond; | ||
break; | ||
} | ||
return { hour, minute, second, millisecond, microsecond, nanosecond }; | ||
} | ||
export function addTime( | ||
hour: i32, | ||
minute: i32, | ||
second: i32, | ||
millisecond: i32, | ||
microsecond: i32, | ||
nanosecond: i32, | ||
hours: i32, | ||
@@ -780,0 +904,0 @@ minutes: i32, |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "An implementation of temporal within AssemblyScript, with an initial focus on non-timezone-aware classes and functionality.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,3 +26,3 @@ ## assemblyscript-temporal | ||
datetime = PlainDateTime.fromString("1976-11-18T12:34:56"); | ||
datetime = PlainDateTime.from("1976-11-18T12:34:56"); | ||
datetime.toString(); // "1976-11-18T12:34:56" | ||
@@ -32,3 +32,3 @@ | ||
datetime.toString(); // "1966-03-03T00:00:00" | ||
``` | ||
``` | ||
@@ -48,3 +48,3 @@ There are various ways you can manipulate a date: | ||
datetime.add(new Duration(1)).toString(); // "2021-01-12T15:00:00"); | ||
``` | ||
``` | ||
@@ -54,4 +54,4 @@ You can compare dates and check for equality | ||
```javascript | ||
dt1 = PlainDateTime.fromString("1976-11-18"); | ||
dt2 = PlainDateTime.fromString("2019-10-29"); | ||
dt1 = PlainDateTime.from("1976-11-18"); | ||
dt2 = PlainDateTime.from("2019-10-29"); | ||
PlainDateTime.compare(dt1, dt1); // 0 | ||
@@ -62,3 +62,3 @@ PlainDateTime.compare(dt1, dt2); // -1 | ||
Currently `PlainDateTime` only supports the ISO 8601 (Gregorian) calendar. | ||
Currently `PlainDateTime` only supports the ISO 8601 (Gregorian) calendar. | ||
@@ -83,2 +83,2 @@ #### `PlainDate` | ||
This project is open source, MIT licenced and your contributions are very much welcomed. | ||
This project is open source, MIT licenced and your contributions are very much welcomed. |
Sorry, the diff of this file is too big to display
271177
27
6720
79