
committed by
Tobias Bosch

parent
3143d188ae
commit
b716046b97
@ -40,3 +40,21 @@ class NumberFormatter {
|
||||
return formatter.format(number);
|
||||
}
|
||||
}
|
||||
|
||||
class DateFormatter {
|
||||
static RegExp _multiPartRegExp = new RegExp(r'^([yMdE]+)([Hjms]+)$');
|
||||
|
||||
static String format(DateTime date, String locale, String pattern) {
|
||||
locale = _normalizeLocale(locale);
|
||||
var formatter = new DateFormat(null, locale);
|
||||
var matches = _multiPartRegExp.firstMatch(pattern);
|
||||
if (matches != null) {
|
||||
// Support for patterns which have known date and time components.
|
||||
formatter.addPattern(matches[1]);
|
||||
formatter.addPattern(matches[2], ', ');
|
||||
} else {
|
||||
formatter.addPattern(pattern);
|
||||
}
|
||||
return formatter.format(date);
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,7 @@ declare module Intl {
|
||||
format(value: number): string;
|
||||
}
|
||||
|
||||
var NumberFormat: {
|
||||
new (locale?: string, options?: NumberFormatOptions): NumberFormat;
|
||||
}
|
||||
var NumberFormat: { new (locale?: string, options?: NumberFormatOptions): NumberFormat; }
|
||||
|
||||
interface DateTimeFormatOptions {
|
||||
localeMatcher?: string;
|
||||
@ -37,9 +35,7 @@ declare module Intl {
|
||||
format(date?: Date | number): string;
|
||||
}
|
||||
|
||||
var DateTimeFormat: {
|
||||
new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat;
|
||||
}
|
||||
var DateTimeFormat: { new (locale?: string, options?: DateTimeFormatOptions): DateTimeFormat; }
|
||||
}
|
||||
|
||||
export enum NumberFormatStyle {
|
||||
@ -71,3 +67,78 @@ export class NumberFormatter {
|
||||
return new Intl.NumberFormat(locale, intlOptions).format(number);
|
||||
}
|
||||
}
|
||||
|
||||
function digitCondition(len: int): string {
|
||||
return len == 2 ? '2-digit' : 'numeric';
|
||||
}
|
||||
function nameCondition(len: int): string {
|
||||
return len < 4 ? 'short' : 'long';
|
||||
}
|
||||
function extractComponents(pattern: string): Intl.DateTimeFormatOptions {
|
||||
var ret: Intl.DateTimeFormatOptions = {};
|
||||
var i = 0, j;
|
||||
while (i < pattern.length) {
|
||||
j = i;
|
||||
while (j < pattern.length && pattern[j] == pattern[i]) j++;
|
||||
let len = j - i;
|
||||
switch (pattern[i]) {
|
||||
case 'G':
|
||||
ret.era = nameCondition(len);
|
||||
break;
|
||||
case 'y':
|
||||
ret.year = digitCondition(len);
|
||||
break;
|
||||
case 'M':
|
||||
if (len >= 3)
|
||||
ret.month = nameCondition(len);
|
||||
else
|
||||
ret.month = digitCondition(len);
|
||||
break;
|
||||
case 'd':
|
||||
ret.day = digitCondition(len);
|
||||
break;
|
||||
case 'E':
|
||||
ret.weekday = nameCondition(len);
|
||||
break;
|
||||
case 'j':
|
||||
ret.hour = digitCondition(len);
|
||||
break;
|
||||
case 'h':
|
||||
ret.hour = digitCondition(len);
|
||||
ret.hour12 = true;
|
||||
break;
|
||||
case 'H':
|
||||
ret.hour = digitCondition(len);
|
||||
ret.hour12 = false;
|
||||
break;
|
||||
case 'm':
|
||||
ret.minute = digitCondition(len);
|
||||
break;
|
||||
case 's':
|
||||
ret.second = digitCondition(len);
|
||||
break;
|
||||
case 'z':
|
||||
ret.timeZoneName = 'long';
|
||||
break;
|
||||
case 'Z':
|
||||
ret.timeZoneName = 'short';
|
||||
break;
|
||||
}
|
||||
i = j;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
var dateFormatterCache: Map<string, Intl.DateTimeFormat> = new Map<string, Intl.DateTimeFormat>();
|
||||
|
||||
export class DateFormatter {
|
||||
static format(date: Date, locale: string, pattern: string): string {
|
||||
var key = locale + pattern;
|
||||
if (dateFormatterCache.has(key)) {
|
||||
return dateFormatterCache.get(key).format(date);
|
||||
}
|
||||
var formatter = new Intl.DateTimeFormat(locale, extractComponents(pattern));
|
||||
dateFormatterCache.set(key, formatter);
|
||||
return formatter.format(date);
|
||||
}
|
||||
}
|
||||
|
@ -33,6 +33,7 @@ bool isStringMap(obj) => obj is Map;
|
||||
bool isArray(obj) => obj is List;
|
||||
bool isPromise(obj) => obj is Future;
|
||||
bool isNumber(obj) => obj is num;
|
||||
bool isDate(obj) => obj is DateTime;
|
||||
|
||||
String stringify(obj) => obj.toString();
|
||||
|
||||
@ -232,8 +233,12 @@ class Json {
|
||||
}
|
||||
|
||||
class DateWrapper {
|
||||
static DateTime create(int year, [int month = 1, int day = 1, int hour = 0,
|
||||
int minutes = 0, int seconds = 0, int milliseconds = 0]) {
|
||||
return new DateTime(year, month, day, hour, minutes, seconds, milliseconds);
|
||||
}
|
||||
static DateTime fromMillis(int ms) {
|
||||
return new DateTime.fromMillisecondsSinceEpoch(ms);
|
||||
return new DateTime.fromMillisecondsSinceEpoch(ms, isUtc: true);
|
||||
}
|
||||
static int toMillis(DateTime date) {
|
||||
return date.millisecondsSinceEpoch;
|
||||
@ -241,7 +246,7 @@ class DateWrapper {
|
||||
static DateTime now() {
|
||||
return new DateTime.now();
|
||||
}
|
||||
static toJson(DateTime date) {
|
||||
static String toJson(DateTime date) {
|
||||
return date.toUtc().toIso8601String();
|
||||
}
|
||||
}
|
||||
|
@ -93,6 +93,10 @@ export function isNumber(obj): boolean {
|
||||
return typeof obj === 'number';
|
||||
}
|
||||
|
||||
export function isDate(obj): boolean {
|
||||
return obj instanceof Date && !isNaN(obj.valueOf());
|
||||
}
|
||||
|
||||
export function stringify(token): string {
|
||||
if (typeof token === 'string') {
|
||||
return token;
|
||||
@ -282,8 +286,12 @@ export class Json {
|
||||
}
|
||||
|
||||
export class DateWrapper {
|
||||
static fromMillis(ms): Date { return new Date(ms); }
|
||||
static toMillis(date: Date): number { return date.getTime(); }
|
||||
static create(year: int, month: int = 1, day: int = 1, hour: int = 0, minutes: int = 0,
|
||||
seconds: int = 0, milliseconds: int = 0): Date {
|
||||
return new Date(year, month - 1, day, hour, minutes, seconds, milliseconds);
|
||||
}
|
||||
static fromMillis(ms: int): Date { return new Date(ms); }
|
||||
static toMillis(date: Date): int { return date.getTime(); }
|
||||
static now(): Date { return new Date(); }
|
||||
static toJson(date): string { return date.toJSON(); }
|
||||
static toJson(date: Date): string { return date.toJSON(); }
|
||||
}
|
||||
|
Reference in New Issue
Block a user