Permalink
Browse files

fixed relatize date

  • Loading branch information...
rsms committed Apr 22, 2010
1 parent ffbbe98 commit 0f9604e776c845f3d429ad0fe9aa152294e48bc7
Showing with 43 additions and 4 deletions.
  1. +10 −3 client/lib/jquery.relatize_date.js
  2. +33 −1 client/lib/std-additions/date.js
@@ -24,13 +24,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(function($) {
$.fn.relatizeDate = function() {
return $(this).each(function() {
- if ($(this).hasClass( 'relatized' )) return;
- $(this).text( $.relatizeDate(this) ).addClass( 'relatized' );
+ var $q = $(this);
+ if ($q.hasClass('date-relatized')) return;
+ $q.text($.relatizeDate($q)).addClass('date-relatized');
});
};
$.relatizeDate = function(element) {
- return $.relatizeDate.timeAgoInWords( new Date($(element).text()) );
+ var n, value = $(element).text(), date;
+ if (!isNaN((n = Number(value)))) {
+ date = Date.fromUTCTimestamp(n);
+ } else {
+ date = new Date(value);
+ }
+ return $.relatizeDate.timeAgoInWords(date);
};
// shortcut
@@ -1,2 +1,34 @@
-
Date.distantFuture = new Date((new Date()).getTime()+9000000000000);
+Date.distantPast = new Date(0);
+Date.timezoneOffset = 0; // Standard offset in milliseconds
+Date.timezoneDSTOffset = 0; // DST offset in milliseconds
+
+// timestamp should be in milliseconds since the epoch, UTC
+Date.fromUTCTimestamp = function(timestamp) {
+ timestamp = parseInt(timestamp);
+ if (isNaN(timestamp))
+ throw new Error('Date.fromUTCTimestamp failed to parse integer argument');
+ return new Date(timestamp+Date.timezoneOffset);
+};
+
+Date.currentUTCTimestamp = function() {
+ return (new Date()).toUTCTimestamp();
+};
+
+Date.recalculateOffsets = function() {
+ var rightNow = new Date(),
+ date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0),
+ date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0),
+ temp = date1.toGMTString(),
+ date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1)),
+ date4;
+ temp = date2.toGMTString();
+ date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")-1));
+ // standard offset, not counting DST
+ Date.timezoneOffset = (date3 - date1);
+ // offset including DST
+ Date.timezoneDSTOffset = (date4 - date2);
+};
+
+// calculate timezone offset at load-time
+Date.recalculateOffsets();

0 comments on commit 0f9604e

Please sign in to comment.