From: Tim Wood Date: Wed, 6 Apr 2011 00:33:28 +0000 (-0700) Subject: Added Timezone and trimmed off some bytes. X-Git-Tag: 0.3.2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=509d2c2e107f9bd69b7885db6f2f38a05dde0d87;p=thirdparty%2Fmoment.git Added Timezone and trimmed off some bytes. --- diff --git a/lib/underscore.date.js b/lib/underscore.date.js index a4b8bb603..735a622e0 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -12,22 +12,22 @@ // assign variables here so they can be overwritten for i18n or customization var self = this, _d, wordsMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], - wordsMonthsShort = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], + wordsMonthsShort = _.map(wordsMonths, firstThreeLetters), wordsWeekdays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"], - wordsWeekdaysShort = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], + wordsWeekdaysShort = _.map(wordsWeekdays, firstThreeLetters), wordsTimeAgo = { future: "in %s", past: "%s ago", - s: "less than a minute", - m: "about a minute", + s: "seconds", + m: "a minute", mm: "%d minutes", - h: "about an hour", - hh: "about %d hours", + h: "an hour", + hh: "%d hours", d: "a day", dd: "%d days", - M: "about a month", + M: "a month", MM: "%d months", - y: "about a year", + y: "a year", yy: "%d years" }, createOrdinal = function(number) { @@ -37,7 +37,12 @@ (b === 2) ? 'nd' : (b === 3) ? 'rd' : 'th'; }; - + // function to return first three characters for minification of wordsMonthsShort + + // wordsWeekdaysShort arrays. + function firstThreeLetters(input){ + return input.slice(0,3); + } + // left zero fill a number // see http://jsperf.com/left-zero-filling for performance comparison function leftZeroFill(number, targetLength) { @@ -88,12 +93,7 @@ // array = new Date(array).getTime() // string = new Date(string).getTime() function makeInputMilliseconds(input){ - return input === undefined ? new Date().getTime() : - !isNaN(input) ? input : - input instanceof _Date ? input.date.getTime() : - _.isDate(input) ? input.getTime() : - _.isArray(input) && input.length > 2 ? dateFromArray(input).getTime() : - new Date(input).getTime(); + return isNaN(input) ? makeInputDate(input).getTime() : input; } // convert any input to a date @@ -132,7 +132,9 @@ currentHours = date.getHours(), currentMinutes = date.getMinutes(), currentSeconds = date.getSeconds(), - charactersToReplace = /(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g; + currentString = date.toString(), + charactersToReplace = /(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g, + nonuppercaseLetters = /[^A-Z]/g; // check if the character is a format // return formatted string or non string. // @@ -220,6 +222,15 @@ return currentSeconds; case 'ss' : return leftZeroFill(currentSeconds, 2); + // TIMEZONE + case 'z' : + return replaceFunction('zz').replace(nonuppercaseLetters, ''); + case 'zz' : + a = currentString.indexOf('('); + if (a > -1) { + return currentString.slice(a + 1, currentString.indexOf(')')); + } + return currentString.slice(currentString.indexOf(':')).replace(nonuppercaseLetters, ''); // DEFAULT default : return input.replace("\\", ""); @@ -236,7 +247,7 @@ return dateAddRemove(this, input, -1); } - _Date.prototype.customize = function(input) { + /*_Date.prototype.customize = function(input) { var inputWeekdays = input.weekdays, inputWeekdaysShort = input.weekdaysShort, inputMonths = input.months, @@ -261,8 +272,47 @@ if (inputOrdinal && _.isFunction(inputOrdinal)) { createOrdinal = inputOrdinal; } + }*/ + + _Date.prototype.customize = function(input) { + var inputWeekdays = input.weekdays, + inputWeekdaysShort = input.weekdaysShort, + inputMonths = input.months, + inputMonthsShort = input.monthsShort, + inputRelativeTime = input.relativeTime, + inputOrdinal = input.ordinal; + if (inputWeekdays) { + _.extend(wordsWeekdays, inputWeekdays); + } + if (inputWeekdaysShort) { + _.extend(wordsWeekdaysShort, inputWeekdaysShort); + } + if (inputMonths) { + _.extend(wordsMonths, inputMonths); + } + if (inputMonthsShort) { + _.extend(wordsMonthsShort, inputMonthsShort); + } + if (inputRelativeTime) { + _.extend(wordsTimeAgo, inputRelativeTime); + } + if (inputOrdinal && _.isFunction(inputOrdinal)) { + createOrdinal = inputOrdinal; + } } + /*_Date.prototype.customize = function(input) { + var inputOrdinal = input.ordinal; + _.extend(wordsWeekdays, input.weekdays); + _.extend(wordsWeekdaysShort, input.weekdaysShort); + _.extend(wordsMonths, input.months); + _.extend(wordsMonthsShort, input.monthsShort); + _.extend(wordsTimeAgo, input.relativeTime); + if (inputOrdinal && _.isFunction(inputOrdinal)) { + createOrdinal = inputOrdinal; + } + }*/ + function msApart(time, now) { return makeInputMilliseconds(time) - makeInputMilliseconds(now); } diff --git a/test/customizePartial.html b/test/customizePartial.html new file mode 100644 index 000000000..2c2c9361c --- /dev/null +++ b/test/customizePartial.html @@ -0,0 +1,18 @@ + + + + Underscore.date customization test suite + + + + + + + + +

Underscore.date customization test suite

+

+

+
    + + diff --git a/test/customizePartial.js b/test/customizePartial.js new file mode 100644 index 000000000..12c3601e9 --- /dev/null +++ b/test/customizePartial.js @@ -0,0 +1,60 @@ +$(function() { + + module("Customize"); + + _.date().customize({ + months : ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"], + weekdays : ["domingo", "lunes", "martes", "miércoles", "jueves", "viernes", "sábado"], + relativeTime : { + future: "%s from now", + past: "%s ago", + s: "moments", + M: "a month", + MM: "%d months", + y: "a year", + yy: "%d years" + }, + ordinal : function(number) { + return 'o'; + } + }); + + test("_date.format()", 14, function() { + var _date = _.date(new Date(2010, 1, 14, 15, 25, 50, 125)); + equal(_date.format("dddd, MMMM Do YYYY, h:mm:ss a"), "domingo, febrero 14o 2010, 3:25:50 pm"); + equal(_date.format("ddd, hA"), "Sun, 3PM"); + equal(_date.format("M Mo MM MMMM MMM"), "2 2o 02 febrero Feb"); + equal(_date.format("YYYY YY"), "2010 10"); + equal(_date.format("D Do DD"), "14 14o 14"); + equal(_date.format("d do dddd ddd"), "0 0o domingo Sun"); + equal(_date.format("DDD DDDo DDDD"), "45 45o 045"); + equal(_date.format("w wo ww"), "8 8o 08"); + equal(_date.format("h hh"), "3 03"); + equal(_date.format("H HH"), "15 15"); + equal(_date.format("m mm"), "25 25"); + equal(_date.format("s ss"), "50 50"); + equal(_date.format("a A"), "pm PM"); + equal(_date.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45o day of the year"); + }); + + test("_.date().relative()", 11, function() { + var start = _.date([2007, 1, 28]); + equal(start.from(_.date([2007, 1, 28]).add({s:30}), true), "moments"); + equal(start.from(_.date([2007, 1, 28]).add({s:60}), true), "a minute"); + equal(start.from(_.date([2007, 1, 28]).add({m:5}), true), "5 minutes"); + equal(start.from(_.date([2007, 1, 28]).add({h:1}), true), "an hour"); + equal(start.from(_.date([2007, 1, 28]).add({h:5}), true), "5 hours"); + equal(start.from(_.date([2007, 1, 28]).add({d:1}), true), "a day"); + equal(start.from(_.date([2007, 1, 28]).add({d:5}), true), "5 days"); + equal(start.from(_.date([2007, 1, 28]).add({M:1}), true), "a month"); + equal(start.from(_.date([2007, 1, 28]).add({M:5}), true), "5 months"); + equal(start.from(_.date([2007, 1, 28]).add({y:1}), true), "a year"); + equal(start.from(_.date([2007, 1, 28]).add({y:5}), true), "5 years"); + }); + + test("_.fromNow()", 2, function() { + equal(_.date(30000).from(0), "moments from now"); + equal(_.date(0).from(30000), "moments ago"); + }); + +}); diff --git a/test/date.js b/test/date.js index 571eebfc6..3f660fbd9 100644 --- a/test/date.js +++ b/test/date.js @@ -15,7 +15,7 @@ $(function() { deepEqual(_.date(), _.now(), "_.now returns wrapped date"); }); - test("_.date().format()", 14, function() { + test("_.date().format()", 15, function() { var dateTest = new Date(2010, 1, 14, 15, 25, 50, 125); var _date = _.date(dateTest); @@ -32,6 +32,7 @@ $(function() { equal(_date.format("m mm"), "25 25"); equal(_date.format("s ss"), "50 50"); equal(_date.format("a A"), "pm PM"); + equal(_date.format("z zz"), "PST Pacific Standard Time"); equal(_date.format("t\\he DDDo \\d\\ay of t\\he ye\\ar"), "the 45th day of the year"); }); @@ -46,16 +47,16 @@ $(function() { test("_.date().from() -- suffixless", 11, function() { var start = _.date([2007, 1, 28]); - equal(start.from(_.date([2007, 1, 28]).add({s:30}), true), "less than a minute"); - equal(start.from(_.date([2007, 1, 28]).add({s:60}), true), "about a minute"); + equal(start.from(_.date([2007, 1, 28]).add({s:30}), true), "seconds"); + equal(start.from(_.date([2007, 1, 28]).add({s:60}), true), "a minute"); equal(start.from(_.date([2007, 1, 28]).add({m:5}), true), "5 minutes"); - equal(start.from(_.date([2007, 1, 28]).add({h:1}), true), "about an hour"); - equal(start.from(_.date([2007, 1, 28]).add({h:5}), true), "about 5 hours"); + equal(start.from(_.date([2007, 1, 28]).add({h:1}), true), "an hour"); + equal(start.from(_.date([2007, 1, 28]).add({h:5}), true), "5 hours"); equal(start.from(_.date([2007, 1, 28]).add({d:1}), true), "a day"); equal(start.from(_.date([2007, 1, 28]).add({d:5}), true), "5 days"); - equal(start.from(_.date([2007, 1, 28]).add({M:1}), true), "about a month"); + equal(start.from(_.date([2007, 1, 28]).add({M:1}), true), "a month"); equal(start.from(_.date([2007, 1, 28]).add({M:5}), true), "5 months"); - equal(start.from(_.date([2007, 1, 28]).add({y:1}), true), "about a year"); + equal(start.from(_.date([2007, 1, 28]).add({y:1}), true), "a year"); equal(start.from(_.date([2007, 1, 28]).add({y:5}), true), "5 years"); }); @@ -71,12 +72,12 @@ $(function() { }); test("_.date().from() -- with suffix", 2, function() { - equal(_.date(30000).from(0), "in less than a minute"); - equal(_.date(0).from(30000), "less than a minute ago"); + equal(_.date(30000).from(0), "in seconds"); + equal(_.date(0).from(30000), "seconds ago"); }); test("_.date().fromNow()", 2, function() { - equal(_.now().add({s:30}).fromNow(), "in less than a minute"); + equal(_.now().add({s:30}).fromNow(), "in seconds"); equal(_.now().add({d:5}).fromNow(), "in 5 days"); });