From 53c2089b62c3175e374892a12d018114a78bb089 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Wed, 10 Sep 2014 14:19:44 +0200 Subject: [PATCH] Improved show times. --- .../app/lib/app_post/i18n.js.coffee | 17 ++++++++++++++++- .../app/lib/app_post/pretty_date.js.coffee | 3 +++ app/models/translation.rb | 8 ++++++++ public/assets/tests/ui.js | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) 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 5840c1c8b..78f780e4c 100644 --- a/app/assets/javascripts/app/lib/app_post/i18n.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/i18n.js.coffee @@ -24,6 +24,11 @@ class App.i18n _instance ?= new _i18nSingleton _instance.timestamp( args, offset ) + @translateDate: ( args, offset = 0 ) -> + if _instance == undefined + _instance ?= new _i18nSingleton + _instance.date( args, offset ) + @get: -> if _instance == undefined _instance ?= new _i18nSingleton @@ -44,6 +49,7 @@ class _i18nSingleton extends Spine.Module constructor: ( locale ) -> @map = {} + @dateFormat = 'yyyy-mm-dd' @timestampFormat = 'yyyy-mm-dd HH:MM' # observe if text has been translated @@ -114,6 +120,10 @@ class _i18nSingleton extends Spine.Module if data.timestampFormat @timestampFormat = data.timestampFormat + # set date format + if data.dateFormat + @dateFormat = data.dateFormat + # load translation collection for object in data.list @@ -176,7 +186,13 @@ class _i18nSingleton extends Spine.Module .replace(/>/g, '>') .replace(/\x22/g, '"') + date: ( time, offset ) => + @convert(time, offset, @dateFormat) + timestamp: ( time, offset ) => + @convert(time, offset, @timestampFormat) + + convert: ( time, offset, format ) => s = ( num, digits ) -> while num.toString().length < digits num = "0" + num @@ -194,7 +210,6 @@ class _i18nSingleton extends Spine.Module S = timeObject.getSeconds() M = timeObject.getMinutes() H = timeObject.getHours() - format = @timestampFormat format = format.replace /dd/, s( d, 2 ) format = format.replace /d/, d format = format.replace /mm/, s( m, 2 ) diff --git a/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee b/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee index 223947c35..a39e6ad25 100644 --- a/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee +++ b/app/assets/javascripts/app/lib/app_post/pretty_date.js.coffee @@ -28,6 +28,9 @@ class App.PrettyDate diff = diff.toString().replace('-', '') diff = parseFloat(diff) + if direction is 'past' && !escalation && diff > ( 60 * 60 * 24 * 14 ) + return App.i18n.translateDate(time) + # days string = '' count = 0 diff --git a/app/models/translation.rb b/app/models/translation.rb index 08901ed5e..79bc2a2b2 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -31,9 +31,17 @@ class Translation < ApplicationModel :de => 'dd.mm.yyyy HH:MM', } timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default + + date_map_default = 'yyyy-mm-dd' + date_map = { + :de => 'dd.mm.yyyy', + } + date = date_map[ locale.to_sym ] || date_map_default + return { :list => list, :timestampFormat => timestamp, + :dateFormat => date, } end diff --git a/public/assets/tests/ui.js b/public/assets/tests/ui.js index ef0004024..35da3e8c5 100644 --- a/public/assets/tests/ui.js +++ b/public/assets/tests/ui.js @@ -43,6 +43,19 @@ test( "check pretty date", function() { result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 10.5 ) ); equal( result, '10 days ago', '10.5 days') + result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 30 ) ); + var pastDate = new Date(current - ( 60000 * 60 * 24 * 30 )) + var dd = pastDate.getDate(); + if( dd<10 ) { + dd = '0' + dd + } + var mm = pastDate.getMonth() + 1; + if( mm<10 ) { + mm = '0' + mm + } + var yyyy = pastDate.getFullYear(); + equal( result, yyyy+'-'+mm+'-'+dd, '30 days') + // future current = new Date() result = App.PrettyDate.humanTime( current ); @@ -72,5 +85,10 @@ test( "check pretty date", function() { result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 2.5 ) ); equal( result, 'in 2 days 12 hours', 'in 2.5 days') + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 5.5 ) ); + equal( result, 'in 5 days 12 hours', 'in 30.5 days') + + result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 30.5 ) ); + equal( result, 'in 30 days', 'in 30.5 days') });