Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

renamed timezone methods, added coerce param

  • Loading branch information...
commit 0d52af636009dc4ba9ed38c27ffa4522ca87b1ef 1 parent 70cd86c
@arshaw authored
View
50 src/xdate.js
@@ -9,7 +9,7 @@
* ---------------------
* An XDate wraps a native Date. The native Date is stored in the '0' property of the object.
* By default, an XDate has a timezone. However, to signify that an XDate does NOT have a timezone,
- * the `notzToString` method is assigned to the internal Date's toString (see `hasTimezone`).
+ * the `notzToString` method is assigned to the internal Date's toString (see `hasLocalTimezone`).
*
*/
@@ -129,33 +129,39 @@ function init(xdate, args) {
---------------------------------------------------------------------------------*/
-proto.addTimezone = methodize(addTimezone);
-function addTimezone(xdate) {
- if (!hasTimezone(xdate)) {
- xdate[0] = UTCToLocal(xdate[0]); // makes a new date, wiping out notzToString
+proto.addLocalTimezone = methodize(addLocalTimezone);
+function addLocalTimezone(xdate, coerce) {
+ if (!hasLocalTimezone(xdate)) {
+ if (coerce) {
+ xdate[0] = UTCToLocal(xdate[0]); // makes a new date, wiping out notzToString
+ }else{
+ xdate[0] = new Date(xdate[0]); // makes a new date, wiping out notzToString
+ }
}
return xdate; // for chaining
}
-proto.removeTimezone = methodize(removeTimezone);
-function removeTimezone(xdate) {
- if (hasTimezone(xdate)) {
- xdate[0] = localToUTC(xdate[0]);
+proto.removeLocalTimezone = methodize(removeLocalTimezone);
+function removeLocalTimezone(xdate, coerce) {
+ if (hasLocalTimezone(xdate)) {
+ if (coerce) {
+ xdate[0] = localToUTC(xdate[0]);
+ }
xdate[0].toString = notzToString;
}
return xdate; // for chaining
}
-proto.hasTimezone = methodize(hasTimezone);
-function hasTimezone(xdate) {
+proto.hasLocalTimezone = methodize(hasLocalTimezone);
+function hasLocalTimezone(xdate) {
return xdate[0].toString !== notzToString;
}
proto.getTimezoneOffset = function() {
- if (hasTimezone(this)) {
+ if (hasLocalTimezone(this)) {
return this[0].getTimezoneOffset();
}else{
return 0;
@@ -171,7 +177,7 @@ proto.getTimezoneOffset = function() {
each(methodSubjects, function(subject, fieldIndex) {
proto['get' + subject] = function() {
- return this[0]['get' + (hasTimezone(this) ? '' : 'UTC') + subject]();
+ return this[0]['get' + (hasLocalTimezone(this) ? '' : 'UTC') + subject]();
};
if (fieldIndex != YEAR) { // because there is no getUTCYear
@@ -224,7 +230,7 @@ function _set(xdate, fieldIndex, value, args, useUTC) {
preventOverflow = args[1];
args = [value];
}
- useUTC = useUTC || !hasTimezone(xdate);
+ useUTC = useUTC || !hasLocalTimezone(xdate);
function getField(i) {
return useUTC ? getUTCField(date, i) : getLocalField(date, i);
@@ -263,10 +269,10 @@ function _diff(xdate, fieldIndex, otherDate) { // fieldIndex=FULLYEAR is for yea
var d1 = xdate.toDate();
var d2 = XDate(otherDate); // will be made a native Date...
- if (xdate.hasTimezone()) {
- d2 = d2.addTimezone();
+ if (xdate.hasLocalTimezone()) {
+ d2 = d2.addLocalTimezone(true);
}else{
- d2 = d2.removeTimezone();
+ d2 = d2.removeLocalTimezone(true);
}
d2 = d2.toDate();
@@ -366,7 +372,7 @@ proto.toString = function(formatString, settings, uniqueness) {
proto.toUTCString = proto.toGMTString = function(formatString, settings, uniqueness) {
if (formatString === undefined || !isValid(this)) {
- if (hasTimezone(this)) {
+ if (hasLocalTimezone(this)) {
return this[0].toUTCString();
}else{
return stripGMT(this[0].toUTCString());
@@ -396,7 +402,7 @@ each(
],
function(methodName) {
proto[methodName] = function() {
- if (hasTimezone(this)) {
+ if (hasLocalTimezone(this)) {
return this[0][methodName]();
}else{
return stripGMT(UTCToLocal(this[0])[methodName]());
@@ -549,7 +555,7 @@ function getTokenReplacement(xdate, token, getField, getSetting, useUTC) {
function _getTZString(xdate, token, useUTC) {
- if (!hasTimezone(xdate)) {
+ if (!hasLocalTimezone(xdate)) {
return '';
}
if (useUTC) {
@@ -630,7 +636,7 @@ function parseISO(str, xdate) {
);
}
}else{ // has no timezone
- xdate.removeTimezone();
+ xdate.removeLocalTimezone();
}
return xdate.setTime(+d);
}
@@ -709,7 +715,7 @@ XDate.getDaysInMonth = getDaysInMonth;
function _clone(xdate) { // returns the internal Date object that should be used
var d = new Date(xdate[0]);
- if (!hasTimezone(xdate)) {
+ if (!hasLocalTimezone(xdate)) {
d.toString = notzToString;
}
return d;
View
40 test/constructors.js
@@ -9,55 +9,55 @@ test("from XDate, with timezone", function() {
var xdate1 = new XDate();
var xdate2 = new XDate(xdate1);
return xdate1.getTime() == xdate2.getTime() &&
- xdate1.hasTimezone() && xdate2.hasTimezone();
+ xdate1.hasLocalTimezone() && xdate2.hasLocalTimezone();
});
test("from XDate, with no timezone", function() {
- var xdate1 = new XDate().removeTimezone();
+ var xdate1 = new XDate().removeLocalTimezone();
var xdate2 = new XDate(xdate1);
return xdate1.getTime() == xdate2.getTime() &&
- !xdate1.hasTimezone() && !xdate2.hasTimezone();
+ !xdate1.hasLocalTimezone() && !xdate2.hasLocalTimezone();
});
-test("from native Date, hasTimezone=yes", function() {
+test("from native Date, hasLocalTimezone=yes", function() {
var date = new Date();
var xdate1 = new XDate(date);
var xdate2 = new XDate(date, true);
return date.getTime() == xdate1.getTime() &&
date.getTime() == xdate2.getTime() &&
- xdate1.hasTimezone() && xdate2.hasTimezone();
+ xdate1.hasLocalTimezone() && xdate2.hasLocalTimezone();
});
-test("from native Date, hasTimezone=no", function() {
+test("from native Date, hasLocalTimezone=no", function() {
var date = new Date();
var xdate = new XDate(date, false);
return date.getTime() == xdate.getTime() &&
- !xdate.hasTimezone();
+ !xdate.hasLocalTimezone();
});
-test("from milliseconds time, hasTimezone=yes", function() {
+test("from milliseconds time, hasLocalTimezone=yes", function() {
var MS = 933490800000;
var xdate1 = new XDate(MS);
var xdate2 = new XDate(MS, true);
- return xdate1.hasTimezone() &&
- xdate2.hasTimezone() &&
+ return xdate1.hasLocalTimezone() &&
+ xdate2.hasLocalTimezone() &&
xdate1.getTime() == MS &&
xdate2.getTime() == MS;
});
-test("from milliseconds time, hasTimezone=no", function() {
+test("from milliseconds time, hasLocalTimezone=no", function() {
var MS = 933490800000;
var xdate = new XDate(MS, false);
- return !xdate.hasTimezone() && xdate.getTime() == MS;
+ return !xdate.hasLocalTimezone() && xdate.getTime() == MS;
});
-test("year/month/date, hasTimezone=yes", function() {
+test("year/month/date, hasLocalTimezone=yes", function() {
var YEAR = 2011;
var MONTH = 5;
var DATE = 4;
var xdate1 = new XDate(YEAR, MONTH, DATE);
var xdate2 = new XDate(YEAR, MONTH, DATE, true);
- return xdate1.hasTimezone() && xdate2.hasTimezone() &&
+ return xdate1.hasLocalTimezone() && xdate2.hasLocalTimezone() &&
xdate1.getTime() == xdate2.getTime() &&
xdate1.getFullYear() == YEAR &&
xdate1.getMonth() == MONTH &&
@@ -68,7 +68,7 @@ test("year/month/date, hasTimezone=yes", function() {
!xdate1.getMilliseconds();
});
-test("year/month/date/minutes/seconds/milliseconds, hasTimezone=yes", function() {
+test("year/month/date/minutes/seconds/milliseconds, hasLocalTimezone=yes", function() {
var YEAR = 2011;
var MONTH = 5;
var DATE = 4;
@@ -78,7 +78,7 @@ test("year/month/date/minutes/seconds/milliseconds, hasTimezone=yes", function()
var MILLISECONDS = 750;
var xdate1 = new XDate(YEAR, MONTH, DATE, HOURS, MINUTES, SECONDS, MILLISECONDS);
var xdate2 = new XDate(YEAR, MONTH, DATE, HOURS, MINUTES, SECONDS, MILLISECONDS, true);
- return xdate1.hasTimezone() && xdate2.hasTimezone() &&
+ return xdate1.hasLocalTimezone() && xdate2.hasLocalTimezone() &&
xdate1.getTime() == xdate2.getTime() &&
xdate1.getFullYear() == YEAR &&
xdate1.getMonth() == MONTH &&
@@ -89,12 +89,12 @@ test("year/month/date/minutes/seconds/milliseconds, hasTimezone=yes", function()
xdate1.getMilliseconds() == MILLISECONDS;
});
-test("year/month/date, hasTimezone=no", function() {
+test("year/month/date, hasLocalTimezone=no", function() {
var YEAR = 2011;
var MONTH = 5;
var DATE = 4;
var xdate = new XDate(YEAR, MONTH, DATE, false);
- return !xdate.hasTimezone()
+ return !xdate.hasLocalTimezone()
xdate.getFullYear() == YEAR &&
xdate.getMonth() == MONTH &&
xdate.getDate() == DATE &&
@@ -104,7 +104,7 @@ test("year/month/date, hasTimezone=no", function() {
!xdate.getMilliseconds();
});
-test("year/month/date/minutes/seconds/milliseconds, hasTimezone=no", function() {
+test("year/month/date/minutes/seconds/milliseconds, hasLocalTimezone=no", function() {
var YEAR = 2011;
var MONTH = 5;
var DATE = 4;
@@ -113,7 +113,7 @@ test("year/month/date/minutes/seconds/milliseconds, hasTimezone=no", function()
var SECONDS = 20;
var MILLISECONDS = 750;
var xdate = new XDate(YEAR, MONTH, DATE, HOURS, MINUTES, SECONDS, MILLISECONDS, false);
- return !xdate.hasTimezone() &&
+ return !xdate.hasLocalTimezone() &&
xdate.getFullYear() == YEAR &&
xdate.getMonth() == MONTH &&
xdate.getDate() == DATE &&
View
4 test/formatting.js
@@ -203,7 +203,7 @@ test("custom formatter function", function() {
-test("toString methods, hasTimezone=yes", function() {
+test("toString methods, hasLocalTimezone=yes", function() {
var realDate = new Date(2011, 3, 20, 12, 30);
var xdate = new XDate(2011, 3, 20, 12, 30);
return realDate.toString() == xdate.toString() &&
@@ -216,7 +216,7 @@ test("toString methods, hasTimezone=yes", function() {
realDate.toGMTString() == xdate.toGMTString();
});
-test("toString methods, hasTimezone=no", function() {
+test("toString methods, hasLocalTimezone=no", function() {
var realDate = new Date(2011, 3, 20, 12, 30);
var xdate = new XDate(2011, 3, 20, 12, 30, false);
return realDate.toString().indexOf(xdate.toString()) == 0 &&
View
22 test/parsing.js
@@ -9,7 +9,7 @@ test("IETF", function() {
d.getUTCHours() == 13 &&
d.getUTCMinutes() == 44 &&
d.getUTCSeconds() == 12 &&
- d.hasTimezone() &&
+ d.hasLocalTimezone() &&
+d == +new Date(s);
});
@@ -24,7 +24,7 @@ test("ISO, no time", function() {
!d.getMinutes() &&
!d.getSeconds() &&
!d.getMilliseconds() &&
- !d.hasTimezone();
+ !d.hasLocalTimezone();
});
@@ -38,7 +38,7 @@ test("ISO, T", function() {
d.getMinutes() == 45 &&
d.getSeconds() == 30 &&
!d.getMilliseconds() &&
- !d.hasTimezone();
+ !d.hasLocalTimezone();
});
@@ -52,7 +52,7 @@ test("ISO, space", function() {
d.getMinutes() == 45 &&
d.getSeconds() == 30 &&
!d.getMilliseconds() &&
- !d.hasTimezone();
+ !d.hasLocalTimezone();
});
@@ -66,7 +66,7 @@ test("ISO, no seconds", function() {
d.getMinutes() == 45 &&
!d.getSeconds() &&
!d.getMilliseconds() &&
- !d.hasTimezone();
+ !d.hasLocalTimezone();
});
@@ -80,7 +80,7 @@ test("ISO, milliseconds", function() {
d.getMinutes() == 45 &&
d.getSeconds() == 30 &&
d.getMilliseconds() == 500 &&
- !d.hasTimezone();
+ !d.hasLocalTimezone();
});
@@ -90,7 +90,7 @@ test("ISO, timezone colon", function() {
return d.getUTCHours() == 16 &&
d.getUTCMinutes() == 30 &&
d.getUTCSeconds() == 28 &&
- d.hasTimezone();
+ d.hasLocalTimezone();
});
@@ -100,7 +100,7 @@ test("ISO, timezone no colon", function() {
return d.getUTCHours() == 16 &&
d.getUTCMinutes() == 30 &&
d.getUTCSeconds() == 28 &&
- d.hasTimezone();
+ d.hasLocalTimezone();
});
@@ -110,7 +110,7 @@ test("ISO, timezone hour only", function() {
return d.getUTCHours() == 16 &&
d.getUTCMinutes() == 45 &&
d.getUTCSeconds() == 34 &&
- d.hasTimezone();
+ d.hasLocalTimezone();
});
@@ -120,7 +120,7 @@ test("ISO, timezone positive", function() {
return d.getUTCHours() == 11 &&
d.getUTCMinutes() == 30 &&
d.getUTCSeconds() == 28 &&
- d.hasTimezone();
+ d.hasLocalTimezone();
});
@@ -130,7 +130,7 @@ test("ISO, with Z", function() {
d.getUTCMonth() == 5 &&
d.getUTCDate() == 8 &&
!d.getUTCHours() && !d.getUTCMinutes() &&
- d.hasTimezone();
+ d.hasLocalTimezone();
});
View
10 test/setters.js
@@ -1,6 +1,6 @@
-test("set*, when hasTimezone=yes", function() {
+test("set*, when hasLocalTimezone=yes", function() {
var xdate = new XDate(2012, 0, 1);
var YEAR = 2011;
var MONTH = 5;
@@ -25,7 +25,7 @@ test("set*, when hasTimezone=yes", function() {
xdate.getMilliseconds() == MILLISECONDS;
});
-test("set*, when hasTimezone=no", function() {
+test("set*, when hasLocalTimezone=no", function() {
var xdate = new XDate(2012, 0, 1, false);
var YEAR = 2011;
var MONTH = 5;
@@ -53,7 +53,7 @@ test("set*, when hasTimezone=no", function() {
test("setTime", function() {
var MS = 933490800000;
var xdate1 = new XDate();
- var xdate2 = new XDate().removeTimezone();
+ var xdate2 = new XDate().removeLocalTimezone();
xdate1.setTime(MS);
xdate2.setTime(MS);
return xdate1.getTime() == MS && xdate2.getTime() == MS;
@@ -89,7 +89,7 @@ test("setMonth, prevent overflow", function() {
return d.getMonth() == 1 && d.getDate() == 28;
});
-test("setUTC*, with hasTimezone=yes", function() {
+test("setUTC*, with hasLocalTimezone=yes", function() {
var xdate = new XDate(2012, 0, 1);
var YEAR = 2011;
var MONTH = 5;
@@ -114,7 +114,7 @@ test("setUTC*, with hasTimezone=yes", function() {
xdate.getUTCMilliseconds() == MILLISECONDS;
});
-test("setUTC*, with hasTimezone=no", function() {
+test("setUTC*, with hasLocalTimezone=no", function() {
var xdate = new XDate(2012, 0, 1, false);
var YEAR = 2011;
var MONTH = 5;
View
36 test/timezones.js
@@ -1,28 +1,40 @@
-test("removeTimezone", function() {
+test("hasLocalTimezone", function() {
+ var xdate1 = new XDate(0, true);
+ var xdate2 = new XDate(0, false);
+ return xdate1.hasLocalTimezone() && !xdate2.hasLocalTimezone();
+});
+
+test("removeLocalTimezone", function() {
+ var xdate1 = new XDate();
+ var xdate2 = xdate1.clone().removeLocalTimezone();
+ return xdate1.hasLocalTimezone() && !xdate2.hasLocalTimezone() && +xdate1 == +xdate2;
+});
+
+test("removeLocalTimezone, coerce", function() {
var xdate1 = new XDate();
- var xdate2 = xdate1.clone().removeTimezone();
- return !xdate2.hasTimezone() &&
+ var xdate2 = xdate1.clone().removeLocalTimezone(true);
+ return !xdate2.hasLocalTimezone() &&
(!xdate1.getTimezoneOffset() || xdate1.getTime() != xdate2.getTime()) &&
(xdate1.getDate() == xdate2.getDate() && xdate2.getDate() == xdate2.getUTCDate()) &&
(xdate1.getHours() == xdate2.getHours() && xdate2.getHours() == xdate2.getUTCHours());
});
-test("addTimezone", function() {
+test("addLocalTimezone", function() {
+ var xdate1 = new XDate(2011, 5, 8, false);
+ var xdate2 = xdate1.clone().addLocalTimezone();
+ return !xdate1.hasLocalTimezone() && xdate2.hasLocalTimezone() && +xdate1 == +xdate2;
+});
+
+test("addLocalTimezone, coerce", function() {
var xdate1 = new XDate(2011, 7, 8, false);
- var xdate2 = xdate1.clone().addTimezone();
- return xdate2.hasTimezone() &&
+ var xdate2 = xdate1.clone().addLocalTimezone(true);
+ return xdate2.hasLocalTimezone() &&
(!xdate1.getTimezoneOffset() || xdate1.getTime() != xdate2.getTime()) &&
xdate1.getDate() == xdate2.getDate() &&
xdate1.getHours() == xdate1.getHours();
});
-test("hasTimezone", function() {
- var xdate1 = new XDate(0, true);
- var xdate2 = new XDate(0, false);
- return xdate1.hasTimezone() && !xdate2.hasTimezone();
-});
-
test("getTimezoneOffset", function() {
var date = new Date();
var xdate1 = new XDate(+date);
View
4 test/utilities.js
@@ -22,14 +22,14 @@ test("valid", function() {
});
-test("toDate, hasTimezone=yes", function() {
+test("toDate, hasLocalTimezone=yes", function() {
var d = new Date(2012, 5, 8);
var xdate = new XDate(d);
return +xdate.toDate() == +d;
});
-test("toDate, hasTimezone=no", function() {
+test("toDate, hasLocalTimezone=no", function() {
var d = new Date(2012, 5, 8);
var xdate = new XDate(+d, false);
return +xdate.toDate() == +d;
Please sign in to comment.
Something went wrong with that request. Please try again.