From 569683b2ba4ae490a3724f4960e5d16a4d6d4b7f Mon Sep 17 00:00:00 2001 From: Tim Wood Date: Fri, 8 Apr 2011 10:30:52 -0700 Subject: [PATCH] Added documentation for the addition of _.date().format('z zz'). Changed some parseint math from 'x|0' to '~~x' based of performance results from http://jsperf.com/floor-vs-bitwise-or-vs-parseint/4 --- README.markdown | 19 +++++++++++ lib/underscore.date.js | 70 ++++++++++++++++++++------------------ lib/underscore.date.min.js | 4 +-- 3 files changed, 57 insertions(+), 36 deletions(-) diff --git a/README.markdown b/README.markdown index 96bc726d7..a84a97122 100644 --- a/README.markdown +++ b/README.markdown @@ -329,6 +329,25 @@ The formats are created by creating a string of replacable characters. ss 00 01 ... 58 59 + + Timezone + + + z + EST CST ... MST PST + + + zz + + Eastern Standard Time ... Pacific Standard Time

+ NOTE: Internet Explorer uses a different implementation of + Date.toString(), so we are unable to retrieve the full string + of the timezone, and will fall back to `z`.

+ So:
+ Firefox, Chrome, Safari, etc. == 'Eastern Standard Time'
+ Internet Explorer, etc. == 'EST' + + diff --git a/lib/underscore.date.js b/lib/underscore.date.js index 5ea435ca3..c077254c4 100644 --- a/lib/underscore.date.js +++ b/lib/underscore.date.js @@ -3,12 +3,17 @@ // (c) 2011 Tim Wood // Underscore.date is freely distributable under the terms of the MIT license. // -// Version 0.3.1 +// Version 0.3.2 /*global _:false */ (function(Date, _, undefined){ + // function to return first three characters for minification of wordsMonthsShort + + // wordsWeekdaysShort arrays. + function firstThreeLetters(input){ + return input.slice(0, 3); + } // 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"], @@ -32,16 +37,11 @@ }, createOrdinal = function(number) { var b = number % 10; - return ((number % 100 / 10 | 0) === 1) ? 'th' : + return (~~ (number % 100 / 10) === 1) ? 'th' : (b === 1) ? 'st' : (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 @@ -62,7 +62,7 @@ (input.h || 0) * 36e5 + // 1000 * 60 * 60 (input.d || 0) * 864e5 + // 1000 * 60 * 60 * 24 (input.w || 0) * 6048e5, // 1000 * 60 * 60 * 24 * 7 - M = (input.M || 0) + + M = (input.M || 0) + (input.y || 0) * 12, currentDate; if (ms) { @@ -85,17 +85,6 @@ return new Date(input[0], input[1] || 0, input[2] || 1, input[3] || 0, input[4] || 0, input[5] || 0, input[6] || 0); } - // convert any input to milliseconds - // - // undefined = _.now() - // number = number - // date = date.gettime() - // array = new Date(array).getTime() - // string = new Date(string).getTime() - function makeInputMilliseconds(input){ - return isNaN(input) ? makeInputDate(input).getTime() : input; - } - // convert any input to a date // // undefined = _.now() @@ -111,16 +100,29 @@ new Date(input); } + // convert any input to milliseconds + // + // undefined = _.now() + // number = number + // date = date.gettime() + // array = new Date(array).getTime() + // string = new Date(string).getTime() + function makeInputMilliseconds(input){ + return isNaN(input) ? makeInputDate(input).getTime() : input; + } + // helper function for _.relativeTime function substituteTimeAgo(string, number) { return wordsTimeAgo[string].replace(/%d/i, number || 1); } + // _Date prototype object function _Date(input) { this.date = makeInputDate(input); return this; } + _Date.prototype.format = function(inputString) { // shortcuts to this and getting time functions // done to save bytes in minification @@ -167,7 +169,7 @@ case 'DDD' : a = new Date(currentYear, currentMonth, currentDate); b = new Date(currentYear, 0, 1); - return ((a - b) / 864e5) + 1.5 | 0; + return ~~ (((a - b) / 864e5) + 1.5); case 'DDDo' : a = replaceFunction('DDD'); return a + createOrdinal(a); @@ -186,7 +188,7 @@ case 'w' : a = new Date(currentYear, currentMonth, currentDate - currentDay + 5); b = new Date(a.getFullYear(), 0, 4); - return (a - b) / 864e5 / 7 + 1.5 | 0; + return ~~ ((a - b) / 864e5 / 7 + 1.5); case 'wo' : a = replaceFunction('w'); return a + createOrdinal(a); @@ -237,15 +239,15 @@ } } return inputString.replace(charactersToReplace, replaceFunction); - } + }; _Date.prototype.add = function(input) { return dateAddRemove(this, input, 1); - } + }; _Date.prototype.subtract = function(input) { return dateAddRemove(this, input, -1); - } + }; _Date.prototype.customize = function(input) { var inputOrdinal = input.ordinal; @@ -257,7 +259,7 @@ if (inputOrdinal && _.isFunction(inputOrdinal)) { createOrdinal = inputOrdinal; } - } + }; function msApart(time, now) { return makeInputMilliseconds(time) - makeInputMilliseconds(now); @@ -269,17 +271,17 @@ hours = minutes / 60, days = hours / 24, years = days / 365; - return seconds < 45 && substituteTimeAgo('s', seconds | 0) || + return seconds < 45 && substituteTimeAgo('s', ~~ seconds) || seconds < 90 && substituteTimeAgo('m') || - minutes < 45 && substituteTimeAgo('mm', minutes | 0) || + minutes < 45 && substituteTimeAgo('mm', ~~ minutes) || minutes < 90 && substituteTimeAgo('h') || - hours < 24 && substituteTimeAgo('hh', hours | 0) || + hours < 24 && substituteTimeAgo('hh', ~~ hours) || hours < 48 && substituteTimeAgo('d') || - days < 25 && substituteTimeAgo('dd', days | 0) || + days < 25 && substituteTimeAgo('dd', ~~ days) || days < 45 && substituteTimeAgo('M') || - days < 350 && substituteTimeAgo('MM', (days + 15) / 30 | 0) || + days < 350 && substituteTimeAgo('MM', ~~ ((days + 15) / 30)) || years < 2 && substituteTimeAgo('y') || - substituteTimeAgo('yy', years | 0); + substituteTimeAgo('yy', ~~ years); } _Date.prototype.from = function(time, withoutSuffix, asMilliseconds) { @@ -288,15 +290,15 @@ return asMilliseconds ? difference : withoutSuffix ? relativeTime(difference) : string.replace(/%s/i, relativeTime(difference)); - } + }; _Date.prototype.fromNow = function(withoutSuffix, asMilliseconds) { return this.from(_.now(), withoutSuffix, asMilliseconds); - } + }; _Date.prototype.isLeapYear = function() { return _.isLeapYear(this.date.getFullYear()); - } + }; // underscore mixins _d = { diff --git a/lib/underscore.date.min.js b/lib/underscore.date.min.js index 0bfd1e4cd..e9e4c673d 100644 --- a/lib/underscore.date.min.js +++ b/lib/underscore.date.min.js @@ -3,6 +3,6 @@ // (c) 2011 Tim Wood // Underscore.date is freely distributable under the terms of the MIT license. // -// Version 0.3.1 +// Version 0.3.2 -(function(a,b,c){function t(a){var b=Math.abs(a)/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&q("s",b|0)||b<90&&q("m")||c<45&&q("mm",c|0)||c<90&&q("h")||d<24&&q("hh",d|0)||d<48&&q("d")||e<25&&q("dd",e|0)||e<45&&q("M")||e<350&&q("MM",(e+15)/30|0)||f<2&&q("y")||q("yy",f|0)}function s(a,b){return o(a)-o(b)}function r(a){this.date=p(a);return this}function q(a,b){return j[a].replace(/%d/i,b||1)}function p(d){return d===c?new a:d instanceof r?d.date:b.isDate(d)?d:b.isArray(d)&&d.length>2?n(d):new a(d)}function o(d){return d===c?(new a).getTime():isNaN(d)?d instanceof r?d.date.getTime():b.isDate(d)?d.getTime():b.isArray(d)&&d.length>2?n(d).getTime():(new a(d)).getTime():d}function n(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function m(b,c,d){var e=b.date,f=(c.ms||0)+(c.s||0)*1e3+(c.m||0)*6e4+(c.h||0)*36e5+(c.d||0)*864e5+(c.w||0)*6048e5,g=(c.M||0)+(c.y||0)*12,h;f&&e.setMilliseconds(e.getMilliseconds()+f*d),g&&(h=e.getDate(),e.setDate(1),e.setMonth(e.getMonth()+g*d),e.setDate(Math.min((new a(e.getFullYear(),e.getMonth()+1,0)).getDate(),h)));return b}function l(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return n>11?"PM":"AM";case"H":return n;case"HH":return l(n,2);case"h":return n%12||12;case"hh":return l(n%12||12,2);case"m":return o;case"mm":return l(o,2);case"s":return p;case"ss":return l(p,2);default:return b.replace("\\","")}}var c=this.date,d=c.getMonth(),e=c.getDate(),j=c.getFullYear(),m=c.getDay(),n=c.getHours(),o=c.getMinutes(),p=c.getSeconds(),q=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?)/g;return b.replace(q,r)},r.prototype.add=function(a){return m(this,a,1)},r.prototype.subtract=function(a){return m(this,a,-1)},r.prototype.customize=function(a){var c=a.weekdays,d=a.weekdaysShort,e=a.months,l=a.monthsShort,m=a.relativeTime,n=a.ordinal;c&&b.isArray(c)&&c.length===7&&(h=c),d&&b.isArray(d)&&d.length===7&&(i=d),e&&b.isArray(e)&&e.length===12&&(f=e),l&&b.isArray(l)&&l.length===12&&(g=l),m&&b.extend(j,m),n&&b.isFunction(n)&&(k=n)},r.prototype.from=function(a,b,c){var d=s(this.date,a),e=d<0?j.past:j.future;return c?d:b?t(d):e.replace(/%s/i,t(d))},r.prototype.fromNow=function(a,c){return this.from(b.now(),a,c)},r.prototype.isLeapYear=function(){return b.isLeapYear(this.date.getFullYear())},e={date:function(a){return new r(a)},now:function(b){return b?(new a).getTime():e.date()},isLeapYear:function(a){return a%4===0&&a%100!==0||a%400===0}},b&&b.mixin&&b.mixin(e)})(Date,_) +(function(a,b,c){function u(a){var b=Math.abs(a)/1e3,c=b/60,d=c/60,e=d/24,f=e/365;return b<45&&r("s",~~b)||b<90&&r("m")||c<45&&r("mm",~~c)||c<90&&r("h")||d<24&&r("hh",~~d)||d<48&&r("d")||e<25&&r("dd",~~e)||e<45&&r("M")||e<350&&r("MM",~~((e+15)/30))||f<2&&r("y")||r("yy",~~f)}function t(a,b){return q(a)-q(b)}function s(a){this.date=p(a);return this}function r(a,b){return k[a].replace(/%d/i,b||1)}function q(a){return isNaN(a)?p(a).getTime():a}function p(d){return d===c?new a:d instanceof s?d.date:b.isDate(d)?d:b.isArray(d)&&d.length>2?o(d):new a(d)}function o(b){return new a(b[0],b[1]||0,b[2]||1,b[3]||0,b[4]||0,b[5]||0,b[6]||0)}function n(b,c,d){var e=b.date,f=(c.ms||0)+(c.s||0)*1e3+(c.m||0)*6e4+(c.h||0)*36e5+(c.d||0)*864e5+(c.w||0)*6048e5,g=(c.M||0)+(c.y||0)*12,h;f&&e.setMilliseconds(e.getMilliseconds()+f*d),g&&(h=e.getDate(),e.setDate(1),e.setMonth(e.getMonth()+g*d),e.setDate(Math.min((new a(e.getFullYear(),e.getMonth()+1,0)).getDate(),h)));return b}function m(a,b){var c=a+"";while(c.length11?"pm":"am";case"A":return n>11?"PM":"AM";case"H":return n;case"HH":return m(n,2);case"h":return n%12||12;case"hh":return m(n%12||12,2);case"m":return o;case"mm":return m(o,2);case"s":return p;case"ss":return m(p,2);case"z":return t("zz").replace(s,"");case"zz":c=q.indexOf("(");if(c>-1)return q.slice(c+1,q.indexOf(")"));return q.slice(q.indexOf(":")).replace(s,"");default:return b.replace("\\","")}}var c=this.date,d=c.getMonth(),e=c.getDate(),f=c.getFullYear(),k=c.getDay(),n=c.getHours(),o=c.getMinutes(),p=c.getSeconds(),q=c.toString(),r=/(\\)?(Mo|MM?M?M?|Do|DDDo|DD?D?D?|dddd?|do?|w[o|w]?|YYYY|YY|a|A|hh?|HH?|mm?|ss?|zz?)/g,s=/[^A-Z]/g;return b.replace(r,t)},s.prototype.add=function(a){return n(this,a,1)},s.prototype.subtract=function(a){return n(this,a,-1)},s.prototype.customize=function(a){var c=a.ordinal;b.extend(i,a.weekdays),b.extend(j,a.weekdaysShort),b.extend(g,a.months),b.extend(h,a.monthsShort),b.extend(k,a.relativeTime),c&&b.isFunction(c)&&(l=c)},s.prototype.from=function(a,b,c){var d=t(this.date,a),e=d<0?k.past:k.future;return c?d:b?u(d):e.replace(/%s/i,u(d))},s.prototype.fromNow=function(a,c){return this.from(b.now(),a,c)},s.prototype.isLeapYear=function(){return b.isLeapYear(this.date.getFullYear())},f={date:function(a){return new s(a)},now:function(b){return b?(new a).getTime():f.date()},isLeapYear:function(a){return a%4===0&&a%100!==0||a%400===0}},b&&b.mixin&&b.mixin(f)})(Date,_) -- 2.47.3