Skip to content

Commit

Permalink
Add DateTime.parseFormatForOpts
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Jul 9, 2022
1 parent 857812f commit 4017a8d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
import { normalizeZone } from "./impl/zoneUtil.js";
import diff from "./impl/diff.js";
import { parseRFC2822Date, parseISODate, parseHTTPDate, parseSQL } from "./impl/regexParser.js";
import { parseFromTokens, explainFromTokens } from "./impl/tokenParser.js";
import { parseFromTokens, explainFromTokens, formatOptsToTokens } from "./impl/tokenParser.js";
import {
gregorianToWeek,
weekToGregorian,
Expand Down Expand Up @@ -889,7 +889,7 @@ export default class DateTime {

/**
* Create an invalid DateTime.
* @param {string} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent
* @param {DateTime} reason - simple string of why this DateTime is invalid. Should not contain parameters or anything else data-dependent
* @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information
* @return {DateTime}
*/
Expand All @@ -916,6 +916,17 @@ export default class DateTime {
return (o && o.isLuxonDateTime) || false;
}

/**
* Produce the format string for a set of options
* @param formatOpts
* @param localeOpts
* @returns {string}
*/
static parseFormatForOpts(formatOpts, localeOpts = {}) {
const tokenList = formatOptsToTokens(formatOpts, Locale.fromObject(localeOpts));
return !tokenList ? null : tokenList.map((t) => (t ? t.val : null)).join("");
}

// INFO

/**
Expand Down
26 changes: 16 additions & 10 deletions src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ const partTypeStyleToTokenVal = {
numeric: "s",
"2-digit": "ss",
},
timeZoneName: {
long: "ZZZZZ",
short: "ZZZ",
},
};

function tokenForPart(part, locale, formatOpts) {
Expand Down Expand Up @@ -379,17 +383,9 @@ function maybeExpandMacroToken(token, locale) {
}

const formatOpts = Formatter.macroTokenToFormatOpts(token.val);
const tokens = formatOptsToTokens(formatOpts, locale);

if (!formatOpts) {
return token;
}

const formatter = Formatter.create(locale, formatOpts);
const parts = formatter.formatDateTimeParts(getDummyDateTime());

const tokens = parts.map((p) => tokenForPart(p, locale, formatOpts));

if (tokens.includes(undefined)) {
if (tokens == null || tokens.includes(undefined)) {
return token;
}

Expand Down Expand Up @@ -431,3 +427,13 @@ export function parseFromTokens(locale, input, format) {
const { result, zone, specificOffset, invalidReason } = explainFromTokens(locale, input, format);
return [result, zone, specificOffset, invalidReason];
}

export function formatOptsToTokens(formatOpts, locale) {
if (!formatOpts) {
return null;
}

const formatter = Formatter.create(locale, formatOpts);
const parts = formatter.formatDateTimeParts(getDummyDateTime());
return parts.map((p) => tokenForPart(p, locale, formatOpts));
}
2 changes: 1 addition & 1 deletion tasks/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ async function cjsBrowser() {
async function es6() {
await buildLibrary("es6", {
format: "es",
compile: false,
minify: true,
compile: false,
});
}

Expand Down
22 changes: 18 additions & 4 deletions test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,8 @@ test("DateTime.fromFormatExplain() takes the same options as fromFormat", () =>
// .fromStringExplain
//-------
test("DateTime.fromStringExplain is an alias for DateTime.fromFormatExplain", () => {
const ff = DateTime.fromFormatExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"),
fs = DateTime.fromStringExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS");
const ff = DateTime.fromStringExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"),
fs = DateTime.fromFormatExplain("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS");

expect(ff).toEqual(fs);
});
Expand All @@ -1122,8 +1122,22 @@ test("DateTime.fromStringExplain is an alias for DateTime.fromFormatExplain", ()
//-------

test("DateTime.fromString is an alias for DateTime.fromFormat", () => {
const ff = DateTime.fromFormat("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"),
fs = DateTime.fromString("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS");
const ff = DateTime.fromString("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS"),
fs = DateTime.fromFormat("1982/05/25 09:10:11.445", "yyyy/MM/dd HH:mm:ss.SSS");

expect(ff).toEqual(fs);
});

//------
// .parseFormatForOpts
//-------

test("DateTime.parseFormatForOpts returns a parsing format", () => {
const format = DateTime.parseFormatForOpts(DateTime.DATETIME_FULL);
expect(format).toEqual("MMMM d, yyyyy, h:m a ZZZ");
});

test("DateTime.parseFormatForOpts returns a parsing format", () => {
const format = DateTime.parseFormatForOpts("");
expect(format).toBeNull();
});

0 comments on commit 4017a8d

Please sign in to comment.