Mismatch in view_friendly_time and the post_time in API result
I am working on a customization wherein we are parsing the time to be displayed in a friendly manner, i.e. 2h ago, 3w ago etc. I know there is already a way of doing it by using ?restapi.response_style=view and then reading the view_friendly_date attribute as in below example:
<response status="success">
<value type="date_time" view_date="06-20-2014" view_time="03:37 AM" view_friendly_date="12 hours ago">2014-06-19T22:07:33+00:00</value>
</response>
but our client has a requirement of showing ‘h’ instead of ‘hours’ and ‘w’ instead of ‘weeks’ etc., so I made a function which is working fine, but when I compared it to what Lithium API is returning, the results are different. Below is my code which I changed so that you can run on your instance just to test.
First line is hard coded to use the time returned in above response.
Second line is the current time
<#assign time = "2014-06-19T22:07:33+00:00">
<#assign timeNow = "2014-06-20T16:07:33+00:00">
<#assign postTimeObject = time?datetime("yyyy-MM-dd'T'hh:mm:ss") />
<#assign postTimeObjectNow = timeNow?datetime("yyyy-MM-dd'T'hh:mm:ss") />
<#assign postTimeString = relativeTime(postTimeObject ) />
<#assign current_time = postTimeObjectNow?long>
<#assign timeNew = postTimeObject?long>
Posted Date - ${postTimeObject}<br><br>
----Converting to 'long' starts----<br>
Long posted - ${timeNew}<br>
Long current - ${current_time }<br>
----Converting to 'long' ends ----<br><br>
<b>Friendly Output - ${postTimeString}</b><br>
<#function relativeTime time>
<#-- this is working in ms, so bear in mind the extra 000 -->
<#assign current_time = postTimeObjectNow?long>
<#assign action_time = time?long>
<#assign diff = current_time - action_time>
<#if diff == 0>
<#return 'now'>
<#elseif (diff > 0)>
<#assign day_diff = diff / 86400000>
<#assign day_diff = day_diff?floor>
here ${day_diff}
<#if (day_diff == 0)>
<#if (diff < 60000)>
<#return 'just now'>
</#if>
<#if (diff < 120000)>
<#return '1m ago'>
</#if>
<#if (diff < 3600000)>
<#assign sixty_min = diff / 60000>
<#return sixty_min?floor + 'm ago'>
</#if>
<#if (diff < 7200000)>
<#return '1h ago'>
</#if>
<#if (diff < 86400000)>
<#assign hours = diff / 3600000>
<#return hours?floor + 'h ago'>
</#if>
</#if>
<#if (day_diff == 1)>
here 1 ${day_diff}
<#return 'Yesterday'>
</#if>
<#if (day_diff < 7)>
here2 ${day_diff}
<#return day_diff + 'd ago'>
</#if>
<#if (day_diff < 31)>
here 3 ${day_diff}
<#assign weeks = (day_diff / 7)?ceiling>
<#if (weeks == 1)>
<#return 'Last week.'>
</#if>
<#return weeks + 'w ago'>
</#if>
<#if (day_diff < 60)>
<#return 'last month'>
</#if>
</#if>
<#return time?string("MM-dd-yy")>
</#function>
At the time of posting this query, the output is:
Posted Date - 19-Jun-2014 22:07:33
----Converting to 'long' starts----
Long posted - 1,403,240,853,000
Long current - 1,403,305,653,000
----Converting to 'long' ends ----
Friendly Output - 18h ago
But the restapi is returning 12 hours ago? any mistake in my code or is it an issue with the API?
Sorry for such a long post.