From 28dfddd612117b0342d0e1e134991e52a49e7991 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 1 Jun 2014 14:17:08 +0200 Subject: [PATCH] Fixed App.i18n.translateTimestamp() to work on browser with different timezones. --- .../app/lib/app_post/i18n.js.coffee | 11 +++++++--- public/assets/tests/core.js | 22 ++++++++++--------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee index 211766cba..af9add9e2 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee @@ -19,10 +19,10 @@ class App.i18n _instance ?= new _i18nSingleton _instance.translateInline( string, args ) - @translateTimestamp: ( args ) -> + @translateTimestamp: ( args, offset = 0 ) -> if _instance == undefined _instance ?= new _i18nSingleton - _instance.timestamp( args ) + _instance.timestamp( args, offset ) @get: -> if _instance == undefined @@ -170,13 +170,18 @@ class _i18nSingleton extends Spine.Module .replace(/>/g, '>') .replace(/\x22/g, '"') - timestamp: ( time ) => + timestamp: ( time, offset ) => s = ( num, digits ) -> while num.toString().length < digits num = "0" + num num timeObject = new Date(time) + + # add timezone diff, needed for unit tests + if offset + timeObject = new Date( timeObject.getTime() + (timeObject.getTimezoneOffset() * 60000) ) + d = timeObject.getDate() m = timeObject.getMonth() + 1 y = timeObject.getFullYear() diff --git a/public/assets/tests/core.js b/public/assets/tests/core.js index a6f1898b9..d72e13fff 100644 --- a/public/assets/tests/core.js +++ b/public/assets/tests/core.js @@ -207,17 +207,17 @@ App.Delay.set( function() { test( "interval - test 1 - 1/2", function() { // check - equal( window.testInterval1, 3, 'interval - test 1' ); + equal( window.testInterval1, 4, 'interval - test 1' ); App.Interval.clear('interval-test1') }); }, - 4400 + 5200 ); App.Delay.set( function() { test( "interval - test 1 - 2/2", function() { // check - equal( window.testInterval1, 3, 'interval - test after clear' ); + equal( window.testInterval1, 4, 'interval - test after clear' ); }); }, 6500 @@ -237,17 +237,17 @@ App.Delay.set( function() { test( "interval - test 2 - 1/2", function() { // check - equal( window.testInterval2, 3, 'interval - test 2' ); + equal( window.testInterval2, 4, 'interval - test 2' ); App.Interval.clearLevel('someLevel') }); }, - 4500 + 5200 ); App.Delay.set( function() { test( "interval - test 2 - 2/2", function() { // check - equal( window.testInterval2, 3, 'interval - test 2 - after clear' ); + equal( window.testInterval2, 4, 'interval - test 2 - after clear' ); }); }, 6900 @@ -265,8 +265,10 @@ test( "i18n", function() { translated = App.i18n.translateContent('//*äöüß'); equal( translated, '<test&now>//*äöüß', 'de - //*äöüß' ); - var timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z'); - equal( timestamp, '06.11.2012 22:07', 'de - timestamp translated correctly' ); + var time_local = new Date(); + var offset = time_local.getTimezoneOffset(); + var timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z', offset); + equal( timestamp, '06.11.2012 21:07', 'de - timestamp translated correctly' ); // en App.i18n.set('en'); @@ -276,8 +278,8 @@ test( "i18n", function() { translated = App.i18n.translateContent(''); equal( translated, '<test&now>', 'en - ' ); - timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z'); - equal( timestamp, '2012-11-06 22:07', 'en - timestamp translated correctly' ); + timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z', offset); + equal( timestamp, '2012-11-06 21:07', 'en - timestamp translated correctly' ); }); // events