Skip to content

Commit

Permalink
beef up English default for toLocaleString
Browse files Browse the repository at this point in the history
  • Loading branch information
icambron committed Oct 30, 2017
1 parent b3fa5ea commit 043fef1
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 186 deletions.
156 changes: 22 additions & 134 deletions src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Util } from './impl/util';
import { RegexParser } from './impl/regexParser';
import { TokenParser } from './impl/tokenParser';
import { Conversions } from './impl/conversions';
import { Formats } from './impl/formats';
import {
InvalidArgumentError,
ConflictingSpecificationError,
Expand Down Expand Up @@ -1137,7 +1138,7 @@ export class DateTime {
* @example DateTime.local().toLocaleString({hour: '2-digit', minute: '2-digit'}); //=> '11:32'
* @return {string}
*/
toLocaleString(opts = {}) {
toLocaleString(opts = Formats.DATE_SHORT) {
return this.isValid
? Formatter.create(this.loc.clone(opts), opts).formatDateTime(this)
: INVALID;
Expand Down Expand Up @@ -1480,252 +1481,139 @@ export class DateTime {
* {@link toLocaleString} format like 10/14/1983
*/
static get DATE_SHORT() {
return {
year: 'numeric',
month: 'numeric',
day: 'numeric'
};
return Formats.DATE_SHORT;
}

/**
* {@link toLocaleString} format like 'Oct 14, 1983'
*/
static get DATE_MED() {
return {
year: 'numeric',
month: 'short',
day: 'numeric'
};
return Formats.DATE_MED;
}

/**
* {@link toLocaleString} format like 'October 14, 1983'
*/
static get DATE_FULL() {
return {
year: 'numeric',
month: 'long',
day: 'numeric'
};
return Formats.DATE_FULL;
}

/**
* {@link toLocaleString} format like 'Tuesday, October 14, 1983'
*/
static get DATE_HUGE() {
return {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long'
};
return Formats.DATE_HUGE;
}

/**
* {@link toLocaleString} format like '09:30 AM'. Only 12-hour if the locale is.
*/
static get TIME_SIMPLE() {
return {
hour: 'numeric',
minute: '2-digit'
};
return Formats.TIME_SIMPLE;
}

/**
* {@link toLocaleString} format like '09:30:23 AM'. Only 12-hour if the locale is.
*/
static get TIME_WITH_SECONDS() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
};
return Formats.TIME_WITH_SECONDS;
}

/**
* {@link toLocaleString} format like '09:30:23 AM EDT'. Only 12-hour if the locale is.
*/
static get TIME_WITH_SHORT_OFFSET() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
};
return Formats.TIME_WITH_SHORT_OFFSET;
}

/**
* {@link toLocaleString} format like '09:30:23 AM Eastern Daylight Time'. Only 12-hour if the locale is.
*/
static get TIME_WITH_LONG_OFFSET() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'long'
};
return Formats.TIME_WITH_LONG_OFFSET;
}

/**
* {@link toLocaleString} format like '09:30', always 24-hour.
*/
static get TIME_24_SIMPLE() {
return {
hour: 'numeric',
minute: '2-digit',
hour12: false
};
return Formats.TIME_24_SIMPLE;
}

/**
* {@link toLocaleString} format like '09:30:23', always 24-hour.
*/
static get TIME_24_WITH_SECONDS() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hour12: false
};
return Formats.TIME_24_WITH_SECONDS;
}

/**
* {@link toLocaleString} format like '09:30:23 EDT', always 24-hour.
*/
static get TIME_24_WITH_SHORT_OFFSET() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZoneName: 'short'
};
return Formats.TIME_24_WITH_SHORT_OFFSET;
}

/**
* {@link toLocaleString} format like '09:30:23 Eastern Daylight Time', always 24-hour.
*/
static get TIME_24_WITH_LONG_OFFSET() {
return {
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
hour12: false,
timeZoneName: 'long'
};
return Formats.TIME_24_WITH_LONG_OFFSET;
}

/**
* {@link toLocaleString} format like '10/14/1983, 9:30 AM'. Only 12-hour if the locale is.
*/
static get DATETIME_SHORT() {
return {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: '2-digit'
};
return Formats.DATETIME_SHORT;
}

/**
* {@link toLocaleString} format like '10/14/1983, 9:30:33 AM'. Only 12-hour if the locale is.
*/
static get DATETIME_SHORT_WITH_SECONDS() {
return {
year: 'numeric',
month: 'numeric',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
};
return Formats.DATETIME_SHORT_WITH_SECONDS;
}

/**
* {@link toLocaleString} format like 'Oct 14, 1983, 9:30 AM'. Only 12-hour if the locale is.
*/
static get DATETIME_MED() {
return {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit'
};
return Formats.DATETIME_MED;
}

/**
* {@link toLocaleString} format like 'Oct 14, 1983, 9:30:33 AM'. Only 12-hour if the locale is.
*/
static get DATETIME_MED_WITH_SECONDS() {
return {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
second: '2-digit'
};
return Formats.DATETIME_MED_WITH_SECONDS;
}

/**
* {@link toLocaleString} format like 'October 14, 1983, 9:30 AM EDT'. Only 12-hour if the locale is.
*/
static get DATETIME_FULL() {
return {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'short'
};
return Formats.DATETIME_FULL;
}

/**
* {@link toLocaleString} format like 'October 14, 1983, 9:303 AM EDT'. Only 12-hour if the locale is.
*/
static get DATETIME_FULL_WITH_SECONDS() {
return {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
};
return Formats.DATETIME_FULL_WITH_SECONDS;
}

/**
* {@link toLocaleString} format like 'Friday, October 14, 1983, 9:30 AM Eastern Daylight Time'. Only 12-hour if the locale is.
*/
static get DATETIME_HUGE() {
return {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: '2-digit',
timeZoneName: 'long'
};
return Formats.DATETIME_HUGE;
}

/**
* {@link toLocaleString} format like 'Friday, October 14, 1983, 9:30:33 AM Eastern Daylight Time'. Only 12-hour if the locale is.
*/
static get DATETIME_HUGE_WITH_SECONDS() {
return {
year: 'numeric',
month: 'long',
day: 'numeric',
weekday: 'long',
hour: 'numeric',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'long'
};
return Formats.DATETIME_HUGE_WITH_SECONDS;
}
}
68 changes: 68 additions & 0 deletions src/impl/english.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import { Formats } from './formats';
import { Util } from './util';

function stringify(obj) {
return JSON.stringify(obj, Object.keys(obj).sort());
}

/**
* @private
*/
Expand Down Expand Up @@ -116,4 +123,65 @@ export class English {
static eraForDateTime(dt, length) {
return English.eras(length)[dt.year < 0 ? 0 : 1];
}

static formatString(knownFormat) {
// these all have the offsets removed because we don't have access to them
// without all the intl stuff this is backfilling
const filtered = Util.pick(knownFormat, [
'weekday',
'era',
'year',
'month',
'day',
'hour',
'minute',
'second',
'timeZoneName'
]),
key = stringify(filtered);
switch (key) {
case stringify(Formats.DATE_SHORT):
return 'M/d/yyyy';
case stringify(Formats.DATE_MED):
return 'LLL d, yyyy';
case stringify(Formats.DATE_FULL):
return 'LLLL d, yyyy';
case stringify(Formats.DATE_HUGE):
return 'EEEE, LLLL d, yyyy';
case stringify(Formats.TIME_SIMPLE):
return 'h:mm a';
case stringify(Formats.TIME_WITH_SECONDS):
return 'h:mm:ss a';
case stringify(Formats.TIME_WITH_SHORT_OFFSET):
return 'h:mm a';
case stringify(Formats.TIME_WITH_LONG_OFFSET):
return 'h:mm a';
case stringify(Formats.TIME_24_SIMPLE):
return 'HH:mm';
case stringify(Formats.TIME_24_WITH_SECONDS):
return 'HH:mm:ss';
case stringify(Formats.TIME_24_WITH_SHORT_OFFSET):
return 'HH:mm a';
case stringify(Formats.TIME_24_WITH_LONG_OFFSET):
return 'HH:mm a';
case stringify(Formats.DATETIME_SHORT):
return 'M/d/yyyy, h:mm a';
case stringify(Formats.DATETIME_MED):
return 'LLL d, yyyy, h:mm a';
case stringify(Formats.DATETIME_FULL):
return 'LLLL d, yyyy, h:mm a';
case stringify(Formats.DATETIME_HUGE):
return 'EEEE, LLLL d, yyyy, h:mm a';
case stringify(Formats.DATETIME_SHORT_WITH_SECONDS):
return 'M/d/yyyy, h:mm:ss a';
case stringify(Formats.DATETIME_MED_WITH_SECONDS):
return 'LLL d, yyyy, h:mm:ss a';
case stringify(Formats.DATETIME_FULL_WITH_SECONDS):
return 'LLLL d, yyyy, h:mm:ss';
case stringify(Formats.DATETIME_HUGE_WITH_SECONDS):
return 'EEEE, LLLL d, yyyy, h:mm:ss a';
default:
return 'EEEE, LLLL d, yyyy, h:mm a';
}
}
}

0 comments on commit 043fef1

Please sign in to comment.