assemblyscript-temporal
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -224,3 +224,3 @@ import { JsDate } from "../date"; | ||
it("epoch", () => { | ||
expect(Date.UTC(1970)).toBe(0); | ||
expect(JsDate.UTC(1970)).toBe(0); | ||
}); | ||
@@ -230,7 +230,7 @@ | ||
it("0", () => { | ||
expect(Date.UTC(0)).toBe(Date.UTC(1900)); | ||
expect(JsDate.UTC(0)).toBe(JsDate.UTC(1900)); | ||
}); | ||
it("99", () => { | ||
expect(Date.UTC(99)).toBe(Date.UTC(1999)); | ||
expect(JsDate.UTC(99)).toBe(JsDate.UTC(1999)); | ||
}); | ||
@@ -240,24 +240,24 @@ }); | ||
it("defaults to month=0", () => { | ||
expect(Date.UTC(2001)).toBe(Date.UTC(2001, 0)); | ||
expect(JsDate.UTC(2001)).toBe(JsDate.UTC(2001, 0)); | ||
}); | ||
it("defaults to date=1", () => { | ||
expect(Date.UTC(2001, 0)).toBe(Date.UTC(2001, 0, 1)); | ||
expect(JsDate.UTC(2001, 0)).toBe(JsDate.UTC(2001, 0, 1)); | ||
}); | ||
it("defaults to hours=0", () => { | ||
expect(Date.UTC(2001, 0, 1)).toBe(Date.UTC(2001, 0, 1, 0)); | ||
expect(JsDate.UTC(2001, 0, 1)).toBe(JsDate.UTC(2001, 0, 1, 0)); | ||
}); | ||
it("defaults to minutes=0", () => { | ||
expect(Date.UTC(2001, 0, 1, 0)).toBe(Date.UTC(2001, 0, 1, 0, 0)); | ||
expect(JsDate.UTC(2001, 0, 1, 0)).toBe(JsDate.UTC(2001, 0, 1, 0, 0)); | ||
}); | ||
it("defaults to seconds=0", () => { | ||
expect(Date.UTC(2001, 0, 1, 0, 0)).toBe(Date.UTC(2001, 0, 1, 0, 0, 0)); | ||
expect(JsDate.UTC(2001, 0, 1, 0, 0)).toBe(JsDate.UTC(2001, 0, 1, 0, 0, 0)); | ||
}); | ||
it("defaults to milliseconds=0", () => { | ||
expect(Date.UTC(2001, 0, 1, 0, 0, 0)).toBe( | ||
Date.UTC(2001, 0, 1, 0, 0, 0, 0) | ||
expect(JsDate.UTC(2001, 0, 1, 0, 0, 0)).toBe( | ||
JsDate.UTC(2001, 0, 1, 0, 0, 0, 0) | ||
); | ||
@@ -269,24 +269,24 @@ }); | ||
it("8639999999999999", () => { | ||
expect(Date.UTC(275760, 8, 12, 23, 59, 59, 999)).toBe(8639999999999999); | ||
expect(JsDate.UTC(275760, 8, 12, 23, 59, 59, 999)).toBe(8639999999999999); | ||
}); | ||
it("8640000000000000", () => { | ||
expect(Date.UTC(275760, 8, 13)).toBe(8640000000000000); | ||
expect(JsDate.UTC(275760, 8, 13)).toBe(8640000000000000); | ||
}); | ||
it("-8639999999999999", () => { | ||
expect(Date.UTC(-271821, 3, 20, 0, 0, 0, 1)).toBe(-8639999999999999); | ||
expect(JsDate.UTC(-271821, 3, 20, 0, 0, 0, 1)).toBe(-8639999999999999); | ||
}); | ||
it("-8640000000000000", () => { | ||
expect(Date.UTC(-271821, 3, 20)).toBe(-8640000000000000); | ||
expect(JsDate.UTC(-271821, 3, 20)).toBe(-8640000000000000); | ||
}); | ||
it("obscure date values", () => { | ||
expect(Date.UTC(1970, 0, 1 + 100000001, -24)).toBe(8640000000000000); | ||
expect(JsDate.UTC(1970, 0, 1 + 100000001, -24)).toBe(8640000000000000); | ||
}); | ||
it("obscure date values", () => { | ||
expect(Date.UTC(1970, 0, 1 - 100000001, 24)).toBe(-8640000000000000); | ||
expect(JsDate.UTC(1970, 0, 1 - 100000001, 24)).toBe(-8640000000000000); | ||
}); | ||
}); |
@@ -20,2 +20,3 @@ import { RegExp } from "assemblyscript-regex"; | ||
coalesce, | ||
parseISOString, | ||
} from "./utils"; | ||
@@ -61,20 +62,8 @@ | ||
private static fromString(date: string): PlainDate { | ||
const dateRegex = new RegExp( | ||
"^((?:[+-]\\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})*))\\])?$", | ||
"i" | ||
const parsed = parseISOString(date); | ||
return new PlainDate( | ||
parsed.year, | ||
parsed.month, | ||
parsed.day | ||
); | ||
const match = dateRegex.exec(date); | ||
if (match != null) { | ||
return new PlainDate( | ||
I32.parseInt(match.matches[1]), | ||
// see https://github.com/ColinEberhardt/assemblyscript-regex/issues/38 | ||
I32.parseInt( | ||
match.matches[2] != "" ? match.matches[2] : match.matches[19] | ||
), | ||
I32.parseInt( | ||
match.matches[3] != "" ? match.matches[3] : match.matches[20] | ||
) | ||
); | ||
} | ||
throw new RangeError("invalid ISO 8601 string: " + date); | ||
} | ||
@@ -81,0 +70,0 @@ |
@@ -17,2 +17,3 @@ import { RegExp } from "assemblyscript-regex"; | ||
addDateTime, | ||
parseISOString, | ||
} from "./utils"; | ||
@@ -84,29 +85,14 @@ | ||
private static fromString(date: string): PlainDateTime { | ||
const dateRegex = new RegExp( | ||
"^((?:[+\u2212-]\\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])|(?:([+\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 parsed = parseISOString(date); | ||
return new PlainDateTime( | ||
parsed.year, | ||
parsed.month, | ||
parsed.day, | ||
parsed.hour, | ||
parsed.minute, | ||
parsed.second, | ||
parsed.millisecond, | ||
parsed.microsecond, | ||
parsed.nanosecond | ||
); | ||
const match = dateRegex.exec(date); | ||
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]), | ||
I32.parseInt( | ||
match.matches[2] != "" ? match.matches[2] : match.matches[19] | ||
), | ||
I32.parseInt( | ||
match.matches[3] != "" ? match.matches[3] : match.matches[20] | ||
), | ||
I32.parseInt(match.matches[4]), | ||
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)) | ||
); | ||
} | ||
throw new RangeError("invalid ISO 8601 string: " + date); | ||
} | ||
@@ -113,0 +99,0 @@ |
import { Instant } from "./instant"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { getPartsFromEpoch, balanceDateTime, formatTimeZoneOffsetString } from "./utils"; | ||
import { | ||
getPartsFromEpoch, | ||
balanceDateTime, | ||
formatTimeZoneOffsetString, | ||
} from "./utils"; | ||
import { offsetForTimezone } from "./tz/index"; | ||
@@ -9,7 +13,11 @@ | ||
getOffsetNanosecondsFor(instant: Instant): i64 { | ||
return this.timezone == "UTC" | ||
? 0 | ||
: i64(offsetForTimezone(this.timezone, instant.epochMilliseconds)) * | ||
1_000_000; | ||
} | ||
getPlainDateTimeFor(instant: Instant): PlainDateTime { | ||
const offsetNs: i64 = | ||
this.timezone == "UTC" | ||
? 0 | ||
: i64(offsetForTimezone(this.timezone, instant.epochMilliseconds)) * 1_000_000; | ||
const offsetNs = this.getOffsetNanosecondsFor(instant); | ||
const parts = getPartsFromEpoch(instant.epochNanoseconds); | ||
@@ -43,7 +51,5 @@ | ||
getOffsetStringFor(instant: Instant): string { | ||
const offsetNs: i64 = this.timezone == "UTC" | ||
? 0 | ||
: i64(offsetForTimezone(this.timezone, instant.epochMilliseconds)) * 1_000_000; | ||
const offsetNs = this.getOffsetNanosecondsFor(instant); | ||
return formatTimeZoneOffsetString(offsetNs); | ||
} | ||
} |
@@ -9,2 +9,4 @@ // for the proposal-temporal implementation, most of the business logic | ||
import { RegExp } from "assemblyscript-regex"; | ||
import { Duration } from "./duration"; | ||
@@ -60,2 +62,6 @@ import { Overflow, TimeComponent } from "./enums"; | ||
export class DTZ extends DT { | ||
timezone: string; | ||
} | ||
export class NanoDays { | ||
@@ -79,10 +85,6 @@ days: i32; | ||
@inline | ||
export function floorDiv(a: i32, b: i32): i32 { | ||
return (a >= 0 ? a : a - b + 1) / b; | ||
export function floorDiv<T extends number>(a: T, b: T): T { | ||
return (a >= 0 ? a : a - b + 1) / b as T; | ||
} | ||
export function floorDivI64(a: i64, b: i64): i64 { | ||
return (a >= 0 ? a : a - b + 1) / b; | ||
} | ||
// @ts-ignore: decorator | ||
@@ -967,23 +969,23 @@ @inline | ||
let quotient = floorDivI64(nanosecond, 1000); | ||
let quotient = floorDiv(nanosecond, 1000); | ||
microsecond += quotient; | ||
nanosecond -= quotient * 1000; | ||
quotient = floorDivI64(microsecond, 1000); | ||
quotient = floorDiv(microsecond, 1000); | ||
millisecond += quotient; | ||
microsecond -= quotient * 1000; | ||
quotient = floorDivI64(millisecond, 1000); | ||
quotient = floorDiv(millisecond, 1000); | ||
second += quotient; | ||
millisecond -= quotient * 1000; | ||
quotient = floorDivI64(second, 60); | ||
quotient = floorDiv(second, 60); | ||
minute += quotient; | ||
second -= quotient * 60; | ||
quotient = floorDivI64(minute, 60); | ||
quotient = floorDiv(minute, 60); | ||
hour += quotient; | ||
minute -= quotient * 60; | ||
let deltaDays = floorDivI64(hour, 24); | ||
let deltaDays = floorDiv(hour, 24); | ||
hour -= deltaDays * 24; | ||
@@ -993,6 +995,6 @@ | ||
deltaDays: i32(deltaDays), | ||
hour : i32(hour), | ||
minute : i32(minute), | ||
hour: i32(hour), | ||
minute: i32(minute), | ||
second: i32(second), | ||
millisecond : i32(millisecond), | ||
millisecond: i32(millisecond), | ||
microsecond: i32(microsecond), | ||
@@ -1058,1 +1060,32 @@ nanosecond: i32(nanosecond) | ||
} | ||
export function parseISOString(date: string): DTZ { | ||
const dateRegex = new RegExp( | ||
"^((?:[+\u2212-]\\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])|(?:([+\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 = dateRegex.exec(date); | ||
if (match == null) { | ||
throw new RangeError("invalid ISO 8601 string: " + date); | ||
} | ||
// see https://github.com/ColinEberhardt/assemblyscript-regex/issues/38 | ||
const fraction = ( | ||
match.matches[7] != "" ? match.matches[7] : match.matches[18] | ||
) + "000000000"; | ||
return { | ||
year: I32.parseInt(match.matches[1]), | ||
month: I32.parseInt( | ||
match.matches[2] != "" ? match.matches[2] : match.matches[19] | ||
), | ||
day: I32.parseInt( | ||
match.matches[3] != "" ? match.matches[3] : match.matches[20] | ||
), | ||
hour: I32.parseInt(match.matches[4]), | ||
minute: I32.parseInt(match.matches[5] != "" ? match.matches[5]: match.matches[16]), | ||
second: I32.parseInt(match.matches[6] != "" ? match.matches[6]: match.matches[17]), | ||
millisecond: I32.parseInt(fraction.substring(0, 3)), | ||
microsecond: I32.parseInt(fraction.substring(3, 6)), | ||
nanosecond: I32.parseInt(fraction.substring(6, 9)), | ||
timezone: match.matches[9] | ||
} | ||
} |
@@ -1,12 +0,45 @@ | ||
import { | ||
MICROS_PER_SECOND, | ||
MILLIS_PER_SECOND, | ||
NANOS_PER_SECOND, | ||
} from "./constants"; | ||
import { RegExp } from "assemblyscript-regex"; | ||
import { Instant } from "./instant"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { DateTimeLike, PlainDateTime } from "./plaindatetime"; | ||
import { TimeZone } from "./timezone"; | ||
import { toPaddedString } from "./utils"; | ||
import { JsDate } from "./date"; | ||
import { parseISOString } from "./utils"; | ||
export class ZonedDateTime { | ||
@inline | ||
static from<T = DateTimeLike>(date: T): ZonedDateTime { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
return this.fromString(<string>date); | ||
} else { | ||
throw new TypeError("invalid date type"); | ||
} | ||
} | ||
private static fromString(date: string): ZonedDateTime { | ||
const parsed = parseISOString(date); | ||
if (parsed.timezone == "") { | ||
throw new RangeError("time zone ID required in brackets"); | ||
} | ||
const epochMillis = JsDate.UTC( | ||
parsed.year, | ||
parsed.month - 1, | ||
parsed.day, | ||
parsed.hour, | ||
parsed.minute, | ||
parsed.second, | ||
parsed.millisecond | ||
); | ||
const epochNanos = | ||
i64(epochMillis) * 1_000_000 + | ||
i64(parsed.microsecond) * 1_000 + | ||
i64(parsed.nanosecond); | ||
const timezone = new TimeZone(parsed.timezone); | ||
return new ZonedDateTime( | ||
epochNanos - timezone.getOffsetNanosecondsFor(new Instant(epochNanos)), | ||
timezone | ||
); | ||
} | ||
private plainDateTime: PlainDateTime; | ||
@@ -22,2 +55,6 @@ | ||
toPlainDateTime(): PlainDateTime { | ||
return this.plainDateTime; | ||
} | ||
get year(): i32 { | ||
@@ -64,28 +101,10 @@ return this.plainDateTime.year; | ||
toString(): string { | ||
// TODO: refactor from PlainDateTime | ||
// 1976-11-18T00:00:00 | ||
return ( | ||
this.year.toString() + | ||
"-" + | ||
toPaddedString(this.month) + | ||
"-" + | ||
toPaddedString(this.day) + | ||
"T" + | ||
toPaddedString(this.hour) + | ||
":" + | ||
toPaddedString(this.minute) + | ||
":" + | ||
toPaddedString(this.second) + | ||
(this.nanosecond != 0 || this.microsecond != 0 || this.millisecond != 0 | ||
? ( | ||
f64(this.nanosecond) / NANOS_PER_SECOND + | ||
f64(this.microsecond) / MICROS_PER_SECOND + | ||
f64(this.millisecond) / MILLIS_PER_SECOND | ||
) | ||
.toString() | ||
.substring(1) | ||
: "") + | ||
this.offset | ||
this.toPlainDateTime().toString() + | ||
this.offset + | ||
"[" + | ||
this.tz.timezone + | ||
"]" | ||
); | ||
} | ||
} |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "1.7.0", | ||
"version": "1.8.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", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2456139
24132