assemblyscript-temporal
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -6,3 +6,2 @@ import { RegExp } from "assemblyscript-regex"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { TimeComponent } from "./enums"; | ||
@@ -9,0 +8,0 @@ // @ts-ignore |
@@ -0,2 +1,49 @@ | ||
import { Duration, DurationLike } from "./duration"; | ||
import { TimeComponent } from "./enums"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { | ||
getPartsFromEpoch, | ||
formatISOString, | ||
ord, | ||
balanceDuration, | ||
} from "./utils"; | ||
export class Instant { | ||
@inline | ||
public static from<T>(instant: T): Instant { | ||
if (isString<T>()) { | ||
// @ts-ignore: cast | ||
const pdt = PlainDateTime.from(<string>instant); | ||
return new Instant(pdt.epochNanoseconds); | ||
} else { | ||
if (isReference<T>()) { | ||
if (instant instanceof Instant) { | ||
return new Instant(instant.epochNanoseconds); | ||
} | ||
} | ||
throw new TypeError("invalid instant type"); | ||
} | ||
} | ||
@inline | ||
static compare(i1: Instant, i2: Instant): i32 { | ||
return ord(i1.epochNanoseconds, i2.epochNanoseconds); | ||
} | ||
public static fromEpochSeconds(seconds: i64): Instant { | ||
return new Instant(seconds * 1_000_000_000); | ||
} | ||
public static fromEpochMilliseconds(millis: i64): Instant { | ||
return new Instant(millis * 1_000_000); | ||
} | ||
public static fromEpochMicroseconds(micros: i64): Instant { | ||
return new Instant(micros * 1_000); | ||
} | ||
public static fromEpochNanoseconds(nanos: i64): Instant { | ||
return new Instant(nanos); | ||
} | ||
constructor(public epochNanoseconds: i64) {} | ||
@@ -18,2 +65,84 @@ | ||
} | ||
add<T = DurationLike>(durationToAdd: T): Instant { | ||
const duration = Duration.from(durationToAdd); | ||
if ( | ||
duration.years != 0 || | ||
duration.months != 0 || | ||
duration.weeks != 0 || | ||
duration.days != 0 | ||
) { | ||
throw new RangeError("invalid duration field"); | ||
} | ||
return new Instant(this.epochNanoseconds + durationToNanos(duration)); | ||
} | ||
subtract<T = DurationLike>(durationToAdd: T): Instant { | ||
const duration = Duration.from(durationToAdd); | ||
if ( | ||
duration.years != 0 || | ||
duration.months != 0 || | ||
duration.weeks != 0 || | ||
duration.days != 0 | ||
) { | ||
throw new RangeError("invalid duration field"); | ||
} | ||
return new Instant(this.epochNanoseconds - durationToNanos(duration)); | ||
} | ||
since( | ||
instant: Instant, | ||
largestUnit: TimeComponent = TimeComponent.Seconds | ||
): Duration { | ||
if (largestUnit <= TimeComponent.Days) { | ||
throw new RangeError("Largest unit must be smaller than days") | ||
} | ||
const diffNanos = this.epochNanoseconds - instant.epochNanoseconds; | ||
return balanceDuration(0, 0, 0, 0, 0, 0, diffNanos, largestUnit); | ||
} | ||
until( | ||
instant: Instant, | ||
largestUnit: TimeComponent = TimeComponent.Seconds | ||
): Duration { | ||
if (largestUnit <= TimeComponent.Days) { | ||
throw new RangeError("Largest unit must be smaller than days") | ||
} | ||
const diffNanos = instant.epochNanoseconds - this.epochNanoseconds; | ||
return balanceDuration(0, 0, 0, 0, 0, 0, diffNanos, largestUnit); | ||
} | ||
equals(other: Instant): boolean { | ||
return this.epochNanoseconds == other.epochNanoseconds; | ||
} | ||
@inline | ||
toString(): string { | ||
const parts = getPartsFromEpoch(this.epochNanoseconds); | ||
return ( | ||
formatISOString( | ||
parts.year, | ||
parts.month, | ||
parts.day, | ||
parts.hour, | ||
parts.minute, | ||
parts.second, | ||
parts.millisecond, | ||
parts.microsecond, | ||
parts.nanosecond | ||
) + "Z" | ||
); | ||
} | ||
} | ||
function durationToNanos(duration: Duration): i64 { | ||
return ( | ||
i64(duration.nanoseconds) + | ||
i64(duration.microseconds) * 1_000 + | ||
i64(duration.milliseconds) * 1_000_000 + | ||
i64(duration.seconds) * 1_000_000_000 + | ||
i64(duration.minutes) * 60 * 1_000_000_000 + | ||
i64(duration.hours) * 60 * 60 * 1_000_000_000 | ||
); | ||
} |
import { PlainDateTime } from "./plaindatetime"; | ||
import { JsDate } from "./date"; | ||
import { PlainDate } from "./plaindate"; | ||
@@ -8,3 +7,3 @@ | ||
const epochMillis = Date.now(); | ||
const date = new JsDate(epochMillis); | ||
const date = new Date(epochMillis); | ||
return new PlainDateTime( | ||
@@ -23,3 +22,3 @@ date.getUTCFullYear(), | ||
const epochMillis = Date.now(); | ||
const date = new JsDate(epochMillis); | ||
const date = new Date(epochMillis); | ||
return new PlainDate( | ||
@@ -26,0 +25,0 @@ date.getUTCFullYear(), |
@@ -1,3 +0,1 @@ | ||
import { RegExp } from "assemblyscript-regex"; | ||
import { Duration, DurationLike } from "./duration"; | ||
@@ -7,7 +5,2 @@ import { Overflow, TimeComponent } from "./enums"; | ||
import { | ||
MICROS_PER_SECOND, | ||
MILLIS_PER_SECOND, | ||
NANOS_PER_SECOND, | ||
} from "./constants"; | ||
import { | ||
dayOfWeek, | ||
@@ -18,3 +11,2 @@ dayOfYear, | ||
daysInYear, | ||
toPaddedString, | ||
coalesce, | ||
@@ -27,2 +19,3 @@ compareTemporalDateTime, | ||
differenceDateTime, | ||
formatISOString, | ||
} from "./utils"; | ||
@@ -259,25 +252,11 @@ import { PlainDate } from "./plaindate"; | ||
toString(): string { | ||
// 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) | ||
: "") | ||
); | ||
return formatISOString(this.year, | ||
this.month, | ||
this.day, | ||
this.hour, | ||
this.minute, | ||
this.second, | ||
this.millisecond, | ||
this.microsecond, | ||
this.nanosecond); | ||
} | ||
@@ -284,0 +263,0 @@ |
@@ -13,4 +13,3 @@ // for the proposal-temporal implementation, most of the business logic | ||
import { Overflow, TimeComponent } from "./enums"; | ||
import { MILLIS_PER_SECOND, NANOS_PER_SECOND } from "./constants"; | ||
import { JsDate } from "./date"; | ||
import { MICROS_PER_SECOND, MILLIS_PER_SECOND, NANOS_PER_SECOND } from "./constants"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
@@ -448,5 +447,5 @@ | ||
seconds: i32, | ||
milliseconds: i32, | ||
microseconds: i32, | ||
nanoseconds: i32, | ||
milliseconds: i64, | ||
microseconds: i64, | ||
nanoseconds: i64, | ||
largestUnit: TimeComponent | ||
@@ -459,5 +458,5 @@ ): Duration { | ||
seconds as i64, | ||
milliseconds as i64, | ||
microseconds as i64, | ||
nanoseconds as i64 | ||
milliseconds, | ||
microseconds, | ||
nanoseconds | ||
); | ||
@@ -1034,3 +1033,3 @@ | ||
const item = new JsDate(epochMilliseconds); | ||
const item = new Date(epochMilliseconds); | ||
const year = item.getUTCFullYear(); | ||
@@ -1106,2 +1105,28 @@ const month = item.getUTCMonth() + 1; | ||
export function formatISOString(year: i32, month: i32, day: i32, hour: i32, minute: i32, | ||
second: i32, millisecond: i32, microsecond: i32, nanosecond: i32): string { | ||
return ( | ||
year.toString() + | ||
"-" + | ||
toPaddedString(month) + | ||
"-" + | ||
toPaddedString(day) + | ||
"T" + | ||
toPaddedString(hour) + | ||
":" + | ||
toPaddedString(minute) + | ||
":" + | ||
toPaddedString(second) + | ||
(nanosecond != 0 || microsecond != 0 || millisecond != 0 | ||
? ( | ||
f64(nanosecond) / NANOS_PER_SECOND + | ||
f64(microsecond) / MICROS_PER_SECOND + | ||
f64(millisecond) / MILLIS_PER_SECOND | ||
) | ||
.toString() | ||
.substring(1) | ||
: "") | ||
); | ||
} | ||
function addInstant(epochNanoseconds: i64, h: i32, min: i32, s: i32, ms: i32, µs: i32, ns: i32): i64 { | ||
@@ -1108,0 +1133,0 @@ return epochNanoseconds + ns + µs * 1_000 + ms * 1_000_000 + s * 1_000_000_000 * min * 60_000_000_000 + h * 3_600_000_000_000; |
@@ -310,8 +310,8 @@ ## Development and roadmap | ||
Static methods | ||
- [ ] from | ||
- [ ] fromEpochSeconds | ||
- [ ] fromEpochMilliseconds | ||
- [ ] fromEpochMicroseconds | ||
- [ ] fromEpochNanoseconds | ||
- [ ] compare | ||
- [x] from | ||
- [x] fromEpochSeconds | ||
- [x] fromEpochMilliseconds | ||
- [x] fromEpochMicroseconds | ||
- [x] fromEpochNanoseconds | ||
- [x] compare | ||
@@ -327,11 +327,15 @@ Properties | ||
- [ ] toZonedDateTime | ||
- [ ] add | ||
- [ ] subtract | ||
- [ ] until | ||
- [ ] since | ||
- [x] add | ||
- [x] subtract | ||
- [x] until | ||
- [x] since | ||
- [ ] round | ||
- [ ] equals | ||
- [ ] toString | ||
- [x] equals | ||
- [x] toString | ||
- [ ] toLocaleString | ||
- [ ] toJSON | ||
- [ ] valueOf | ||
- [ ] valueOf | ||
General features | ||
- [ ] rounding and smallest unit behaviour |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "An implementation of temporal within AssemblyScript, with an initial focus on non-timezone-aware classes and functionality.", | ||
@@ -23,8 +23,8 @@ "main": "index.js", | ||
"@assemblyscript/loader": "^0.18.20", | ||
"assemblyscript": "^0.18.20", | ||
"prettier": "^2.2.1" | ||
}, | ||
"dependencies": { | ||
"assemblyscript": "^0.18.31", | ||
"assemblyscript-regex": "^1.6.3" | ||
} | ||
} |
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
509105
3
12070
2
35
+ Addedassemblyscript@^0.18.31
+ Addedassemblyscript@0.18.32(transitive)
+ Addedbinaryen@100.0.0-nightly.20210413(transitive)
+ Addedlong@4.0.0(transitive)