Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rbuckton
Last active January 14, 2019 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbuckton/86e15cb9b45bc17cedc7b1d036434f10 to your computer and use it in GitHub Desktop.
Save rbuckton/86e15cb9b45bc17cedc7b1d036434f10 to your computer and use it in GitHub Desktop.
declare type DateUnit = "year" | "month" | "day";
declare type DateDelta = "years" | "months" | "weeks" | "days";
declare type TimeUnit = "hour" | "minute" | "second" | "millisecond" | "nanosecond";
declare type TimeDelta = "hours" | "minutes" | "seconds" | "milliseconds" | "nanoseconds";
declare type InstantUnit = "milliseconds" | "nanoseconds";
declare type InstantDelta = "milliseconds" | "nanoseconds";
declare type Components<Unit extends string> = Partial<Record<Unit, number>>;
// Represents either a fixed-offset time-zone, the SYSTEM time-zone, or a regional (IANA) time-zone.
declare class Zone {
static readonly Z: Zone;
static readonly SYSTEM: Zone;
readonly ianaZone: string;
readonly timeZone: string;
getOffsetSeconds(instant: Instant): number;
getOffsetString(instant: Instant): string;
withClock(clock: Clock): ZonedClock;
withInstant(instant: Instant): ZonedDateTime;
static fromZone(zone: string): Zone;
static fromOffset(offsetSeconds: number): Zone;
}
// Represents either a fixed-time clock or a dynamic-time clock with user-defined precision.
declare class Clock {
readonly precision: TimeUnit;
currentTime(): bigint; // nanoseconds since UNIX epoch
currentInstant(): Instant;
withPrecision(precision: TimeUnit): Clock;
withZone(zone: Zone): ZonedClock;
withOffset(offsetSeconds: number): ZonedClock;
static getClock(precision?: TimeUnit): Clock;
static getFixedClock(nanoseconds: bigint): Clock;
}
// Represents a Clock with respect to a specific Zone.
declare abstract class ZonedClock {
readonly clock: Clock;
readonly zone: Zone;
constructor(clock: Clock, zone: Zone);
currentZonedDateTime(): ZonedDateTime;
currentCivilDateTime(): CivilDateTime;
currentCivilDate(): CivilDate;
currentCivilTime(): CivilTime;
withPrecision(precision: TimeUnit): ZonedClock;
withZoneSameClock(zone: Zone): ZonedClock;
withClockSameZone(clock: Clock): ZonedClock;
}
// Represents an ISO civil date
// YYYY-MM-DD
// YYYY-Www-D
// YYYY-DDD
declare class CivilDate {
readonly year: number;
readonly month: number;
readonly day: number;
readonly weekOfYear: number;
readonly dayOfWeek: number;
readonly dayOfYear: number;
constructor(year: number, month: number, day: number);
plus(deltas?: Components<DateDelta>): CivilDate;
minus(deltas?: Components<DateDelta>): CivilDate;
with(components?: Components<DateUnit>): CivilDate;
withTime(time: CivilTime): CivilDateTime;
until(other: CivilDate): Duration;
since(other: CivilDate): Duration;
equals(other: CivilDate): boolean;
toCalendarDateString(): string;
toWeekDateString(): string;
toOrdinalDateString(): string;
toString(): string;
toJSON(): string;
static fromWeekDate(year: number, weekOfYear: number, dayOfWeek: number): CivilDate;
static fromOrdinalDate(year: number, dayOfYear: number): CivilDate;
static fromString(text: string): CivilDate;
static now(clock: Clock, zone: Zone): CivilDate;
static now(clock: ZonedClock): CivilDate;
}
// Represents an ISO civil time
// hh:mm:ss.mmmnnnnnn
declare class CivilTime {
static readonly START_OF_DAY: CivilTime; // 00:00:00.000000000
static readonly END_OF_DAY: CivilTime; // 24:00:00.000000000
readonly hour: number;
readonly minute: number;
readonly second: number;
readonly millisecond: number;
readonly nanosecond: number;
constructor(hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number);
plus(deltas?: Components<TimeDelta>): CivilTime;
minus(deltas?: Components<TimeDelta>): CivilTime;
with(components?: Components<TimeUnit>): CivilTime;
withDate(date: CivilDate): CivilDateTime;
until(other: CivilTime): Duration;
since(other: CivilTime): Duration;
equals(other: CivilTime): boolean;
toString(): string;
toJSON(): string;
static fromString(text: string): CivilTime;
static now(clock: Clock, zone: Zone): CivilTime;
static now(clock: ZonedClock): CivilTime;
}
// Represents an ISO Civil date and time
// YYYY-MM-DDThh:mm:ss.mmmnnnnnn
// YYYY-Www-DThh:mm:ss.mmmnnnnnn
// YYYY-DDDThh:mm:ss.mmmnnnnnn
declare class CivilDateTime {
readonly year: number;
readonly month: number;
readonly day: number;
readonly hour: number;
readonly minute: number;
readonly second: number;
readonly millisecond: number;
readonly nanosecond: number;
readonly dayOfYear: number;
readonly dayOfWeek: number;
readonly weekOfYear: number;
constructor(year: number, month: number, day: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number);
plus(deltas?: Components<DateDelta | TimeDelta>): CivilDateTime;
minus(deltas?: Components<DateDelta | TimeDelta>): CivilDateTime;
with(components?: Components<DateUnit | TimeUnit>): CivilDateTime;
withZone(zone: Zone): ZonedDateTime;
until(other: CivilDateTime): Duration;
since(other: CivilDateTime): Duration;
toCivilDate(): CivilDate;
toCivilTime(): CivilTime;
toCalendarDateTimeString(): string;
toWeekDateTimeString(): string;
toOrdinalDateTimeString(): string;
toString(): string;
toJSON(): string;
static from(date: CivilDate, time: CivilTime): CivilDateTime;
static fromWeekDateTime(year: number, weekOfYear: number, dayOfWeek: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number): CivilDateTime;
static fromOrdinalDateTime(year: number, dayOfYear: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number): CivilDateTime;
static fromString(text: string): CivilDateTime;
static now(clock: Clock, zone: Zone): CivilDateTime;
static now(clock: ZonedClock): CivilDateTime;
}
// Represents an ISO instant based on the number of nanoseconds since the UNIX epoch.
// YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ
declare class Instant {
readonly milliseconds: number;
readonly nanoseconds: number;
constructor(ns: bigint);
plus(deltas?: Components<InstantDelta>): Instant;
minus(deltas?: Components<InstantDelta>): Instant;
with(components?: Components<InstantUnit>): Instant;
withZone(zone: Zone): ZonedDateTime;
until(other: Instant): Duration;
since(other: Instant): Duration;
equals(other: Instant): boolean;
toCivilDateTime(): CivilDateTime;
toCivilDate(): CivilDate;
toCivilTime(): CivilTime;
toString(): string;
toJSON(): string;
static fromString(text: string): Instant;
static fromMilliseconds(milliseconds: number, nanoseconds?: number): Instant;
static now(clock: Clock | ZonedClock): Instant;
}
// Represents the Date and Time of an instant within a specific time-zone.
// YYYY-MM-DDThh:mm:ss.mmmnnnnnn±hh:mm[ZZZ]
// YYYY-Www-DThh:mm:ss.mmmnnnnnn±hh:mm[ZZZ]
// YYYY-DDDThh:mm:ss.mmmnnnnnn±hh:mm[ZZZ]
declare class ZonedDateTime {
readonly instant: Instant;
readonly zone: Zone;
readonly offsetSeconds: number;
readonly ianaZone: string;
readonly timeZone: string;
readonly offsetString: string;
readonly year: number;
readonly month: number;
readonly day: number;
readonly dayOfYear: number;
readonly dayOfWeek: number;
readonly weekOfYear: number;
readonly hour: number;
readonly minute: number;
readonly second: number;
readonly millisecond: number;
readonly nanosecond: number;
constructor(instant: Instant, zone: Zone);
plus(deltas?: Components<DateDelta | TimeDelta>): ZonedDateTime;
minus(deltas?: Components<DateDelta | TimeDelta>): ZonedDateTime;
with(components?: Components<DateUnit | TimeUnit>): ZonedDateTime;
withZoneSameInstant(zone: Zone): ZonedDateTime;
withZoneSameLocal(zone: Zone): ZonedDateTime;
until(other: ZonedDateTime): Duration;
since(other: ZonedDateTime): Duration;
equals(other: ZonedDateTime): boolean;
toCivilDateTime(): CivilDateTime;
toCivilDate(): CivilDate;
toCivilTime(): CivilTime;
toZonedCalendarDateTimeString(): string;
toZonedWeekDateTimeString(): string;
toZonedOrdinalDateTimeString(): string;
toString(): string;
toJSON(): string;
static fromCalendarDateTime(zone: Zone | string, year: number, weekOfYear: number, dayOfWeek: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number): CivilDateTime;
static fromWeekDateTime(zone: Zone | string, year: number, weekOfYear: number, dayOfWeek: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number): CivilDateTime;
static fromOrdinalDateTime(zone: Zone | string, year: number, dayOfYear: number, hour: number, minute: number, second?: number, millisecond?: number, nanosecond?: number): CivilDateTime;
static fromString(text: string): ZonedDateTime;
static now(clock: Clock, zone: Zone): ZonedDateTime;
static now(clock: ZonedClock): ZonedDateTime;
}
// Represents an ISO duration
// PnnYnnMnnDTnnHnnMnnS
declare class Duration {
static readonly ZERO: Duration;
readonly years: number;
readonly months: number;
readonly days: number;
readonly hours: number;
readonly minutes: number;
readonly seconds: number;
readonly milliseconds: number;
readonly nanoseconds: number;
readonly size: number;
constructor(components?: Components<DateDelta | TimeDelta>);
plus(deltas?: Components<DateDelta | TimeDelta>): Duration;
minus(deltas?: Components<DateDelta | TimeDelta>): Duration;
scale(amount: number): Duration;
with(components?: Components<DateDelta | TimeDelta>): Duration;
negate(): Duration;
abs(): Duration;
truncate(unit: DateDelta | TimeDelta): Duration;
toTimeAgoString(): string;
toString(): string;
toJSON(): string;
static fromString(text: string): Duration;
static fromMilliseconds(milliseconds: number, nanoseconds?: number): Duration;
static fromNanoseconds(ns: bigint): Duration;
}
// Represents an ISO interval
declare abstract class Interval {
readonly repeat: number;
contains(instant: Instant | Interval): boolean;
overlaps(interval: Interval): boolean;
nextOccurence(instant: Instant): Interval | undefined;
previousOccurence(instant: Instant): Interval | undefined;
occurences(): IterableIterator<Interval>;
withStartSameEnd(start: Instant): StartEndInterval;
withStartSameDuration(start: Instant): StartDurationInterval;
withEndSameStart(end: Instant): StartEndInterval;
withEndSameDuration(end: Instant): DurationEndInterval;
withDurationSameStart(duration: Duration): StartDurationInterval;
withDurationSameEnd(duration: Duration): DurationEndInterval;
withRepetitions(repeat: number): StartEndInterval | StartDurationInterval | DurationEndInterval | DurationEndInterval;
toString(): string;
toJSON(): string;
static fromString(text: string): StartEndInterval | StartDurationInterval | DurationEndInterval | DurationInterval;
}
// YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (start/end)
// R/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (start/end)
// Rnn/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (start/end)
declare class StartEndInterval extends Interval {
readonly start: CivilDateTime | ZonedDateTime;
readonly end: CivilDateTime | ZonedDateTime;
}
// YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/PnnYnnMnnDTnnHnnMnnS (start/duration)
// R/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/PnnYnnMnnDTnnHnnMnnS (start/duration)
// Rnn/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ/PnnYnnMnnDTnnHnnMnnS (start/duration)
declare class StartDurationInterval extends Interval {
readonly start: CivilDateTime | ZonedDateTime;
readonly duration: Duration;
}
// PnnYnnMnnDTnnHnnMnnS/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (duration/end)
// R/PnnYnnMnnDTnnHnnMnnS/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (duration/end)
// Rnn/PnnYnnMnnDTnnHnnMnnS/YYYY-MM-DDThh:mm:ss.mmmnnnnnnZ (duration/end)
declare class DurationEndInterval extends Interval {
readonly duration: Duration;
readonly end: CivilDateTime | ZonedDateTime;
}
// PnnYnnMnnDTnnHnnMnnS (duration)
// R/PnnYnnMnnDTnnHnnMnnS (duration)
// Rnn/PnnYnnMnnDTnnHnnMnnS (duration)
declare class DurationInterval extends Interval {
readonly duration: Duration;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment