assemblyscript-temporal
Advanced tools
Comparing version 1.4.0 to 1.5.0
@@ -1,2 +0,2 @@ | ||
import { RegExp } from "../node_modules/assemblyscript-regex/assembly/index"; | ||
import { RegExp } from "assemblyscript-regex"; | ||
@@ -12,2 +12,3 @@ import { Duration, DurationLike } from "./duration"; | ||
daysInMonth, | ||
daysInYear, | ||
balanceDuration, | ||
@@ -115,3 +116,3 @@ toPaddedString, | ||
get daysInYear(): i32 { | ||
return 365 + i32(leapYear(this.year)); | ||
return daysInYear(this.year); | ||
} | ||
@@ -222,3 +223,3 @@ | ||
subtract<T = DurationLike>(durationToSubtract: T): PlainDate { | ||
subtract<T = DurationLike>(durationToSubtract: T, overflow: Overflow = Overflow.Constrain): PlainDate { | ||
const duration = | ||
@@ -249,3 +250,3 @@ durationToSubtract instanceof DurationLike | ||
-balancedDuration.days, | ||
Overflow.Constrain | ||
overflow | ||
); | ||
@@ -252,0 +253,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { RegExp } from "../node_modules/assemblyscript-regex/assembly/index"; | ||
import { RegExp } from "assemblyscript-regex"; | ||
@@ -10,5 +10,5 @@ import { Duration, DurationLike } from "./duration"; | ||
dayOfYear, | ||
leapYear, | ||
weekOfYear, | ||
daysInMonth, | ||
daysInYear, | ||
toPaddedString, | ||
@@ -85,3 +85,3 @@ coalesce, | ||
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})*))\\])?$", | ||
"^((?:[+\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" | ||
@@ -153,3 +153,3 @@ ); | ||
get daysInYear(): i32 { | ||
return 365 + i32(leapYear(this.year)); | ||
return daysInYear(this.year); | ||
} | ||
@@ -156,0 +156,0 @@ |
@@ -0,1 +1,6 @@ | ||
import { Duration, DurationLike } from "./duration"; | ||
import { Overflow, TimeComponent } from "./enums"; | ||
import { RegExp } from "assemblyscript-regex"; | ||
import { PlainDateTime } from "./plaindatetime"; | ||
import { DateLike } from "./plaindate"; | ||
import { | ||
@@ -12,7 +17,3 @@ sign, | ||
} 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 { | ||
@@ -19,0 +20,0 @@ hour: i32 = -1; |
@@ -121,2 +121,8 @@ // for the proposal-temporal implementation, most of the business logic | ||
// @ts-ignore: decorator | ||
@inline | ||
export function daysInYear(year: i32): i32 { | ||
return 365 + i32(leapYear(year)); | ||
} | ||
// Original: Disparate variation | ||
@@ -151,9 +157,9 @@ // Modified: TomohikoSakamoto algorithm from https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week | ||
let daysInYear = 0; | ||
let daysPerYear = 0; | ||
let testYear = month > 2 ? year : year - 1; | ||
while (((daysInYear = 365 + i32(leapYear(testYear))), day < -daysInYear)) { | ||
while (((daysPerYear = daysInYear(testYear)), day < -daysPerYear)) { | ||
year -= 1; | ||
testYear -= 1; | ||
day += daysInYear; | ||
day += daysPerYear; | ||
} | ||
@@ -163,6 +169,6 @@ | ||
while (((daysInYear = 365 + i32(leapYear(testYear))), day > daysInYear)) { | ||
while (((daysPerYear = daysInYear(testYear)), day > daysPerYear)) { | ||
year += 1; | ||
testYear += 1; | ||
day -= daysInYear; | ||
day -= daysPerYear; | ||
} | ||
@@ -331,3 +337,3 @@ | ||
if (week === 53) { | ||
if (365 + i32(leapYear(year)) - doy < 4 - dow) { | ||
if (daysInYear(year) - doy < 4 - dow) { | ||
return 1; | ||
@@ -414,9 +420,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; | ||
let | ||
nanosecondsI64: i64 = 0, | ||
microsecondsI64: i64 = 0, | ||
millisecondsI64: i64 = 0, | ||
secondsI64: i64 = 0, | ||
minutesI64: i64 = 0, | ||
hoursI64: i64 = 0, | ||
daysI64: i64 = 0; | ||
@@ -427,5 +434,5 @@ if ( | ||
) { | ||
const _ES$NanosecondsToDays = nanosecondsToDays(durationNs); | ||
daysI64 = _ES$NanosecondsToDays.days; | ||
nanosecondsI64 = _ES$NanosecondsToDays.nanoseconds; | ||
const nanoDays = nanosecondsToDays(durationNs); | ||
daysI64 = nanoDays.days; | ||
nanosecondsI64 = nanoDays.nanoseconds; | ||
} else { | ||
@@ -517,7 +524,10 @@ daysI64 = 0; | ||
export function compareTemporalDate(y1: i32, m1: i32, d1: i32, y2: i32, m2: i32, d2: i32): i32 { | ||
let res = y1 - y2; | ||
export function compareTemporalDate( | ||
yr1: i32, mo1: i32, d1: i32, | ||
yr2: i32, mo2: i32, d2: i32 | ||
): i32 { | ||
let res = yr1 - yr2; | ||
if (res) return sign(res); | ||
res = m1 - m2; | ||
res = mo1 - mo2; | ||
if (res) return sign(res); | ||
@@ -529,10 +539,10 @@ | ||
export function compareTemporalDateTime( | ||
y1: i32, m1: i32, d1: i32, h1: i32, min1: i32, s1: i32, mill1: i32, mic1: i32, nan1: i32, | ||
y2: i32, m2: i32, d2: i32, h2: i32, min2: i32, s2: i32, mill2: i32, mic2: i32, nan2: i32 | ||
yr1: i32, mo1: i32, d1: i32, h1: i32, m1: i32, s1: i32, ms1: i32, µs1: i32, ns1: i32, | ||
yr2: i32, mo2: i32, d2: i32, h2: i32, m2: i32, s2: i32, ms2: i32, µs2: i32, ns2: i32 | ||
): i32 { | ||
let res = y1 - y2; | ||
let res = yr1 - yr2; | ||
if (res) return sign(res); | ||
res = m1 - m2; | ||
res = mo1 - mo2; | ||
if (res) return sign(res); | ||
@@ -546,3 +556,3 @@ | ||
res = min1 - min2; | ||
res = m1 - m2; | ||
if (res) return sign(res); | ||
@@ -553,9 +563,9 @@ | ||
res = mill1 - mill2; | ||
res = ms1 - ms2; | ||
if (res) return sign(res); | ||
res = mic1 - mic2; | ||
res = µs1 - µs2; | ||
if (res) return sign(res); | ||
res = nan1 - nan2; | ||
res = ns1 - ns2; | ||
if (res) return sign(res); | ||
@@ -567,8 +577,4 @@ | ||
export function differenceDate( | ||
y1: i32, | ||
m1: i32, | ||
d1: i32, | ||
y2: i32, | ||
m2: i32, | ||
d2: i32, | ||
yr1: i32, mo1: i32, d1: i32, | ||
yr2: i32, mo2: i32, d2: i32, | ||
largestUnit: TimeComponent = TimeComponent.Days | ||
@@ -579,15 +585,15 @@ ): Duration { | ||
case TimeComponent.Months: { | ||
let sign = -compareTemporalDate(y1, m1, d1, y2, m2, d2); | ||
let sign = -compareTemporalDate(yr1, mo1, d1, yr2, mo2, d2); | ||
if (sign == 0) return new Duration(); | ||
let startYear = y1; | ||
let startMonth = m1; | ||
let startYear = yr1; | ||
let startMonth = mo1; | ||
let endYear = y2; | ||
let endMonth = m2; | ||
let endYear = yr2; | ||
let endMonth = mo2; | ||
let endDay = d2; | ||
let years = endYear - startYear; | ||
let mid = addDate(y1, m1, d1, years, 0, 0, 0, Overflow.Constrain); | ||
let midSign = -compareTemporalDate(mid.year, mid.month, mid.day, y2, m2, d2); | ||
let mid = addDate(yr1, mo1, d1, years, 0, 0, 0, Overflow.Constrain); | ||
let midSign = -compareTemporalDate(mid.year, mid.month, mid.day, yr2, mo2, d2); | ||
@@ -607,4 +613,4 @@ if (midSign === 0) { | ||
mid = addDate(y1, m1, d1, years, months, 0, 0, Overflow.Constrain); | ||
midSign = compareTemporalDate(mid.year, mid.month, mid.day, y2, m2, d2); | ||
mid = addDate(yr1, mo1, d1, years, months, 0, 0, Overflow.Constrain); | ||
midSign = compareTemporalDate(mid.year, mid.month, mid.day, yr2, mo2, d2); | ||
@@ -627,4 +633,4 @@ if (midSign === 0) { | ||
mid = addDate(y1, m1, d1, years, months, 0, 0, Overflow.Constrain); | ||
midSign = compareTemporalDate(y1, m1, d1, mid.year, mid.month, mid.day); | ||
mid = addDate(yr1, mo1, d1, years, months, 0, 0, Overflow.Constrain); | ||
midSign = compareTemporalDate(yr1, mo1, d1, mid.year, mid.month, mid.day); | ||
} | ||
@@ -662,10 +668,10 @@ | ||
case TimeComponent.Days: { | ||
let neg = compareTemporalDate(y1, m1, d1, y2, m2, d2) < 0; | ||
let neg = compareTemporalDate(yr1, mo1, d1, yr2, mo2, d2) < 0; | ||
let smallerYear = neg ? y1 : y2; | ||
let smallerMonth = neg ? m1 : m2; | ||
let smallerYear = neg ? yr1 : yr2; | ||
let smallerMonth = neg ? mo1 : mo2; | ||
let smallerDay = neg ? d1 : d2; | ||
let largerYear = neg ? y2 : y1; | ||
let largerMonth = neg ? m2 : m1; | ||
let largerYear = neg ? yr2 : yr1; | ||
let largerMonth = neg ? mo2 : mo1; | ||
let largerDay = neg ? d2 : d1; | ||
@@ -706,17 +712,7 @@ | ||
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 | ||
h1: i32, m1: i32, s1: i32, ms1: i32, µs1: i32, ns1: i32, | ||
h2: i32, m2: i32, s2: i32, ms2: i32, µs2: i32, ns2: i32 | ||
): Duration { | ||
let hours = h2 - h1; | ||
let minutes = min2 - min1; | ||
let minutes = m2 - m1; | ||
let seconds = s2 - s1; | ||
@@ -728,6 +724,6 @@ let milliseconds = ms2 - ms1; | ||
const sign = durationSign( | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
hours, | ||
@@ -747,3 +743,5 @@ minutes, | ||
let balancedTime = balanceTime(hours, minutes, seconds, milliseconds, microseconds, nanoseconds); | ||
let balancedTime = balanceTime( | ||
hours, minutes, seconds, milliseconds, microseconds, nanoseconds | ||
); | ||
@@ -986,1 +984,13 @@ return new Duration( | ||
} | ||
export function isoYearString(year: i32): string { | ||
let yearString: string; | ||
if (year < 1000 || year > 9999) { | ||
let sign = year < 0 ? '-' : '+'; | ||
let yearNumber = abs(year); | ||
yearString = sign + `000000${yearNumber}`.slice(-6); | ||
} else { | ||
yearString = year.toString(); | ||
} | ||
return yearString; | ||
} |
@@ -10,3 +10,2 @@ ### Implementation approach | ||
### Implementation progress | ||
@@ -69,2 +68,122 @@ | ||
#### PlainTime | ||
PlainTime is currently being implemented based on the ISO 8601 calendar. | ||
Constructor | ||
- [x] new PlainTime | ||
Static methods | ||
- [x] from | ||
- [x] compare | ||
Properties | ||
- [x] hour | ||
- [x] minute | ||
- [x] second | ||
- [x] millisecond | ||
- [x] microsecond | ||
- [x] nanosecond | ||
- [ ] calendar | ||
Methods | ||
- [x] with | ||
- [x] add | ||
- [x] subtract | ||
- [x] until | ||
- [x] since | ||
- [ ] round | ||
- [x] equals | ||
- [x] toString | ||
- [ ] toLocaleString | ||
- [ ] toJSON | ||
- [ ] valueOf | ||
- [ ] toZonedDateTime | ||
- [x] toPlainDateTime | ||
- [ ] getISOFields | ||
General features | ||
- [x] overflow modes (current implementation defaults to constrain) | ||
- [ ] non ISO 8601 calendars | ||
#### PlainMonthDay | ||
PlainMonthDay is currently being implemented based on the ISO 8601 calendar. | ||
Constructor | ||
- [x] new PlainMonthDay | ||
Static methods | ||
- [x] from | ||
Properties | ||
- [x] monthCode | ||
- [x] day | ||
- [ ] calendar | ||
Methods | ||
- [x] with | ||
- [x] equals | ||
- [x] toString | ||
- [ ] toLocaleString | ||
- [ ] toJSON | ||
- [ ] valueOf | ||
- [x] toPlainDate | ||
- [ ] getISOFields | ||
#### PlainYearMonth | ||
PlainYearMonth is currently being implemented based on the ISO 8601 calendar. | ||
Constructor | ||
- [x] new Temporal.PlainYearMonth | ||
Static methods | ||
- [x] from | ||
- [x] compare | ||
Properties | ||
- [x] year | ||
- [x] month | ||
- [x] monthCode | ||
- [ ] calendar | ||
- [ ] era | ||
- [ ] eraYear | ||
- [x] daysInMonth | ||
- [x] daysInYear | ||
- [x] monthsInYear | ||
- [x] inLeapYear | ||
Methods | ||
- [x] with | ||
- [x] add | ||
- [x] subtract | ||
- [x] until | ||
- [x] since | ||
- [x] equals | ||
- [x] toString | ||
- [ ] toLocaleString | ||
- [ ] toJSON | ||
- [ ] valueOf | ||
- [x] toPlainDate | ||
- [ ] getISOFields | ||
General features | ||
- [x] overflow modes (current implementation defaults to constrain) | ||
- [ ] non ISO 8601 calendars | ||
#### PlainDateTime | ||
@@ -71,0 +190,0 @@ |
{ | ||
"name": "assemblyscript-temporal", | ||
"version": "1.4.0", | ||
"version": "1.5.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", |
@@ -66,2 +66,32 @@ ## assemblyscript-temporal | ||
#### `PlainTime` | ||
A `PlainTime` object represents a wall-clock time that is not associated with a particular date or time zone, e.g. 7:39 PM. For detailed documentation see the [TC39 Temporal proposal website](https://tc39.es/proposal-temporal/docs/plaintime.html), this implementation follows the specification as closely as possible. | ||
The `PlainTime` API is almost identical to `PlainDateTime`, so see above for API usage examples. | ||
#### `PlainMonthDay` | ||
A date without a year component. This is useful to express things like "Bastille Day is on the 14th of July". | ||
For detailed documentation see the | ||
[TC39 Temporal proposal website](https://tc39.es/proposal-temporal/docs/plainmonthday.html) | ||
, this implementation follows the specification as closely as possible. | ||
```javascript | ||
const monthDay = PlainMonthDay.from({ month: 7, day: 14 }); // => 07-14 | ||
const date = monthDay.toPlainDate({ year: 2030 }); // => 2030-07-14 | ||
date.dayOfWeek; // => 7 | ||
``` | ||
The `PlainMonthDay` API is almost identical to `PlainDateTime`, so see above for more API usage examples. | ||
#### `PlainYearMonth` | ||
A date without a day component. This is useful to express things like "the October 2020 meeting". | ||
For detailed documentation see the | ||
[TC39 Temporal proposal website](https://tc39.es/proposal-temporal/docs/plainyearmonth.html) | ||
, this implementation follows the specification as closely as possible. | ||
The `PlainYearMonth` API is almost identical to `PlainDateTime`, so see above for API usage examples. | ||
#### `now` | ||
@@ -76,5 +106,4 @@ | ||
## Contributing | ||
This project is open source, MIT licenced and your contributions are very much welcomed. | ||
This project is open source, MIT licensed and your contributions are very much welcomed. |
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
350319
31
8763
108