Skip to content

Commit

Permalink
Date: Olson-timezone-support (real z, v, V) and options.timeZone (2/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxaviers committed Apr 7, 2017
1 parent 735b610 commit 5ba59fe
Show file tree
Hide file tree
Showing 23 changed files with 1,142 additions and 1,386 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ requirements. See table below.
|---|---|
| Core module | cldr/supplemental/likelySubtags.json |
| Currency module | cldr/main/`locale`/currencies.json<br>cldr/supplemental/currencyData.json<br>+CLDR JSON files from number module<br>+CLDR JSON files from plural module for name style support |
| Date module | cldr/main/`locale`/ca-gregorian.json<br>cldr/main/`locale`/timeZoneNames.json<br>cldr/supplemental/timeData.json<br>cldr/supplemental/weekData.json<br>+CLDR JSON files from number module |
| Date module | cldr/main/`locale`/ca-gregorian.json<br>cldr/main/`locale`/timeZoneNames.json<br>cldr/supplemental/metaZones.json<br>cldr/supplemental/timeData.json<br>cldr/supplemental/weekData.json<br>+CLDR JSON files from number module |
| Number module | cldr/main/`locale`/numbers.json<br>cldr/supplemental/numberingSystems.json |
| Plural module | cldr/supplemental/plurals.json (for cardinals)<br>cldr/supplemental/ordinals.json (for ordinals) |
| Relative time module | cldr/main/`locale`/dateFields.json<br>+CLDR JSON files from number and plural modules |
Expand Down
35 changes: 29 additions & 6 deletions doc/api/date/date-formatter.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ A JSON object including one of the following.
> [raw pattern (anything in the "Sym." column)](http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table)
> eg. `{ raw: "dd/mm" }`. Note this is NOT recommended for i18n in general.
> Use `skeleton` instead.
>
> **timeZone**
>
> String based on the time zone names of the [IANA time zone
> database](https://www.iana.org/time-zones), such as `"Asia/Shanghai"`, `"Asia/Kolkata"`,
> `"America/New_York"`.
**value**

Expand All @@ -63,15 +69,12 @@ Date instance to be formatted, eg. `new Date()`;

Prior to using any date methods, you must load
`cldr/main/{locale}/ca-gregorian.json`, `cldr/main/{locale}/timeZoneNames.json`,
`cldr/supplemental/timeData.json`, `cldr/supplemental/weekData.json`, and the
CLDR content required by the number module. Read [CLDR content][] if you need
more information.
`cldr/supplemental/metaZones.json`, `cldr/supplemental/timeData.json`,
`cldr/supplemental/weekData.json`, and the CLDR content required by the number
module. Read [CLDR content][] if you need more information.

[CLDR content]: ../../../README.md#2-cldr-content

You can use the static method `Globalize.dateFormatter()`, which uses the default
locale.

```javascript
var formatter;

Expand Down Expand Up @@ -183,6 +186,26 @@ hourMinuteSecondFormatter( date );
// > "17:55:00"
```

Using specific timeZones, i.e., using `options.timezone`. Note that prior to using
it, you must load IANA Timezone Data.

```js
Globalize.loadIANATimezone( require( "iana-tz-data" ) );
```

```js
Globalize.locale( "en" );

Globalize.dateFormatter({ datetime: "medium", timeZone: "America/Los_Angeles" })( new Date() );
// > "Nov 1, 2010, 12:55:00 PM"

Globalize.dateFormatter({ datetime: "medium", timeZone: "America/Sao_Paulo" })( new Date() )
// > "Nov 1, 2010, 5:55:00 PM"

Globalize.dateFormatter({ datetime: "full", timeZone: "Europe/Berlin" })( new Date() )
// > "Monday, November 1, 2010 at 8:55:00 PM Central European Standard Time"
```

For improved performance on iterations, first create the formatter. Then, reuse
it on each loop.

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"grunt-git-authors": "^3.1.0",
"grunt-jscs": "1.8.0",
"gzip-js": "0.3.2",
"iana-tz-data": "0.0.4",
"matchdep": "0.3.0"
},
"commitplease": {
Expand Down
18 changes: 16 additions & 2 deletions src/date-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ Globalize._dateParser = dateParse;
Globalize._dateTokenizer = dateTokenizer;
Globalize._validateParameterTypeDate = validateParameterTypeDate;

function optionsHasStyle( options ) {
return options.skeleton !== undefined ||
options.date !== undefined ||
options.time !== undefined ||
options.datetime !== undefined ||
options.raw !== undefined;
}

Globalize.dateFormatter =
Globalize.prototype.dateFormatter = function( options ) {
var formatterFn = this.dateToPartsFormatter( options );
Expand All @@ -33,13 +41,19 @@ Globalize.prototype.dateFormatter = function( options ) {

Globalize.dateToPartsFormatter =
Globalize.prototype.dateToPartsFormatter = function( options ) {
options = options || { skeleton: "yMd" };
options = options || {};
if ( !optionsHasStyle( options ) ) {
options.skeleton = "yMd";
}
return Globalize[ runtimeKey( "dateToPartsFormatter", this._locale, [ options ] ) ];
};

Globalize.dateParser =
Globalize.prototype.dateParser = function( options ) {
options = options || { skeleton: "yMd" };
options = options || {};
if ( !optionsHasStyle( options ) ) {
options.skeleton = "yMd";
}
return Globalize[ runtimeKey( "dateParser", this._locale, [ options ] ) ];
};

Expand Down
71 changes: 61 additions & 10 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ define([
validateParameterTypeString, Globalize, dateExpandPattern, dateToPartsFormatterFn,
dateFormatProperties, dateParserFn, dateParseProperties, dateTokenizerProperties ) {

function optionsHasStyle( options ) {
return options.skeleton !== undefined ||
options.date !== undefined ||
options.time !== undefined ||
options.datetime !== undefined ||
options.raw !== undefined;
}

function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
skip: [
/dates\/calendars\/gregorian\/dateTimeFormats\/availableFormats/,
/dates\/calendars\/gregorian\/days\/.*\/short/,
/dates\/timeZoneNames\/zone/,
/dates\/timeZoneNames\/metazone/,
/globalize-iana/,
/supplemental\/metaZones/,
/supplemental\/timeData\/(?!001)/,
/supplemental\/weekData\/(?!001)/
]
Expand Down Expand Up @@ -60,14 +72,33 @@ function validateOptionsSkeleton( pattern, skeleton ) {
);
}

function validateRequiredIana( timeZone ) {
return function( path, value ) {

if ( !/globalize-iana/.test( path ) ) {
return;
}

validate(
"E_MISSING_IANA_TZ",
"Missing required IANA timezone content for `{timeZone}`: `{path}`.",
value,
{
path: path.replace( /globalize-iana\//, "" ),
timeZone: timeZone
}
);
};
}

/**
* .loadIANA( json )
* .loadIANATimezone( json )
*
* @json [JSON]
*
* Load IANA data.
* Load IANA timezone data.
*/
Globalize.loadIANA = function( json ) {
Globalize.loadIANATimezone = function( json ) {
var customData = {
"globalize-iana": json
};
Expand Down Expand Up @@ -122,26 +153,36 @@ Globalize.prototype.dateFormatter = function( options ) {
*/
Globalize.dateToPartsFormatter =
Globalize.prototype.dateToPartsFormatter = function( options ) {
var args, cldr, numberFormatters, pad, pattern, properties, returnFn, timeZone;
var args, cldr, numberFormatters, pad, pattern, properties, returnFn,
timeZone;

validateParameterTypePlainObject( options, "options" );

cldr = this.cldr;
options = options || { skeleton: "yMd" };
options = options || {};
if ( !optionsHasStyle( options ) ) {
options.skeleton = "yMd";
}

validateOptionsPreset( options );
validateDefaultLocale( cldr );

timeZone = options.timeZone;
validateParameterTypeString( options.timeZone, "options.timeZone" );
validateParameterTypeString( timeZone, "options.timeZone" );

args = [ options ];

cldr.on( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.on( "get", validateRequiredIana( timeZone ) );
}
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
properties = dateFormatProperties( pattern, cldr, timeZone );
cldr.off( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.off( "get", validateRequiredIana( timeZone ) );
}

// Create needed number formatters.
numberFormatters = properties.numberFormatters;
Expand Down Expand Up @@ -169,27 +210,37 @@ Globalize.prototype.dateToPartsFormatter = function( options ) {
*/
Globalize.dateParser =
Globalize.prototype.dateParser = function( options ) {
var args, cldr, numberParser, parseProperties, pattern, tokenizerProperties, returnFn, timeZone;
var args, cldr, numberParser, parseProperties, pattern, returnFn, timeZone,
tokenizerProperties;

validateParameterTypePlainObject( options, "options" );

cldr = this.cldr;
options = options || { skeleton: "yMd" };
options = options || {};
if ( !optionsHasStyle( options ) ) {
options.skeleton = "yMd";
}

validateOptionsPreset( options );
validateDefaultLocale( cldr );

timeZone = options.timeZone;
validateParameterTypeString( options.timeZone, "options.timeZone" );
validateParameterTypeString( timeZone, "options.timeZone" );

args = [ options ];

cldr.on( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.on( "get", validateRequiredIana( timeZone ) );
}
pattern = dateExpandPattern( options, cldr );
validateOptionsSkeleton( pattern, options.skeleton );
tokenizerProperties = dateTokenizerProperties( pattern, cldr );
parseProperties = dateParseProperties( cldr );
parseProperties = dateParseProperties( cldr, timeZone );
cldr.off( "get", validateRequiredCldr );
if ( timeZone ) {
cldr.off( "get", validateRequiredIana( timeZone ) );
}

numberParser = this.numberParser({ raw: "0" });

Expand Down
2 changes: 1 addition & 1 deletion src/date/fields-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ return objectInvert({
"hour": "hHkK",
"minute": "m",
"second": "sSA",
"zone": "zOxX"
"zone": "zvVOxX"
}, function( object, key, value ) {
value.split( "" ).forEach(function( symbol ) {
object[ symbol ] = key;
Expand Down
Loading

0 comments on commit 5ba59fe

Please sign in to comment.