// 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) {
(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) {
// 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
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.
//
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("\\", "");
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,
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);
}
--- /dev/null
+<!DOCTYPE HTML>
+<html>
+ <head>
+ <title>Underscore.date customization test suite</title>
+ <link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
+ <script type="text/javascript" src="vendor/jquery.js"></script>
+ <script type="text/javascript" src="vendor/underscore.js"></script>
+ <script type="text/javascript" src="../lib/underscore.date.js"></script>
+ <script type="text/javascript" src="vendor/qunit.js"></script>
+ <script type="text/javascript" src="customizePartial.js"></script>
+ </head>
+ <body>
+ <h1 id="qunit-header">Underscore.date customization test suite</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ </body>
+</html>
--- /dev/null
+$(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");
+ });
+
+});
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);
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");
});
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");
});
});
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");
});