Improved show times.
This commit is contained in:
parent
e1a7b0f3fb
commit
53c2089b62
4 changed files with 45 additions and 1 deletions
|
@ -24,6 +24,11 @@ class App.i18n
|
||||||
_instance ?= new _i18nSingleton
|
_instance ?= new _i18nSingleton
|
||||||
_instance.timestamp( args, offset )
|
_instance.timestamp( args, offset )
|
||||||
|
|
||||||
|
@translateDate: ( args, offset = 0 ) ->
|
||||||
|
if _instance == undefined
|
||||||
|
_instance ?= new _i18nSingleton
|
||||||
|
_instance.date( args, offset )
|
||||||
|
|
||||||
@get: ->
|
@get: ->
|
||||||
if _instance == undefined
|
if _instance == undefined
|
||||||
_instance ?= new _i18nSingleton
|
_instance ?= new _i18nSingleton
|
||||||
|
@ -44,6 +49,7 @@ class _i18nSingleton extends Spine.Module
|
||||||
|
|
||||||
constructor: ( locale ) ->
|
constructor: ( locale ) ->
|
||||||
@map = {}
|
@map = {}
|
||||||
|
@dateFormat = 'yyyy-mm-dd'
|
||||||
@timestampFormat = 'yyyy-mm-dd HH:MM'
|
@timestampFormat = 'yyyy-mm-dd HH:MM'
|
||||||
|
|
||||||
# observe if text has been translated
|
# observe if text has been translated
|
||||||
|
@ -114,6 +120,10 @@ class _i18nSingleton extends Spine.Module
|
||||||
if data.timestampFormat
|
if data.timestampFormat
|
||||||
@timestampFormat = data.timestampFormat
|
@timestampFormat = data.timestampFormat
|
||||||
|
|
||||||
|
# set date format
|
||||||
|
if data.dateFormat
|
||||||
|
@dateFormat = data.dateFormat
|
||||||
|
|
||||||
# load translation collection
|
# load translation collection
|
||||||
for object in data.list
|
for object in data.list
|
||||||
|
|
||||||
|
@ -176,7 +186,13 @@ class _i18nSingleton extends Spine.Module
|
||||||
.replace(/>/g, '>')
|
.replace(/>/g, '>')
|
||||||
.replace(/\x22/g, '"')
|
.replace(/\x22/g, '"')
|
||||||
|
|
||||||
|
date: ( time, offset ) =>
|
||||||
|
@convert(time, offset, @dateFormat)
|
||||||
|
|
||||||
timestamp: ( time, offset ) =>
|
timestamp: ( time, offset ) =>
|
||||||
|
@convert(time, offset, @timestampFormat)
|
||||||
|
|
||||||
|
convert: ( time, offset, format ) =>
|
||||||
s = ( num, digits ) ->
|
s = ( num, digits ) ->
|
||||||
while num.toString().length < digits
|
while num.toString().length < digits
|
||||||
num = "0" + num
|
num = "0" + num
|
||||||
|
@ -194,7 +210,6 @@ class _i18nSingleton extends Spine.Module
|
||||||
S = timeObject.getSeconds()
|
S = timeObject.getSeconds()
|
||||||
M = timeObject.getMinutes()
|
M = timeObject.getMinutes()
|
||||||
H = timeObject.getHours()
|
H = timeObject.getHours()
|
||||||
format = @timestampFormat
|
|
||||||
format = format.replace /dd/, s( d, 2 )
|
format = format.replace /dd/, s( d, 2 )
|
||||||
format = format.replace /d/, d
|
format = format.replace /d/, d
|
||||||
format = format.replace /mm/, s( m, 2 )
|
format = format.replace /mm/, s( m, 2 )
|
||||||
|
|
|
@ -28,6 +28,9 @@ class App.PrettyDate
|
||||||
diff = diff.toString().replace('-', '')
|
diff = diff.toString().replace('-', '')
|
||||||
diff = parseFloat(diff)
|
diff = parseFloat(diff)
|
||||||
|
|
||||||
|
if direction is 'past' && !escalation && diff > ( 60 * 60 * 24 * 14 )
|
||||||
|
return App.i18n.translateDate(time)
|
||||||
|
|
||||||
# days
|
# days
|
||||||
string = ''
|
string = ''
|
||||||
count = 0
|
count = 0
|
||||||
|
|
|
@ -31,9 +31,17 @@ class Translation < ApplicationModel
|
||||||
:de => 'dd.mm.yyyy HH:MM',
|
:de => 'dd.mm.yyyy HH:MM',
|
||||||
}
|
}
|
||||||
timestamp = timestamp_map[ locale.to_sym ] || timestamp_map_default
|
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 {
|
return {
|
||||||
:list => list,
|
:list => list,
|
||||||
:timestampFormat => timestamp,
|
:timestampFormat => timestamp,
|
||||||
|
:dateFormat => date,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,19 @@ test( "check pretty date", function() {
|
||||||
result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 10.5 ) );
|
result = App.PrettyDate.humanTime( current - ( 60000 * 60 * 24 * 10.5 ) );
|
||||||
equal( result, '10 days ago', '10.5 days')
|
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
|
// future
|
||||||
current = new Date()
|
current = new Date()
|
||||||
result = App.PrettyDate.humanTime( current );
|
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 ) );
|
result = App.PrettyDate.humanTime( current.getTime() + ( 60050 * 60 * 24 * 2.5 ) );
|
||||||
equal( result, 'in 2 days 12 hours', 'in 2.5 days')
|
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')
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue