Saturday, March 20, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Dec 2005 >>
S M T W T F S
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
             

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1381) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (197) [RSS]
 • Flex (498) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Entry / Main
December 6, 2005

ColdFusion UDF To Access NIST Time Servers

A developer asked me how he could get absolutely accurate time information for an application that he is working on. He cannot rely on local server time as he has no control over the machine, and can't verify that it is accurate (and can't change the time if not). There is no NTP (network time protocol) tag in ColdFusion, but fortunately one is not needed, because the NIST time servers also respond to plain text daytime protocol requests.

Here is a quick UDF I threw together to solve the problem. Call GetNISTTime() and it'll return a structure containing the raw data returned from the time server, as well as individual fields broken out for ease of use:

<!---

Name:            GetNISTTime()

Author:            Ben Forta, 12/6/2005

Description:    Obtains current time data from NIST
                Internet Time Service servers.

                DST:        US daylight savings time flag.
                HEALTHY:    TRUE if time server is healthy, FALSE if not.
                JULIAN:        Last 5 digits of Julian date/time value.
                LEAPMONTH:    TRUE is second will be added to or subtracted
                            from the current month.
                MSADV:        Number of milliseconds advanced by server to
                            compensate for network latency.
                NOW:        Current date/time.
                RAW:        Raw data from time server.
                SUCCESS:    TRUE if worked, FALSE if not, check
                            this flag first.

Note:            For a list of NIST time servers see:
                http://tf.nist.gov/timefreq/service/time-servers.html
                Servers should be addressed via IP address rather than
                host name. The server used here is time.nist.gov
                (192.43.244.18), but any of the listed servers will work.
                To use an alternate server, just specify the IP
                address in timeServer variable.
--->


<cffunction name="GetNISTTime" returntype="struct" output="false">
    <cfset var timeServer="192.43.244.18">
    <cfset var result=StructNew()>

    <!--- Try/catch block --->
    <cftry>

        <!--- Try get time data --->
        <cfhttp url="http://#timeServer#:13/" />
        <!--- Save raw data --->
        <cfset result.raw = CFHTTP.FileContent>
        <!--- Extract Julian date --->
        <cfset result.julian=ListGetAt(result.raw, 1, " ")>
        <!--- Extract current date and time --->
        <cfset result.now=ParseDateTime(ListGetAt(result.raw, 2, " ")
                                        & " "
                                        & ListGetAt(result.raw, 3, " "))
>

        <!--- Extract daylight savings time flag --->
        <cfset result.dst=IIf(ListGetAt(result.raw, 4, " ") IS 0,
                                FALSE, TRUE)
>

        <!--- Extract leap month flag --->
        <cfset result.leapmonth=IIf(ListGetAt(result.raw, 5, " ") IS 0,
                                    FALSE, TRUE)
>

        <!--- Extract health flag --->
        <cfset result.healthy=IIf(ListGetAt(result.raw, 6, " ") IS 0,
                                    FALSE, TRUE)
>

        <!--- Extract advance milliseconds --->
        <cfset result.msadv=ListGetAt(result.raw, 7, " ")>
        <!--- Success --->
        <cfset result.success=TRUE>

        <!--- Catch any errors --->
        <cfcatch type="any">
            <cfset result.success=FALSE>
        </cfcatch>

    </cftry>

    <cfreturn result>

</cffunction>

To test this code you can just use:

<cfset x=GetNISTTime()>
<cfdump var="#x#">

TrackBacks
There are no trackbacks for this entry.

No trackback URL. Trackbacks are only allowed via interactive form.

Comments

  © Copyright 1997-2009 Ben Forta, All Rights Reserved