From: Tim Wood Date: Fri, 2 Mar 2012 00:00:22 +0000 (+1200) Subject: Adding utc mode #158 X-Git-Tag: 1.5.0~17 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3e53d19360be9837cc6890586ad7c50f3ce1b86d;p=thirdparty%2Fmoment.git Adding utc mode #158 --- diff --git a/moment.js b/moment.js index fede61db0..b1eb831ef 100644 --- a/moment.js +++ b/moment.js @@ -30,8 +30,9 @@ shortcuts = 'Month|Date|Hours|Minutes|Seconds|Milliseconds'.split('|'); // Moment prototype object - function Moment(date) { + function Moment(date, isUTC) { this._d = date; + this._isUTC = !!isUTC; } // left zero fill a number @@ -89,9 +90,8 @@ } // format date using native date object - function formatDate(date, inputString) { - var m = new Moment(date), - currentMonth = m.month(), + function formatMoment(m, inputString) { + var currentMonth = m.month(), currentDate = m.date(), currentYear = m.year(), currentDay = m.day(), @@ -192,7 +192,7 @@ case 'zz' : // depreciating 'zz' fall through to 'z' case 'z' : - return (date.toString().match(timezoneRegex) || [''])[0].replace(nonuppercaseLetters, ''); + return (m._d.toString().match(timezoneRegex) || [''])[0].replace(nonuppercaseLetters, ''); case 'Z' : return (currentZone < 0 ? '-' : '+') + leftZeroFill(~~(Math.abs(currentZone) / 60), 2) + ':' + leftZeroFill(~~(Math.abs(currentZone) % 60), 2); case 'ZZ' : @@ -203,7 +203,7 @@ case 'LLL' : case 'LLLL' : case 'LT' : - return formatDate(date, moment.longDateFormat[input]); + return formatMoment(m, moment.longDateFormat[input]); // DEFAULT default : return input.replace(/(^\[)|(\\)|\]$/g, ""); @@ -353,7 +353,7 @@ curScore; for (i = 0; i < formats.length; i++) { curDate = makeDateFromStringAndFormat(string, formats[i]); - curScore = compareArrays(inputParts, formatDate(curDate, formats[i]).match(inputCharacters)); + curScore = compareArrays(inputParts, formatMoment(new Moment(curDate), formats[i]).match(inputCharacters)); if (curScore < scoreToBeat) { scoreToBeat = curScore; output = curDate; @@ -407,6 +407,14 @@ return new Moment(date); }; + // creating with utc + moment.utc = function(input, format) { + if (isArray(input)) { + return new Moment(new Date(Date.UTC.apply({}, input)), true); + } + return (format && input) ? moment(input + ' 0', format + ' Z').utc() : moment(input).utc(); + }; + // version number moment.version = VERSION; @@ -541,9 +549,18 @@ return this._d; }, + utc : function () { + this._isUTC = true; + return this; + }, + + local : function () { + this._isUTC = false; + return this; + }, + format : function (inputString) { - return inputString ? formatDate(this._d, inputString) : - formatDate(this._d, moment.defaultFormat); + return formatMoment(this, inputString ? inputString : moment.defaultFormat); }, add : function (input, val) { @@ -636,7 +653,7 @@ }, zone : function () { - return this._d.getTimezoneOffset(); + return this._isUTC ? 0 : this._d.getTimezoneOffset(); }, daysInMonth : function () { @@ -647,11 +664,12 @@ // helper for adding shortcuts function makeShortcut(name, key) { moment.fn[name] = function (input) { + var utc = this._isUTC ? 'UTC' : ''; if (input != null) { - this._d['set' + key](input); + this._d['set' + utc + key](input); return this; } else { - return this._d['get' + key](); + return this._d['get' + utc + key](); } }; } diff --git a/test/moment/utc.js b/test/moment/utc.js new file mode 100644 index 000000000..3fc62099e --- /dev/null +++ b/test/moment/utc.js @@ -0,0 +1,43 @@ +var moment = require("../../moment"); + +exports.utc = { + "utc and local" : function(test) { + test.expect(5); + + var m = moment(Date.UTC(2011, 1, 2, 3, 4, 5, 6)); + m.utc(); + // utc + test.equal(m.date(), 2, "the day should be correct for utc"); + test.equal(m.hours(), 3, "the hours should be correct for utc"); + + // local + m.local(); + if (moment().zone() > 180) { + test.equal(m.date(), 1, "the day should be correct for utc"); + } else { + test.equal(m.date(), 2, "the day should be correct for utc"); + } + var expected = (24 + 3 - Math.floor(moment().zone() / 60)) % 24; + test.equal(m.hours(), expected, "the hours (" + m.hours() + ") should be correct for utc"); + test.equal(moment().utc().zone(), 0, "timezone in utc should always be zero"); + test.done(); + }, + + "creating with utc" : function(test) { + test.expect(6); + + var m = moment.utc([2011, 1, 2, 3, 4, 5, 6]); + test.equal(m.date(), 2, "the day should be correct for utc array"); + test.equal(m.hours(), 3, "the hours should be correct for utc array"); + + m = moment.utc("2011-02-02 3:04:05", "YYYY-MM-DD HH:mm:ss"); + test.equal(m.date(), 2, "the day should be correct for utc parsing format"); + test.equal(m.hours(), 3, "the hours should be correct for utc parsing format"); + + m = moment.utc("2011-02-02T03:04:05+00:00"); + test.equal(m.date(), 2, "the day should be correct for utc parsing iso"); + test.equal(m.hours(), 3, "the hours should be correct for utc parsing iso"); + + test.done(); + } +};