Simple User-Defined Function (UDF) to Convert Server Date/Time Stamp Into Local North American Time

Very often, a web server is located in a different time zone from the majority of the visitors (and owner) of a web site. I came up with this user-defined function (UDF) to return a timestamp in the web site owner's local North American timezone. This way, reports, emails, etc. can all have a consistent timestamp.

Valid time zones for North America are: newfoundland, atlantic, eastern, central, mountain, pacific, hawaii, utc. Obviously, I'm using more user-friendly terminology here than EST, CDT, etc.

For more information on North American time zones, visit http://www.timeanddate.com/library/abbreviations/timezones/na/.

This function returns a string as a timestamp. For example: {ts '2010-01-01 01:00:00'}. You can then use dateFormat() and/or timeFormat() to output as desired.

Examples


<cfoutput>
    Central: #getNorthAmericanTimeStamp('central')#<br />
    Pacific: #dateFormat(getNorthAmericanTimeStamp('pacific'), 'mm/dd/yyyy')#<br />
    Hawaii: #timeFormat(getNorthAmericanTimeStamp('hawaii'), 'h:mm:ss tt')#
</cfoutput>

The Code


<!--- Info --------------------------------------------------------------------------------------------------

    Document:        getNorthAmericanTimeStamp.cfc
    Author:            Steve Withington (steve [at] stephenwithington [dot] com)
    Creation Date:    01/08/2010
    Copyright:        (c) 2010 Stephen J. Withington, Jr. | www.stephenwithington.com
    
    Purpose:        I convert the server date/time to any North American timezone date/timestamp.
                    Pass me a timezone, and I'll convert my server date/time to the preferred timezone's
                    date/time and pass it back to you.
    
    Revision Log / Notes:
    01/08/2010 sjw    First release.

-------------------------------------------------------------------------------------------------- /Info --->

<cffunction name="getNorthAmericanTimeStamp" returntype="string" output="false" access="remote">
    <cfargument name="timeZone" type="string" required="false" default="eastern" />
    <cfscript>        
        var local = structNew();
        local.t = structNew();
        local.t.dts = now();
        local.t.tzi = getTimeZoneInfo();
        local.t.utc = dateAdd("s", local.t.tzi.utcTotalOffset, local.t.dts);
        local.timeZones = "newfoundland,atlantic,eastern,central,mountain,pacific,hawaii,utc";

        if ( local.t.tzi.isDSTon ) {
            local.t.newfoundland = dateAdd("n", -150, local.t.utc);            
            local.t.atlantic = dateAdd("h", -3, local.t.utc);
            local.t.eastern = dateAdd("h", -4, local.t.utc);
            local.t.central = dateAdd("h", -5, local.t.utc);
            local.t.mountain = dateAdd("h", -6, local.t.utc);
            local.t.pacific = dateAdd("h", -7, local.t.utc);
            local.t.hawaii = dateAdd("h", -9, local.t.utc);    

        } else {
            local.t.newfoundland = dateAdd("n", -210, local.t.utc);
            local.t.atlantic = dateAdd("h", -4, local.t.utc);
            local.t.eastern = dateAdd("h", -5, local.t.utc);
            local.t.central = dateAdd("h", -6, local.t.utc);
            local.t.mountain = dateAdd("h", -7, local.t.utc);
            local.t.pacific = dateAdd("h", -8, local.t.utc);
            local.t.hawaii = dateAdd("h", -10, local.t.utc);
        };
                    
        if ( listFindNoCase(local.timeZones, arguments.timeZone, ",") ) {
            switch(arguments.timeZone) {
                case "newfoundland":
                    return local.t.newfoundland;
                    break;
                case "atlantic":
                    return local.t.atlantic;
                    break;
                case "eastern":
                    return local.t.eastern;
                    break;
                case "central":
                    return local.t.central;
                    break;
                case "mountain":
                    return local.t.mountain;
                    break;
                case "pacific":
                    return local.t.pacific;
                    break;
                case "hawaii":
                    return local.t.hawaii;
                    break;
                case "utc":
                    return local.t.utc;
                    break;
                default:
                    return local.t.eastern;
            };
        } else {
            return local.t.eastern;
        };    
    
</cfscript>
</cffunction>

Hope this helps!

Simple User-Defined Function (UDF) Get ID of New Record With ColdFusion

I've been doing more development for applications that could be used with a variety of databases. So recently I whipped up this little user-defined function (UDF) to grab the identity (ID) of a row that was inserted into a database that I thought other developers might find useful. You'll need at least ColdFusion 8 to use this little puppy.


<!---
Pass me a CFQuery result struct and I'll try my best to get the identity (ID) of a row inserted into a database.
Works with SQL Server, Oracle, Sybase, Informix, and MySQL.
Requires ColdFusion 8+

@param qResult query result structure (Required)
@return Returns any
@author Stephen Withington (steve@stephenwithington.com)
@version 0, January 7, 2010
--->

<cffunction name="getIdentity" returntype="any" access="remote" output="false">
    <cfargument name="qResult" type="struct" required="true" />
    <cfscript>
        var local = structNew();
        local.identity = "unknown";
        
        if ( structKeyExists(arguments, "qResult") ) {
        
            // SQL Server
            if ( structKeyExists(arguments.qResult, "IDENTITYCOL") ) {
                local.identity = arguments.qResult.identitycol;
            } else
            // Oracle
            if ( structKeyExists(arguments.qResult, "ROWID") ) {
                local.identity = arguments.qResult.rowid;
            } else
            // Sybase
            if ( structKeyExists(arguments.qResult, "SYB_IDENTITY") ) {
                local.identity = arguments.qResult.syb_identity;
            } else
            // Informix
            if ( structKeyExists(arguments.qResult, "SERIAL_COL") ) {
                local.identity = arguments.qResult.serial_col;
            } else
            // MySQL
            if ( structKeyExists(arguments.qResult, "GENERATED_KEY") ) {
                local.identity = arguments.qResult.generated_key;
            };

        };
        
        return local.identity;
    
</cfscript>
</cffunction>

Example Usage


<cffunction name="addName" access="public" returntype="any" output="false">
    <cfargument name="name" type="string" required="true" />
    <cfscript>
        var local = structNew();
        var rs = "";
    
</cfscript>
    <cfquery datasource="#getDatasource()#" username="#getDBUsername()#" password="#getDBPassword()#" name="rs" result="local.rsResult">
        INSERT INTO tblNames ([Name])
        VALUES (<cfqueryparam value="#arguments.name#" cfsqltype="cf_sql_varchar" maxlength="50" />)
    </cfquery>
    <cfscript>
        local.identity = getIdentity(local.rsResult);
        return local.identity;
    
</cfscript>
</cffunction>

Hope this helps someone else other than me!

Adding Custom CSS Style Options to the Mura CMS Content Editor Toolbar

If you would like to add your custom CSS styles to the Mura CMS content editor toolbar, also known as a the FCKeditor toolbar, then read on!

First, let's make sure we're all on the same page. If you have access to the "Admin" area of a Mura CMS site, you can login, go to the Site Manager and select a page to edit content on. In the 'Content' area, a toolbar containing buttons to format the text, insert pictures, etc. runs across the top. This is the toolbar to which I'm referring. Here you will find a 'Style' dropdown.

This dropdown is controlled by an .XML file located under /{siteid}/includes/themes/{themename, i.e., 'merced'}/css/fckstyles.xml. Here is the basic content of that file:


<?xml version="1.0" encoding="utf-8" ?>
<Styles>
    <Style name="Align Image Left" element="img">
        <Attribute name="class" value="left" />
    </Style>
    <Style name="Align Image Right" element="img">
        <Attribute name="class" value="right" />
    </Style>
    <Style name="Intro" element="p">
        <Attribute name="class" value="intro" />
    </Style>
    <Style name="Centered Text" element="p">
        <Attribute name="class" value="center" />
    </Style>
    <Style name="Call to Action" element="a">
        <Attribute name="class" value="callToAction" />
    </Style>
    <Style name="Call to Action" element="li">
        <Attribute name="class" value="callToAction" />
    </Style>
</Styles>

In order to get your custom styles to appear in the dropdown, we would then obviously need to edit this particular file. To test this out, let's add some <Style> elements to this file.


    <Style name="Paragraph" element="p" />
    <Style name="Heading 1" element="h1" />
    <Style name="Heading 2" element="h2" />
    <Style name="Heading 3" element="h3" />
    <Style name="Heading 4" element="h4" />
    <Style name="Heading 5" element="h5" />
    <Style name="Heading 6" element="h6" />
    <Style name="Italic" element="em" />
    <Style name="Blockquote" element="blockquote" />
    <Style name="Preformatted Text" element="pre" />

Be sure you place these just after the opening <Styles> tag! Save the file, upload it to your site and navigate to your Admin > Site Manager, then select a page to edit.

Hey! Where's My Styles?

The first thing you'll probably notice is that nothing has changed. If this is the case, trust me, your changes are there, you just can't see them yet. Unfortunately, a <CTRL> + <F5> won't be enough to remedy this situation either, and this is probably where some people have thrown up their hands in frustration. Don't worry, we'll get this fixed in a snap.

The problem is merely that the contents of the XML file have been cached. So, let's 'officially' clear the cache, shall we? In Firefox, at the top of the Firefox window, click on the Tools menu, and select Options. Select the Advanced panel, then click on the Network tab. In the Offline Storage section, click the Clear Now button. Then click the OK button to close the Options window. If you're using Internet Explorer, then use Firefox. Just kidding ... but seriously, use Firefox.

If you're like me and have taken advantage of the 'Web Developer' toolbar add-on for Firefox, it's even simpler. Just click the Miscellaneous button on the toolbar, hover over Clear Private Data, then click Cache. That's it!

Show Me More!

So now you should see your 'new' styles appearing in the style dropdown. However, you want more don't you? Of course you do. Luckily, there is some pretty good documentation on the CKEditor site at http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration/Styles. There you should be able to learn more about the XML file, Style nodes, Attribute nodes, "Object" elements, and more.

I hope this helps!

More Entries

© 2026, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.