PageRenderTime 13ms CodeModel.GetById 10ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/wheels/view/dates.cfm

http://cfwheels.googlecode.com/
ColdFusion | 113 lines | 111 code | 2 blank | 0 comment | 38 complexity | d119ecd237db8392556374bcf0b3a3b7 MD5 | raw file
  1<cffunction name="distanceOfTimeInWords" returntype="string" access="public" output="false" hint="Pass in two dates to this method, and it will return a string describing the difference between them."
  2	examples='
  3	<cfset aWhileAgo = Now() - 30>
  4	<cfset rightNow = Now()>
  5	<cfoutput>##distanceOfTimeInWords(aWhileAgo, rightNow)##</cfoutput>
  6	'
  7	categories="view-helper,dates" chapters="miscellaneous-helpers" functions="timeAgoInWords,timeUntilInWords">
  8	<cfargument name="fromTime" type="date" required="true" hint="Date to compare from.">
  9	<cfargument name="toTime" type="date" required="true" hint="Date to compare to.">
 10	<cfargument name="includeSeconds" type="boolean" required="false" hint="Whether or not to include the number of seconds in the returned string.">
 11	<cfscript>
 12		var loc = {};
 13		$args(name="distanceOfTimeInWords", args=arguments);
 14		loc.minuteDiff = DateDiff("n", arguments.fromTime, arguments.toTime);
 15		loc.secondDiff = DateDiff("s", arguments.fromTime, arguments.toTime);
 16		loc.hours = 0;
 17		loc.days = 0;
 18		loc.returnValue = "";
 19		if (loc.minuteDiff <= 1)
 20		{
 21			if (loc.secondDiff < 60)
 22				loc.returnValue = "less than a minute";
 23			else
 24				loc.returnValue = "1 minute";
 25			if (arguments.includeSeconds)
 26			{
 27				if (loc.secondDiff < 5)
 28					loc.returnValue = "less than 5 seconds";
 29				else if (loc.secondDiff < 10)
 30					loc.returnValue = "less than 10 seconds";
 31				else if (loc.secondDiff < 20)
 32					loc.returnValue = "less than 20 seconds";
 33				else if (loc.secondDiff < 40)
 34					loc.returnValue = "half a minute";
 35			}
 36		}
 37		else if (loc.minuteDiff < 45)
 38		{
 39			loc.returnValue = loc.minuteDiff & " minutes";
 40		}
 41		else if (loc.minuteDiff < 90)
 42		{
 43			loc.returnValue = "about 1 hour";
 44		}
 45		else if (loc.minuteDiff < 1440)
 46		{
 47			loc.hours = Ceiling(loc.minuteDiff/60);
 48			loc.returnValue = "about " & loc.hours & " hours";
 49		}
 50		else if (loc.minuteDiff < 2880)
 51		{
 52			loc.returnValue = "1 day";
 53		}
 54		else if (loc.minuteDiff < 43200)
 55		{
 56			loc.days = Int(loc.minuteDiff/1440);
 57			loc.returnValue = loc.days & " days";
 58		}
 59		else if (loc.minuteDiff < 86400)
 60		{
 61			loc.returnValue = "about 1 month";
 62		}
 63		else if (loc.minuteDiff < 525600)
 64		{
 65			loc.months = Int(loc.minuteDiff/43200);
 66			loc.returnValue = loc.months & " months";
 67		}
 68		else if (loc.minuteDiff < 657000)
 69		{
 70			loc.returnValue = "about 1 year";
 71		}
 72		else if (loc.minuteDiff < 919800)
 73		{
 74			loc.returnValue = "over 1 year";
 75		}
 76		else if (loc.minuteDiff < 1051200)
 77		{
 78			loc.returnValue = "almost 2 years";
 79		}
 80		else if (loc.minuteDiff >= 1051200)
 81		{
 82			loc.years = Int(loc.minuteDiff/525600);
 83			loc.returnValue = "over " & loc.years & " years";
 84		}
 85	</cfscript>
 86	<cfreturn loc.returnValue>
 87</cffunction>
 88
 89<cffunction name="timeAgoInWords" returntype="string" access="public" output="false" hint="Pass in a date to this method, and it will return a string describing the approximate time difference between that date and the current date."
 90	examples='
 91		<cfset aWhileAgo = Now() - 30>
 92		<cfoutput>##timeAgoInWords(aWhileAgo)##</cfoutput>
 93	'
 94	categories="view-helper,dates" chapters="miscellaneous-helpers" functions="distanceOfTimeInWords,timeUntilInWords">
 95	<cfargument name="fromTime" type="date" required="true" hint="See documentation for @distanceOfTimeInWords.">
 96	<cfargument name="includeSeconds" type="boolean" required="false" hint="See documentation for @distanceOfTimeInWords.">
 97	<cfargument name="toTime" type="date" required="false" default="#now()#" hint="See documentation for @distanceOfTimeInWords.">
 98	<cfset $args(name="timeAgoInWords", args=arguments)>
 99	<cfreturn distanceOfTimeInWords(argumentCollection=arguments)>
100</cffunction>
101
102<cffunction name="timeUntilInWords" returntype="string" access="public" output="false" hint="Pass in a date to this method, and it will return a string describing the approximate time difference between the current date and that date."
103	examples='
104		<cfset aLittleAhead = Now() + 30>
105		<cfoutput>##timeUntilInWords(aLittleAhead)##</cfoutput>
106	'
107	categories="view-helper,dates" chapters="miscellaneous-helpers" functions="timeAgoInWords,distanceOfTimeInWords">
108	<cfargument name="toTime" type="date" required="true" hint="See documentation for @distanceOfTimeInWords.">
109	<cfargument name="includeSeconds" type="boolean" required="false" hint="See documentation for @distanceOfTimeInWords.">
110	<cfargument name="fromTime" type="date" required="false" default="#now()#" hint="See documentation for @distanceOfTimeInWords.">
111	<cfset $args(name="timeUntilInWords", args=arguments)>
112	<cfreturn distanceOfTimeInWords(argumentCollection=arguments)>
113</cffunction>