mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 23:07:06 +00:00
[@types/luxon] Added tests from docs.
Just went through the docs/manual and copied the examples over, plus added `$ExpectType`. The exercise did uncover a couple minor issues with local options.
This commit is contained in:
Vendored
+4
-5
@@ -244,9 +244,9 @@ export class DateTime {
|
||||
minus(duration: Duration | number | DurationObject): DateTime;
|
||||
plus(duration: Duration | number | DurationObject): DateTime;
|
||||
reconfigure(properties: LocaleOptions): DateTime;
|
||||
resolvedLocaleOpts(options?: DateTimeFormatOptions): LocaleOptions;
|
||||
resolvedLocaleOpts(options?: DateTimeFormatOptions): Intl.ResolvedDateTimeFormatOptions;
|
||||
set(values: DateObjectUnits): DateTime;
|
||||
setLocale(locale: any): DateTime;
|
||||
setLocale(locale: string): DateTime;
|
||||
setZone(zone: string | Zone, options?: ZoneOptions): DateTime;
|
||||
startOf(unit: DurationUnit): DateTime;
|
||||
toBSON(): Date;
|
||||
@@ -259,9 +259,8 @@ export class DateTime {
|
||||
toJSDate(): Date;
|
||||
toJSON(): string;
|
||||
toLocal(): DateTime;
|
||||
toLocaleParts(options?: DateTimeFormatOptions): any[];
|
||||
toLocaleString(options?: DateTimeFormatOptions): string;
|
||||
toMillis(): number;
|
||||
toLocaleParts(options?: LocaleOptions & DateTimeFormatOptions): any[];
|
||||
toLocaleString(options?: LocaleOptions & DateTimeFormatOptions): string;
|
||||
toMillis(): number;
|
||||
toObject(options?: { includeConfig?: boolean }): DateObject;
|
||||
toRelative(options?: ToRelativeOptions): string | null;
|
||||
|
||||
+184
-36
@@ -35,19 +35,30 @@ getters.daysInMonth;
|
||||
getters.ordinal;
|
||||
getters.isInLeapYear;
|
||||
|
||||
dt.toLocaleString();
|
||||
dt.toLocaleString(DateTime.DATE_MED);
|
||||
dt.toISO();
|
||||
dt.toISO({includeOffset: true});
|
||||
dt.toSeconds();
|
||||
dt.toSQL();
|
||||
dt.toSQL({includeOffset: false, includeZone: true});
|
||||
dt.toSQLDate();
|
||||
dt.toSQLTime();
|
||||
dt.toSQLTime({includeOffset: false, includeZone: true});
|
||||
|
||||
dt.toRelative();
|
||||
dt.toRelativeCalendar();
|
||||
dt.toBSON(); // $ExpectType Date
|
||||
dt.toHTTP(); // $ExpectType string
|
||||
dt.toISO(); // $ExpectType string
|
||||
dt.toISO({ includeOffset: true }); // $ExpectType string
|
||||
dt.toISODate(); // $ExpectType string
|
||||
dt.toISOTime(); // $ExpectType string
|
||||
dt.toISOWeekDate(); // $ExpectType string
|
||||
dt.toJSDate(); // $ExpectType Date
|
||||
dt.toJSON(); // $ExpectType string
|
||||
dt.toLocaleString(); // $ExpectType string
|
||||
dt.toLocaleString({ month: 'long', day: 'numeric' }); // $ExpectType string
|
||||
dt.toLocaleString(DateTime.DATE_MED); // $ExpectType string
|
||||
dt.toMillis(); // $ExpectType number
|
||||
dt.toMillis(); // $ExpectType number
|
||||
dt.toRelative(); // $ExpectType string | null
|
||||
dt.toRelativeCalendar(); // $ExpectType string | null
|
||||
dt.toRFC2822(); // $ExpectType string
|
||||
dt.toSeconds(); // $ExpectType number
|
||||
dt.toSQL(); // $ExpectType string
|
||||
dt.toSQL({ includeOffset: false, includeZone: true }); // $ExpectType string
|
||||
dt.toSQLDate(); // $ExpectType string
|
||||
dt.toSQLTime(); // $ExpectType string
|
||||
dt.toSQLTime({ includeOffset: false, includeZone: true }); // $ExpectType string
|
||||
dt.valueOf(); // $ExpectType number
|
||||
|
||||
// $ExpectType string | null
|
||||
dt.toRelative({
|
||||
@@ -73,49 +84,69 @@ dt.minus({ days: 7 });
|
||||
dt.startOf('day');
|
||||
dt.endOf('hour');
|
||||
dt.zone;
|
||||
dt.zoneName;
|
||||
dt.zoneName; // $ExpectType string
|
||||
dt.offset; // $ExpectType number
|
||||
dt.offsetNameShort; // $ExpectType string
|
||||
dt.offsetNameLong; // $ExpectType string
|
||||
dt.isOffsetFixed; // $ExpectType boolean
|
||||
dt.isInDST; // $ExpectType boolean
|
||||
|
||||
dt.set({ hour: 3 }).hour;
|
||||
dt.set({ hour: 3 }).hour; // $ExpectType number
|
||||
|
||||
const f = { month: 'long', day: 'numeric' };
|
||||
dt.setLocale('fr').toLocaleString(f);
|
||||
dt.setLocale('en-GB').toLocaleString(f);
|
||||
dt.setLocale('en-US').toLocaleString(f);
|
||||
|
||||
DateTime.fromObject({ zone: 'America/Los_Angeles' });
|
||||
DateTime.local().setZone('America/Los_Angeles');
|
||||
|
||||
DateTime.utc(2017, 5, 15);
|
||||
DateTime.utc();
|
||||
DateTime.local().toUTC();
|
||||
DateTime.utc().toLocal();
|
||||
DateTime.utc(2017, 5, 15); // $ExpectType DateTime
|
||||
DateTime.utc(); // $ExpectType DateTime
|
||||
DateTime.local().toUTC(); // $ExpectType DateTime
|
||||
DateTime.utc().toLocal(); // $ExpectType DateTime
|
||||
|
||||
DateTime.fromMillis(1527780819458).toMillis();
|
||||
DateTime.max(dt, now); // $ExpectType DateTime
|
||||
DateTime.min(dt, now); // $ExpectType DateTime
|
||||
|
||||
const {input, result, zone} = DateTime.fromFormatExplain('Aug 6 1982', 'MMMM d yyyy');
|
||||
const anything: any = 0;
|
||||
if (DateTime.isDateTime(anything)) {
|
||||
anything; // $ExpectType DateTime
|
||||
}
|
||||
|
||||
const { input, result, zone } = DateTime.fromFormatExplain('Aug 6 1982', 'MMMM d yyyy');
|
||||
|
||||
/* Duration */
|
||||
const dur = Duration.fromObject({ hours: 2, minutes: 7 });
|
||||
dt.plus(dur);
|
||||
dur.hours;
|
||||
dur.minutes;
|
||||
dur.seconds;
|
||||
dt.plus(dur); // $ExpectType DateTime
|
||||
dur.hours; // $ExpectType number
|
||||
dur.minutes; // $ExpectType number
|
||||
dur.seconds; // $ExpectType number
|
||||
|
||||
dur.as('seconds');
|
||||
dur.as('seconds'); // $ExpectType number
|
||||
dur.toObject();
|
||||
dur.toISO();
|
||||
dur.toISO(); // $ExpectType string
|
||||
dur.normalize(); // $ExpectType Duration
|
||||
|
||||
if (Duration.isDuration(anything)) {
|
||||
anything; // $ExpectType Duration
|
||||
}
|
||||
|
||||
/* Interval */
|
||||
const later = DateTime.local();
|
||||
const i = Interval.fromDateTimes(now, later);
|
||||
i.length();
|
||||
i.length('years');
|
||||
i.contains(DateTime.local(2019));
|
||||
i.set({end: DateTime.local(2020)});
|
||||
i.mapEndpoints((d) => d);
|
||||
i.length(); // $ExpectType number
|
||||
i.length('years'); // $ExpectType number
|
||||
i.contains(DateTime.local(2019)); // $ExpectType boolean
|
||||
i.set({end: DateTime.local(2020)}); // $ExpectType Interval
|
||||
i.mapEndpoints((d) => d); // $ExpectType Interval
|
||||
|
||||
i.toISO();
|
||||
i.toString();
|
||||
i.toISO(); // $ExpectType string
|
||||
i.toString(); // $ExpectType string
|
||||
i.toDuration('months'); // $ExpectType Duration
|
||||
|
||||
if (Interval.isInterval(anything)) {
|
||||
anything; // $ExpectType Interval
|
||||
}
|
||||
|
||||
/* Info */
|
||||
Info.months();
|
||||
@@ -128,6 +159,7 @@ Info.features().zones;
|
||||
Settings.defaultLocale;
|
||||
Settings.defaultLocale = 'en';
|
||||
Settings.defaultZoneName = 'Europe/Paris';
|
||||
Settings.throwOnInvalid = true;
|
||||
Settings.now();
|
||||
Settings.now = () => 0;
|
||||
Settings.resetCaches();
|
||||
@@ -135,4 +167,120 @@ Settings.resetCaches();
|
||||
// $ExpectError
|
||||
Settings.defaultZone = Settings.defaultZone;
|
||||
|
||||
/* Zone */
|
||||
// The following tests were coped from the docs
|
||||
// http://moment.github.io/luxon/docs/manual/
|
||||
|
||||
/* Intl */
|
||||
DateTime.local().setLocale("el").toLocaleString(DateTime.DATE_FULL); // $ExpectType string
|
||||
dt.locale; // $ExpectType string
|
||||
DateTime.local().setLocale("fr").locale; // $ExpectType string
|
||||
DateTime.local().reconfigure({ locale: "fr" }).locale; // $ExpectType string
|
||||
|
||||
Settings.defaultLocale = "fr";
|
||||
DateTime.local().locale; // $ExpectType string
|
||||
|
||||
Settings.defaultLocale = DateTime.local().resolvedLocaleOpts().locale;
|
||||
|
||||
dt.setLocale("fr").toLocaleString(DateTime.DATE_FULL); // $ExpectType string
|
||||
dt.toLocaleString({ locale: "es", ...DateTime.DATE_FULL }); // $ExpectType string
|
||||
dt.setLocale("fr").toFormat("MMMM dd, yyyy GG"); // $ExpectType string
|
||||
|
||||
DateTime.fromFormat("septembre 25, 2017 après Jésus-Christ", "MMMM dd, yyyy GG", { locale: "fr" });
|
||||
|
||||
Info.months("long", { locale: "fr" }); // $ExpectType string[]
|
||||
Info.weekdays("long", { locale: "fr" }); // $ExpectType string[]
|
||||
Info.eras("long", { locale: "fr" }); // $ExpectType string[]
|
||||
|
||||
DateTime.local().reconfigure({ locale: "it", numberingSystem: "beng" });
|
||||
Settings.defaultNumberingSystem = "beng";
|
||||
|
||||
/* Time zones and offsets */
|
||||
Info.features().zones; // $ExpectType boolean
|
||||
|
||||
const bogus = DateTime.local().setZone("America/Bogus");
|
||||
bogus.isValid; // $ExpectType boolean
|
||||
bogus.invalidReason; // $ExpectType string | null
|
||||
bogus.invalidExplanation; // $ExpectType string | null
|
||||
|
||||
const local = DateTime.local(2017, 5, 15, 09, 10, 23);
|
||||
local.zoneName; // $ExpectType string
|
||||
local.toString(); // $ExpectType string
|
||||
local.setZone("America/Los_Angeles"); // $ExpectType DateTime
|
||||
local.setZone("America/Los_Angeles", { keepLocalTime: true }); // $ExpectType DateTime
|
||||
|
||||
const iso = DateTime.fromISO("2017-05-15T09:10:23");
|
||||
iso.zoneName; // $ExpectType string
|
||||
iso.toString(); // $ExpectType string
|
||||
|
||||
DateTime.fromISO("2017-05-15T09:10:23", { zone: "Europe/Paris", setZone: true }); // $ExpectType DateTime
|
||||
DateTime.fromFormat("2017-05-15T09:10:23 Europe/Paris", "yyyy-MM-dd'T'HH:mm:ss z"); // $ExpectType DateTime
|
||||
|
||||
Settings.defaultZoneName = "Asia/Tokyo";
|
||||
|
||||
/* Calendars */
|
||||
DateTime.fromISO('2017-W23-3').plus({ weeks: 1, days: 2 }).toISOWeekDate(); // $ExpectType string
|
||||
|
||||
const dtHebrew = DateTime.local().reconfigure({ outputCalendar: 'hebrew' });
|
||||
dtHebrew.outputCalendar; // $ExpectType string
|
||||
dtHebrew.toLocaleString(); // $ExpectType string
|
||||
|
||||
DateTime.fromObject({ outputCalendar: 'buddhist' }).toLocaleString(DateTime.DATE_FULL);
|
||||
Settings.defaultOutputCalendar = 'persian';
|
||||
|
||||
/* Formatting */
|
||||
DateTime.fromISO('2014-08-06T13:07:04.054').toFormat('yyyy LLL dd'); // $ExpectType string
|
||||
|
||||
/* Parsing */
|
||||
DateTime.fromObject({ zone: 'America/Los_Angeles' }); // $ExpectType DateTime
|
||||
DateTime.fromISO('2016-05-25'); // $ExpectType DateTime
|
||||
DateTime.fromJSDate(new Date()); // $ExpectType DateTime
|
||||
DateTime.fromRFC2822('Tue, 01 Nov 2016 13:23:12 +0630'); // $ExpectType DateTime
|
||||
DateTime.fromHTTP('Sunday, 06-Nov-94 08:49:37 GMT'); // $ExpectType DateTime
|
||||
DateTime.fromSQL('2017-05-15 09:24:15'); // $ExpectType DateTime
|
||||
DateTime.fromMillis(1542674993410); // $ExpectType DateTime
|
||||
DateTime.fromSeconds(1542674993); // $ExpectType DateTime
|
||||
DateTime.fromFormat('May 25 1982', 'LLLL dd yyyy'); // $ExpectType DateTime
|
||||
DateTime.fromFormat('mai 25 1982', 'LLLL dd yyyy', { locale: 'fr' }); // $ExpectType DateTime
|
||||
|
||||
DateTime.fromFormatExplain("Aug 6 1982", "MMMM d yyyy").regex;
|
||||
|
||||
/* Math */
|
||||
const d1: DateTime = DateTime.local(2017, 2, 13).plus({ days: 30 });
|
||||
const d2: DateTime = DateTime.fromISO('2017-04-30').plus({days: 1}).plus({months: 1});
|
||||
|
||||
if (d1 < d2 || +d1 === +d2) {
|
||||
//
|
||||
}
|
||||
|
||||
d1.hasSame(d2, 'millisecond'); // $ExpectType boolean
|
||||
d1.hasSame(d2, 'minute'); // $ExpectType boolean
|
||||
d1.hasSame(d2, 'year'); // $ExpectType boolean
|
||||
|
||||
dur.toObject().days; // $ExpectType number | undefined
|
||||
dur.as('minutes'); // $ExpectType number
|
||||
dur.shiftTo('minutes').toObject().minutes; // $ExpectType number | undefined
|
||||
DateTime.fromISO("2017-05-15").plus(dur).toISO(); // $ExpectType string
|
||||
|
||||
const end = DateTime.fromISO('2017-03-13');
|
||||
const start = DateTime.fromISO('2017-02-13');
|
||||
|
||||
const diffInMonths = end.diff(start, 'months');
|
||||
diffInMonths.toObject().months; // $ExpectType number | undefined
|
||||
|
||||
const diff = end.diff(start);
|
||||
diff.toObject().milliseconds; // $ExpectType number | undefined
|
||||
end.diff(start, ['months', 'days']).months; // $ExpectType number
|
||||
end.diffNow(['months', 'days']); // $ExpectType Duration
|
||||
|
||||
dur.as('days'); // $ExpectType number
|
||||
dur.shiftTo('days').toObject().days; // $ExpectType number | undefined
|
||||
dur.shiftTo('weeks', 'hours').toObject().weeks; // $ExpectType number | undefined
|
||||
DateTime.local().plus(dur.shiftTo('milliseconds')).year; // $ExpectType number
|
||||
|
||||
Duration.fromISO('PY23', { conversionAccuracy: 'longterm' }); // $ExpectType Duration
|
||||
end.diff(start, 'hours', { conversionAccuracy: 'longterm' }); // $ExpectType Duration
|
||||
end.diff(start, ['months', 'days', 'hours']); // $ExpectType Duration
|
||||
dur.reconfigure({ conversionAccuracy: 'longterm' }); // $ExpectType Duration
|
||||
|
||||
start.until(end); // $ExpectType Interval
|
||||
i.toDuration(['years', 'months', 'days']); // $ExpectType Duration
|
||||
|
||||
Reference in New Issue
Block a user