Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements #6

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ This isn't a comprehesive list, but it hopefully will hit the high points.
* Remove conditions in `finish()` in `parser.js` that are not used by any tests
* Fix a couple relative-ish cases like `15th at 3pm`
* Added support for "biasing" toward dates in the past or the future

* Added support for load multiple locale files
* Added support for switch between locales (with Date.setLocale) and for single instance

## Biasing ##

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ task :test do
sh "node --stack-trace-limit=2 test/node/node_modules/nodeunit/bin/nodeunit --reporter skip_passed test/node/node_test.coffee"
end

task :default => :test
task :default => :test
5 changes: 4 additions & 1 deletion TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ TODO

3. More tests!

4. Test for changing locale at runtime

5. Test for multiple locale file loading


CUTTING ROOM FLOOR
Expand Down Expand Up @@ -61,4 +64,4 @@ They are documented here for reference.
*/
$D.isDate = function (obj) {
return (obj !== null) ? obj.constructor.toString().match(/Date/i) == "Date" : false;
};
};
77 changes: 58 additions & 19 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
(function () {
var $D = Date,
$P = $D.prototype,
$C = $D.CultureInfo,
p = function (s, l) {
if (!l) {
l = 2;
Expand Down Expand Up @@ -84,6 +83,7 @@
* @return {Number} The day number
*/
$D.getDayNumberFromName = function (name) {
var $C = this.getCultureInfo();
var n = $C.dayNames, m = $C.abbreviatedDayNames, o = $C.shortestDayNames, s = name.toLowerCase();
for (var i = 0; i < n.length; i++) {
if (n[i].toLowerCase() == s || m[i].toLowerCase() == s || o[i].toLowerCase() == s) {
Expand All @@ -99,6 +99,7 @@
* @return {Number} The day number
*/
$D.getMonthNumberFromName = function (name) {
var $C = this.getCultureInfo();
var n = $C.monthNames, m = $C.abbreviatedMonthNames, s = name.toLowerCase();
for (var i = 0; i < n.length; i++) {
if (n[i].toLowerCase() == s || m[i].toLowerCase() == s) {
Expand Down Expand Up @@ -128,6 +129,7 @@
};

$D.getTimezoneAbbreviation = function (offset) {
var $C = this.getCultureInfo();
var z = $C.timezones, p;
for (var i = 0; i < z.length; i++) {
if (z[i].offset === offset) {
Expand All @@ -138,6 +140,7 @@
};

$D.getTimezoneOffset = function (name) {
var $C = this.getCultureInfo();
var z = $C.timezones, p;
for (var i = 0; i < z.length; i++) {
if (z[i].name === name.toUpperCase()) {
Expand Down Expand Up @@ -764,35 +767,41 @@
* @param {String} A format string consisting of one or more format spcifiers [Optional].
* @return {String} A string representation of the current Date object.
*/
$P.toString = function (format) {
$P.toString = function (format, options) {
var x = this;
var $CI = this.cultureInfo;
if ( options instanceof Object && options.locale )
{
$CI = Date.AvailableCultureInfo[options.locale];
}
$CI = $CI || Date.getCultureInfo(); // Default to Date.CultureInfo

// Standard Date and Time Format Strings. Formats pulled from CultureInfo file and
// may vary by culture.
if (format && format.length == 1) {
var c = $C.formatPatterns;
var c = $CI.formatPatterns;
x.t = x.toString;
switch (format) {
case "d":
return x.t(c.shortDate);
return x.t(c.shortDate, options);
case "D":
return x.t(c.longDate);
return x.t(c.longDate, options);
case "F":
return x.t(c.fullDateTime);
return x.t(c.fullDateTime, options);
case "m":
return x.t(c.monthDay);
return x.t(c.monthDay, options);
case "r":
return x.t(c.rfc1123);
return x.t(c.rfc1123, options);
case "s":
return x.t(c.sortableDateTime);
return x.t(c.sortableDateTime, options);
case "t":
return x.t(c.shortTime);
return x.t(c.shortTime, options);
case "T":
return x.t(c.longTime);
return x.t(c.longTime, options);
case "u":
return x.t(c.universalSortableDateTime);
return x.t(c.universalSortableDateTime, options);
case "y":
return x.t(c.yearMonth);
return x.t(c.yearMonth, options);
}
}

Expand Down Expand Up @@ -841,25 +850,25 @@
case "yy":
return p(x.getFullYear());
case "dddd":
return $C.dayNames[x.getDay()];
return $CI.dayNames[x.getDay()];
case "ddd":
return $C.abbreviatedDayNames[x.getDay()];
return $CI.abbreviatedDayNames[x.getDay()];
case "dd":
return p(x.getDate());
case "d":
return x.getDate();
case "MMMM":
return $C.monthNames[x.getMonth()];
return $CI.monthNames[x.getMonth()];
case "MMM":
return $C.abbreviatedMonthNames[x.getMonth()];
return $CI.abbreviatedMonthNames[x.getMonth()];
case "MM":
return p((x.getMonth() + 1));
case "M":
return x.getMonth() + 1;
case "t":
return x.h() < 12 ? $C.amDesignator.substring(0, 1) : $C.pmDesignator.substring(0, 1);
return x.h() < 12 ? $CI.amDesignator.substring(0, 1) : $CI.pmDesignator.substring(0, 1);
case "tt":
return x.h() < 12 ? $C.amDesignator : $C.pmDesignator;
return x.h() < 12 ? $CI.amDesignator : $CI.pmDesignator;
case "S":
return ord(x.getDate());
default:
Expand All @@ -868,4 +877,34 @@
}
) : this._toString();
};

$P.getCultureInfo = function() {
return this.cultureInfo || $D.CultureInfo;
};

$P.setLocale = function(cultureInfo) {
if ( cultureInfo instanceof Object )
this.cultureInfo = cultureInfo;
else if ( Date.AvailableCultureInfo && Date.AvailableCultureInfo[cultureInfo] )
this.cultureInfo = Date.AvailableCultureInfo[cultureInfo];
else
throw "Unknow locale";
};

$D.setLocale = function(cultureInfo) {
if ( cultureInfo instanceof Object )
{
$D.CultureInfo = cultureInfo;
}
else if ( Date.AvailableCultureInfo && Date.AvailableCultureInfo[cultureInfo] )
{
$D.CultureInfo = Date.AvailableCultureInfo[cultureInfo];
}
else
throw "Unknow locale";
};

$D.getCultureInfo = function() {
return this.CultureInfo;
};
}());
1 change: 0 additions & 1 deletion src/extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
(function () {
var $D = Date,
$P = $D.prototype,
$C = $D.CultureInfo,
$f = [],
p = function (s, l) {
if (!l) {
Expand Down
9 changes: 7 additions & 2 deletions src/globalization/af-ZA.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["af-ZA"] = {
/* Culture Name */
name: "af-ZA",
englishName: "Afrikaans (South Africa)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["af-ZA"];
9 changes: 7 additions & 2 deletions src/globalization/ar-AE.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-AE"] = {
/* Culture Name */
name: "ar-AE",
englishName: "Arabic (U.A.E.)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-AE"];
9 changes: 7 additions & 2 deletions src/globalization/ar-BH.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-BH"] = {
/* Culture Name */
name: "ar-BH",
englishName: "Arabic (Bahrain)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-BH"];
9 changes: 7 additions & 2 deletions src/globalization/ar-DZ.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-DZ"] = {
/* Culture Name */
name: "ar-DZ",
englishName: "Arabic (Algeria)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-DZ"];
9 changes: 7 additions & 2 deletions src/globalization/ar-EG.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-EG"] = {
/* Culture Name */
name: "ar-EG",
englishName: "Arabic (Egypt)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-EG"];
9 changes: 7 additions & 2 deletions src/globalization/ar-IQ.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-IQ"] = {
/* Culture Name */
name: "ar-IQ",
englishName: "Arabic (Iraq)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-IQ"];
9 changes: 7 additions & 2 deletions src/globalization/ar-JO.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-JO"] = {
/* Culture Name */
name: "ar-JO",
englishName: "Arabic (Jordan)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-JO"];
9 changes: 7 additions & 2 deletions src/globalization/ar-KW.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-KW"] = {
/* Culture Name */
name: "ar-KW",
englishName: "Arabic (Kuwait)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-KW"];
9 changes: 7 additions & 2 deletions src/globalization/ar-LB.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-LB"] = {
/* Culture Name */
name: "ar-LB",
englishName: "Arabic (Lebanon)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-LB"];
9 changes: 7 additions & 2 deletions src/globalization/ar-LY.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Date.CultureInfo = {
if ( !Date.AvailableCultureInfo )
Date.AvailableCultureInfo = {};

Date.AvailableCultureInfo["ar-LY"] = {
/* Culture Name */
name: "ar-LY",
englishName: "Arabic (Libya)",
Expand Down Expand Up @@ -192,4 +195,6 @@ Date.CultureInfo = {
* end end
* long long
* short short
*/
*/
if ( !Date.CultureInfo )
Date.CultureInfo = Date.AvailableCultureInfo["ar-LY"];
Loading