diff options
author | James Lopez <james@jameslopez.es> | 2016-11-16 17:55:20 +0300 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2016-11-17 10:22:59 +0300 |
commit | cbd7d000395ff60fe3726e67ec351bd4d44582ec (patch) | |
tree | f497cc29ef7cb87a9f6bd4334ea536f6b6aa81fd /app/serializers/entity_date_helper.rb | |
parent | 4844476e77f625829d99b7db2680186939ef660a (diff) |
added custom date helper and spec and fixed some unrelated spec failures
Diffstat (limited to 'app/serializers/entity_date_helper.rb')
-rw-r--r-- | app/serializers/entity_date_helper.rb | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/app/serializers/entity_date_helper.rb b/app/serializers/entity_date_helper.rb index 57817ce1812..b333b3344c3 100644 --- a/app/serializers/entity_date_helper.rb +++ b/app/serializers/entity_date_helper.rb @@ -4,4 +4,32 @@ module EntityDateHelper def interval_in_words(diff) "#{distance_of_time_in_words(diff.to_f)} ago" end + + # Converts seconds into a hash such as: + # { days: 1, hours: 3, mins: 42, seconds: 40 } + # + # It returns 0 seconds for zero or negative numbers + # It rounds to nearest time unit and does not return zero + # i.e { min: 1 } instead of { mins: 1, seconds: 0 } + def distance_of_time_as_hash(diff) + diff = diff.abs.floor + + return { seconds: 0 } if diff == 0 + + mins = (diff / 60).floor + seconds = diff % 60 + hours = (mins / 60).floor + mins = mins % 60 + days = (hours / 24).floor + hours = hours % 24 + + duration_hash = {} + + duration_hash[:days] = days if days > 0 + duration_hash[:hours] = hours if hours > 0 + duration_hash[:mins] = mins if mins > 0 + duration_hash[:seconds] = seconds if seconds > 0 + + duration_hash + end end |