CFLib.org – Common Function Library Project

minutesToUtcOffset(minutes[, format][, returnZero])

Last updated January 8, 2015

author

Mosh Teitelbaum

Version: 1 | Requires: CF10 | Library: DateLib

Description:
Converts minutes to UTC Offset in the specified format.

Return Values:
A string indicating the specified minutes in UTC Offset format.

Example:

<cfoutput>
#minutesToUtcOffset(-300)# = -05:00<br />
#minutesToUtcOffset(300, "+hhmm")# = +0500<br />
#minutesToUtcOffset(-300, "+hh")# = -05<br />
</cfoutput>

Parameters:

Name Description Required
minutes The number of minutes to be converted to UTC Offset format. Yes
format Indicates the UTC Offset format to be used. Options are "+hh:mm" (default), "+hhmm", and "+hh". "+hh" falls back to the default if the converted minutes cannot convert to hours alone. No
returnZero Indicates whether or not anything should be returned for zero minutes (e.g., "+00:00"). Default is false. No

Full UDF Source:

<!---
Converts minutes to UTC Offset in the specified format.

@param minutes The number of minutes to be converted to UTC Offset format. (Required)
@param format Indicates the UTC Offset format to be used. Options are "+hh:mm" (default), "+hhmm", and "+hh". "+hh" falls back to the default if the converted minutes cannot convert to hours alone. (Optional)
@param returnZero Indicates whether or not anything should be returned for zero minutes (e.g., "+00:00"). Default is false. (Optional)
@return A string indicating the specified minutes in UTC Offset format.
@author Mosh Teitelbaum (mosh.teitelbaum@evoch.com)
@version 1.0, 1/8/2015
--->

<cffunction name="minutesToUtcOffset" returntype="string" output="no" description="Converts minutes to UTC Offset in the specified format.">
    <cfargument name="minutes" type="numeric" required="yes" hint="The number of minutes to be converted to UTC Offset format.">
    <cfargument name="format" type="string" required="no" default="+hh:mm" hint="Indicates the UTC Offset format to be used. Options are '+hh:mm' (default), '+hhmm', and '+hh'. '+hh' falls back to the default if the converted minutes cannot convert to hours alone.">
	<cfargument name="returnZero" type="boolean" required="no" default="false" hint="Indicates whether or not anything should be returned for zero minutes (e.g., '+00:00'). Default is false.">

	<!--- Initialize local variables --->
	<cfset var h = fix(arguments.minutes / 60)>		<!--- hours --->
	<cfset var m = abs(arguments.minutes) MOD 60>	<!--- minutes --->
	<cfset var f = arguments.format>				<!--- format --->

	<!--- If zero and not returning zero, return empty string --->
	<cfif (arguments.minutes EQ 0) AND (NOT arguments.returnZero)>
		<cfreturn "">
	</cfif>

	<!--- Validate format --->
	<cfif (listFindNoCase("+hh:mm,+hhmm,+hh", arguments.format) EQ 0) OR ( (compareNoCase(arguments.format, "+hh") EQ 0) AND (m NEQ 0) )>
		<cfset f = "+hh:mm">
	</cfif>

	<!--- Return formatted string --->
	<cfswitch expression="#f#">
		<cfcase value="+hhmm">
			<cfreturn numberFormat(h, "+09") & numberFormat(m, "09")>
		</cfcase>

		<cfcase value="+hh">
			<cfreturn numberFormat(h, "+09")>
		</cfcase>

		<cfcase value="+hh:mm">
			<cfreturn numberFormat(h, "+09") & ":" & numberFormat(m, "09")>
		</cfcase>
	</cfswitch>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson