1 // ========================================================================== 2 // Project: SproutCore - JavaScript Application Framework 3 // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 // Portions ©2008-2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 8 SC.DateTime.mixin( 9 /** @scope SC.DateTime */ { 10 11 /** 12 @private 13 14 Called on `document.ready`. 15 16 Because localizations may have been modified by an application developer, 17 we need to wait for the ready event to actually evaluate the localizations. 18 */ 19 _setup: function() { 20 SC.DateTime.dayNames = SC.String.w(SC.String.loc('_SC.DateTime.dayNames')); 21 SC.DateTime.abbreviatedDayNames = SC.String.w(SC.String.loc('_SC.DateTime.abbreviatedDayNames')); 22 SC.DateTime.monthNames = SC.String.w(SC.String.loc('_SC.DateTime.monthNames')); 23 SC.DateTime.abbreviatedMonthNames = SC.String.w(SC.String.loc('_SC.DateTime.abbreviatedMonthNames')); 24 SC.DateTime.AMPMNames = SC.String.w(SC.String.loc('_SC.DateTime.AMPMNames')); 25 }, 26 27 /** 28 @private 29 30 Elapsed string formatting override, as it depends on localization. 31 32 @see SC.DateTime#toFormattedString 33 */ 34 __getElapsedStringFormat: function(start, timezone) { 35 36 var elapsed = Math.floor((+new Date() - this._date.getTime() - (timezone * 60000)) / 1000); 37 var future = !!(elapsed < 0) ? "In" : "Ago"; 38 var interval = 0; 39 var intervalType = "now"; 40 41 if(Math.abs(elapsed) > 31536000) { // 60 * 60 * 24 * 365 - years 42 interval = Math.floor(Math.abs(elapsed / 31536000)); 43 intervalType = 'year'; 44 } else if(Math.abs(elapsed) > 2678400) { // 60 * 60 * 24 * 31 - months 45 interval = Math.floor(Math.abs(elapsed / 2678400)); 46 intervalType = 'month'; 47 } else if(Math.abs(elapsed) > 604800) { // 60 * 60 * 24 * 7 - weeks 48 interval = Math.floor(Math.abs(elapsed / 604800)); 49 intervalType = 'week'; 50 } else if(Math.abs(elapsed) > 86400) { // 60 * 60 * 24 - day 51 interval = Math.floor(Math.abs(elapsed / 86400)); 52 intervalType = 'day'; 53 } else if(Math.abs(elapsed) > 3600) { // 60 * 60 - hour 54 interval = Math.floor(Math.abs(elapsed / 3600)); 55 intervalType = 'hour'; 56 } else if(Math.abs(elapsed) > 60) { // 60 - minute 57 interval = Math.floor(Math.abs(elapsed / 60)); 58 intervalType = 'minute'; 59 } else if(Math.abs(elapsed) > 0) { // second 60 interval = Math.abs(elapsed); 61 intervalType = 'second'; 62 } 63 64 var formatString = '_SC.DateTime.now'; 65 if(intervalType !== 'now') { 66 formatString = "_SC.DateTime.%@%@%@".fmt(intervalType, interval > 1 ? "s" : "", future); 67 } 68 return formatString.loc().replace('%e', interval); 69 }, 70 71 }); 72 73 jQuery(document).ready(function() { 74 SC.DateTime._setup(); 75 }); 76