Railo Regional Settings Caution

One of the great things about Railo webs is the ability to set regional settings for each of your contexts. That means you can have several different context on the same server all showing different time zones. But you have to be rather careful when using it.

BlogCFC uses a Util component to handle all the local date conversions and such and inside this component they tap into the underlying java object java.text.DateFormat. So far so good, right? I mean what could possibly go wrong?

Well here's the problem

now() returns the regional date/time that I have in my Railo Regional settings as expected. The date object is passed to the timeLocaleFormat function correctly and then the true work is done by this code.


<cfscript>
// hack to trap & fix varchar mystery goop coming out of mysql datetimes
try {
return variables.aDateFormat.getTimeInstance(variables.aDateFormat[arguments.style],variables.thisLocale).format(arguments.date);
}
catch (Any e) {
variables.aCalendar.setTime(arguments.date);
return variables.aDateFormat.getTimeInstance(variables.aDateFormat[arguments.style],variables.thisLocale).format(variables.aCalendar.getTime());
}
</cfscript>
For some strange reason format() is taking my date object and completely ignoring it and making a new date object from the server. I ran a couple of tests to see what was going on.

Tests

I set the time for my Railo web to GMT+8 which put the time difference from server by 7 hours (I'm in GMT+1). The first test I used the now() function and the second test I created the date object by hand using createDateTime()

<cfset variables.thisLocale=createObject("java","java.util.Locale").init('en', 'DE')>
<cfset variables.aDateFormat=createObject("java","java.text.DateFormat")>
<cfset variables.mydate=now()/>
<!---this is the Railo Time: {ts '2009-01-28 21:44:13'} --->
<cfoutput>this is the Railo Time:#variables.mydate#</cfoutput><br>
<!---the output Time: 2:44 PM, server time 2:44pm --->
<cfoutput>
the output Time:#variables.aDateFormat.getTimeInstance(variables.aDateFormat['SHORT'],variables.thisLocale).format(variables.mydate)#, server time #lstimeformat(nowserver(),'short')#<br>
</cfoutput>
<cfset variables.mydate2=createdatetime(2009,1,28,18,44,13)/>
<!---this is the created date/time: {ts '2009-01-28 18:44:13'} --->
<cfoutput>this is the created date/time:#variables.mydate2#</cfoutput><br>
<!---the output 11:44am, the Server Time: 2:44 PM --->
<cfoutput>the output
#variables.aDateFormat.getTimeInstance(variables.aDateFormat['SHORT'],variables.thisLocale).format(variables.mydate2)#, the Server Time:#lstimeformat(nowserver(),'short')#
</cfoutput>

As you can see from the above results it's as if the Java function either returns the servers time or takes in a date/time object and time-shifts the date/time object to the servers time zone. Thats the only thing I can think of why it would take my statically created date/time object of '2009-01-28 18:44:13' and spit out 11:44am, it's exactly 7 hours different.

Now is there anyone out there that can tell me why? Is this something that's wrong with Railo, although I can't imagine what, or is this a Java thing?

1 Comments to "Railo Regional Settings Caution"- Add Yours
Streit MIchael's Gravatar Hi Gary
that is very easy to explain, in java the Date Object (java.util.Date) is not Timezone specific,
every date object contain only a long value with the offset in milli seconds from 1/1/1970 UTC (Unix Time).
<cfoutput>unix time:#now().getTime()#<br></cfoutput>
the timezone specific outputs happens when you format the date object to a string like:
<cfset DateFormat=createObject("java","java.text.DateFormat")>
<cfset locale=createObject("java","java.util.Locale").init('en', 'DE')>
<cfset df=DateFormat.getDateTimeInstance(DateFormat['SHORT'],DateFormat['SHORT'],locale)>
<cfoutput>DateFormat-now:#df.format(now())#<br></cfoutput>
in this case you get the jvm time, because the method format take the default timezone of the jvm, when you do not specify a other one,
but when you specify the railo timezone, you get the right result
<cfset DateFormat=createObject("java","java.text.DateFormat")>
<cfset locale=createObject("java","java.util.Locale").init('en', 'DE')>
<cfset df=DateFormat.getDateTimeInstance(DateFormat['SHORT'],DateFormat['SHORT'],locale)>
<cfset df.setTimeZone(getPageContext().getConfig().getTimeZone())>
<cfoutput>DateFormat-now:#df.format(now())#<br></cfoutput>
and railo does exactly the same when you cast the date to a string (#now()#).
you can compare this with the following:
<cfoutput>lsCurrencyFormat:#lsCurrencyFormat(1,"international","german (swiss)")#<br></cfoutput>
the input of this function is only a number, the givel locale define the format.
but what is with the function nowServer, like written before date object does not contain the timezone, only the unix time offset,
because that we simply remove the differents in milli seconds between the default timezone and the railo timezone from the date of nowserver.
that was a hack the see the server time. means use nowsserver only with cfml code. nowserver has begins his live as internal function.
pherhaps it would be better to remove the functon nowserver from railo and atd 2 new functions [getTimeZone, setTimeZone] similar to [setLocale,getLocale]
then you can output the server time as follows
<cfoutput>now:#now()#<br></cfoutput>
<cfset setTimezone('UTC')>
<cfoutput>nowserver:#now()#<br></cfoutput>
that would be a cleaner solution
greetings Michael
# Posted By Streit MIchael | 1/31/09 9:43 AM

Powered By Railo

Subscribe

Subscribe via RSS
Follow garyrgilbert on Twitter Follow me on Twitter
Or, Receive daily updates via email.

Tags

adobe air ajax apple cf community cfml coldfusion examples ext flash flex google javascript max2007 max2008 misc open source programming railo software technology ui

Recent Entries

Converting structkeys to lowercase

Blogroll

An Architect's View
CFSilence
Rey Bango
TalkingTree

Wish List

My Amazon.com Wish List