Get rid of Word's funky characters

I always despise the funky characters that Microsoft decided to put into Word documents for simple things like quotes and dashes. Extracted (borrowed!) from the safetext() ColdFusion UDF function, the following ColdFusion code will do the trick:

<cfset goodText=replaceList(badText, chr(8216) & "," &
chr(8217) & "," & chr(8220) & "," & chr(8221) & "," & chr(8212) & "," &
chr(8213) & "," & chr(8230),"',',"","",--,--,...")>

Quick test for Daylight Saving Time updates

I wrote a quick-and-dirty little ColdFusion page to test my various servers for DST updates. It shows the running java virtual machine version and then tests 3 dates: today, March 15, 2007, and April 15, 2007. The March and April dates should both show an offset of 1 if your server is running in a DST time zone affected by the change. If the March date shows zero, then you need to upgrade your JVM. The recommendation version is Sun's JDK 1.4.2_11. I installed 1.4.2_13 (the latest 1.4.x available) and seem to be running fine.

Here is the code:

<!---
PROGRAM: date.cfm
PURPOSE: test new DST rules
--->

<cfset sys=CreateObject("java","java.lang.System")>
<cfoutput>
<p>You are running
   <strong>#sys.getProperty("java.vendor")#</strong>
   jvm version
   <strong>#sys.getProperty("java.version")#</strong>.
</p>
<hr />
</cfoutput>

<cfset today=now()>
<cfset march15=CreateDateTime(2007, 3, 15, 1, 0, 0)>
<cfset april15=CreateDateTime(2007, 4, 15, 1, 0, 0)>
<cfset t=CreateObject("java", "java.util.TimeZone").getDefault()>
<cfset cal=CreateObject("java", "java.util.GregorianCalendar").init(t)>
<cfset cal.setTime(today)>
<cfset offset_today=cal.get(cal.DST_OFFSET)/(60*60*1000)><!--- in hours --->
<cfset cal.setTime(march15)>
<cfset offset_march15=cal.get(cal.DST_OFFSET)/(60*60*1000)><!--- in hours --->
<cfset cal.setTime(april15)>
<cfset offset_april15=cal.get(cal.DST_OFFSET)/(60*60*1000)><!--- in hours --->
<cfoutput>
Time Zone = #t.getDisplayName()#<br />
Today offset=#offset_today#<br />
March 15 offset=#offset_march15#<br />
April 15 offset=#offset_april15#<br />
</cfoutput>

CF Tip - Using Java Date methods

I despise using the ColdFusion DateCompare() and DateDiff() functions. They just don't seem to be very logical to me, or at least I can never remember the exact syntax and output I want. For instance, when I do a DateDiff(), does a negative result mean that the first or second date is later? In addition, the DateCompare() function returns 3 values (-1, 0, or 1), so if I want to compare the dates in an cfif statement, I can't use short-circuit syntax because. Many other CF functions (Pos(), Find(), etc.) allow a short-circuit for a true value because it returns a positive integer while the false value returns a zero.

Because of this, I prefer using the underlying java date functions. A ColdFusion datetime object is a java.util.Date under the covers, which gives you power beyond the normal built-in CF functions. The two Date functions that I use most are date.before() and date.after(), both of which return a boolean result. So, given a CF date object named "mydate", I can easily do a comparison with another date, such as now(). For instance, if the date is in the past, "mydate.before(now())" will return true.

But, before you jump on the horse, you have to remember that CF is loosely typed. So you cannot just do this:

<cfset mydate="1/1/2007">
<cfif mydate.before(now())>
<cfoutput>mydate is in the past</cfoutput>
<cfelse>
<cfoutput>mydate is in the future</cfoutput>
</cfif>
ColdFusion will throw an error stating "The selected method before was not found..." This is because you are operating on a java String, not a Date.

Therefore, you must convert the string to a Date object. You could do this with the CF CreateDate() function, but you would then have to split the month, day, and year out because of the required arguments of that function. A shorter way that doesn't require you to do that is to use the DateAdd() function. You can add a zero value to the date string, and ColdFusion with return a real date object. So change the code to this to make the before() function work:

<cfset mydate=DateAdd("d", 0, "1/1/2007")>
<cfif mydate.before(now())>
<cfoutput>mydate is in the past</cfoutput>
<cfelse>
<cfoutput>mydate is in the future</cfoutput>
</cfif>

You could make it even shorter by bypassing the mydate assignment if you don't need the variable:

<cfif DateAdd("d", 0, "1/1/2007").before(now())>
...

Viola!

BlogCFC was created by Raymond Camden. This blog is running version 5.8.