Coldfusion XML PubDate Time Comparison

146 views Asked by At

I have a project I'm creating in Coldfusion 10 and it's almost done but I just need to display how long it is has been since the last update in the XML feed. I know this shouldn't be too difficult but I can't seem to figure it out.

The project is feed by an XML feed from a publication and of all the pubdates in the feed I need to find out which pubdate is the most recent(they may not be in chronological order in the feed). I also need to compare the most recent pubdate in the feed to the current local time which is PDT and pubdates in feed are listed in GMT. I already have the XML parsed out and the pubdates are captured in a variable named "rssDate" and the pubdates are formatted like: {ts '2014-06-27 20:48:46'}.

Here are the needed elements broken out

  1. Find most recent pubdate in the XML feed
  2. Convert the most recent pubdate (date & time) from GMT to PDT and display as last update time stamp
  3. Determine the time since publication of the most recent pubdate and output as color coded display based on time ranges (see included output code).

Color coded output of time since last update based upon time range

<cfif timeDifference LT 1>
<cfset meterColor = "4bbd07">
<cfset colorName = "Green">
<cfset messageText = "Updated less than one hour ago">
<cfelseif timeDifference GTE 1 AND timeDifference LT 1.3>
<cfset meterColor = "64ff06">
<cfset colorName = "Lime">
<cfset messageText = "Updated less than one and a half hours ago">
<cfelseif timeDifference GTE 1.3 AND timeDifference LT 2>
<cfset meterColor = "fffc06">
<cfset colorName = "Yellow">
<cfset messageText = "Updated more than two hours ago">
<cfelseif timeDifference GTE 2 AND timeDifference LT 2.3>
<cfset meterColor = "fdae15">
<cfset colorName = "Orange">
<cfset messageText = "Updated more than two hours ago">
<cfelseif timeDifference GTE 2.3 AND timeDifference LT 3>
<cfset meterColor = "ff00fc">
<cfset colorName = "Pink">
<cfset messageText = "Updated more than two and a half hours ago">
<cfelseif timeDifference GTE 3>
<cfset meterColor = "fe0000">
<cfset colorName = "Red">
<cfset messageText = "Updated more than three hours ago">
</cfif>
1

There are 1 answers

0
CFML_Developer On

There are many way to do it. One is as as PDT is 7 hours behind GMT. Adding 7 hours in your PDT time will make it GMT and then you can compare both. (You can do it all on a single line, just for more clarity breaking it up)

<cfset myGMTTime=DateAdd("h",7,now())>
<cfset rssDat="2014-06-27 20:48:46"> <!---as you said you have this figured out--->
<cfset timeElapsed=DateDiff("n",myGMTTime,rssDate)><!---can use "h" for hours or "s" for seconds. It is "n" for minutes. "m" is used for months--->