Skip to main content
Inspiring
July 2, 2008
Question

persian date

  • July 2, 2008
  • 12 replies
  • 5078 views
Hi all,

Since coldfusion doesn't support Farsi/Persian dates, I tried to use the icu4j lib and installed it as instructed. I tried to run the attached code(sample from a cf article) and as expected, it gave me an output in Farsi/Persian, literally. I mean, the characters and numbers were in Persian. The thing that I don't get is the Now(), as used in this code, which is supposed to give a Persian date (year 1387), it still gives me the gregorian date(year 2008). Although the characters are in persian, the date is still in gregorian. i want it to output in persian as in year 1387 which is the current year right now in Persian calendar. Same also with the month and day. Am I missing something here? Any insights are really much appreciated. Thanks.
This topic has been closed for replies.

12 replies

dongzkyAuthor
Inspiring
July 16, 2008
Hi Paul,

More info on what I did:
My server/PC is localozed to persian and also the timezone.

I created the ff. customized functions:

<!--- substitutes the Now() function in CF --->
<cffunction access="public" name="global_Now" output="No" returntype="date">
<cfscript>

var utcNow=DateConvert("local2utc",Now());

</cfscript>
<cfreturn utcNow>
</cffunction>

<!--- this will make a call to your i18nDateTImeFormat --->
<cffunction access="public" name="global_DateTimeFormat" output="no" returntype="string">
<cfargument name="thisDate" required="yes" type="date">
<cfargument name="thisDateFormat" default="LONG" required="No" type="string">
<cfargument name="thisTimeFormat" default="LONG" required="No" type="string">

<cfscript>

var lstStandardDateTimeFormats = "FULL,LONG,MEDIUM,SHORT";
var nDateFormat=1;
var nTimeFormat=1;

if(ListFindNoCase(lstStandardDateTimeFormats,arguments.thisDateFormat,","))
{
nDateFormat=ListFindNoCase(lstStandardDateTimeFormats,arguments.thisDateFormat,",")-1;
}
else
{
nDateFormat=1;
}
if(ListFindNoCase(lstStandardDateTimeFormats,arguments.thisTimeFormat,","))
{
nTimeFormat=ListFindNoCase(lstStandardDateTimeFormats,arguments.thisTimeFormat,",")-1;
}
else
{
nTimeFormat=1;
}

sDateTime=thisCalendar.i18nDateTimeFormat(thisDate=arguments.thisDate,thisLocale=theLocale,thisDateFormat=nDateFormat,thisTimeFormat=nTimeFormat);

</cfscript>

<cfreturn sDateTime>
</cffunction>

--------------------------------------------------------------------------------------
This is the code that tries to display the persian datetime:

#global_DateTimeFormat(global_Now(),"FULL","FULL")#

The code above returns the GMT+00:00.


However, when i try to directly call your method like this one below:

<cfscript>

thisDate=DateConvert("local2UTC",now());
sDateTime=thisCalendar.i18nDateTimeFormat(thisDate=thisDate,thisLocale="fa_IR",thisDateFormat=0,thisTimeFormat=0);
</cfscript>
<cfoutput>
#sDateTime#
</cfoutput>

This gave me the correct output. Is there anything worng with creating my own functions that calls your methods in your cfc? The reason I'm doing this is I have an existing English application that is based on Gregorian dates that I need to convert to Persian. And in order to avoid more hassles on the changes, I want to retain the parameters passed the way CF does and only need to change the function names(e.g. DateFormat becomes global_DateFormat with parameters passed unchanged). Thanks.



dongzkyAuthor
Inspiring
July 16, 2008
Hi Paul,

I have fixed the problem. It is in my code. The problem was I tried to declare my thisCalendar object this way:

<cfparam name="theCalendar" default="#cfcLocation#gregorianCalendar">

<cfset theLocale = "fa_IR">
<cfset theCalendar = "#cfcLocation#persianCalendar">

<cfset init()>

<cffunction access="public" name="init" output="No">
<cfset variables.thisCalendar=CreateObject("component",theCalendar)>
</cffunction>
----------------------------------------
I also tried another way:
No init() function and just simply this:

<cfset variables.thisCalendar=CreateObject("component",theCalendar)>

Base on those two ways of declaring above, the variables.thisCalendar doesn't seem to produce the correct output. I still don't understand why. So what I did is I remove the init() function or the direct cfset for the 2nd test, and just declare the object thisCalendar in each function I created. And it worked! But still confused why. I don't want to be declaring the object everytime I create a new function. But I guess I'll just have to stick with this workaround for now. Thanks again.

dongzkyAuthor
Inspiring
July 10, 2008
quote:

Originally posted by: Newsgroup User
dongzky wrote:
> How do I use your cfc to return a persian date object? I mean in your testbed,

why? as i explained before you're better off storing gregorian datetime objects
(or better yet epoch offsets which is the underlying value for all icu4j
calendars & all my calendar CFCs).



Oh, I see. Thanks.
dongzkyAuthor
Inspiring
July 15, 2008
Hi Paul,

I have a question on the i18nDateTimeFormat regarding the time. I am testing on my PC using windows xp. I set the locale of my PC to Farsi/Persian through the regional settings. After that, I changed the time zone in my pc to use GMT +3:30 (Tehran), which is I believe the tz, if not used, is similar in Iran. Now looking again at i18nDateTimeFormat method, the argument accepts a date in UTC format, right? So if I have a now() function to pass, I have to use DateConvert("local2utc",now()) as my input date(Please correct me If I'm no longer on the right track here of using your cfc). Base on the cfc, the return is:

return tDateFormatter.format(dateConvert("utc2local",arguments.thisDate));

In the return above, the UTC time is converted to local time(in my PC) and gives the format based on its locale, which is Farsi.Persian in my case. The output string is shown below:
۸:۳۷:۵۳ (GMT+۰۰:۰۰)، ساعت سه‌شنبه ۲۵ تیر ۱۳۸۷

(GMT+۰۰:۰۰) -> this part means (GMT+00:00), the standard UTC tz. I thought the time return is going to use my locale tz (GMT+3:30). Any way of displaying the actual local time in my PC(GMT+3:30)? Anything I did wrong? Hope I'm making sense. Thanks.
dongzkyAuthor
Inspiring
July 9, 2008
It could be that the class/method is by default meant for java apps, that's why it never bothered to subtract 1. Coz, if it did, then when a java app passes 0(first month), it would become -1 and would throw an error. In cf, that wouldn't be the case though since month number starts from 1.

I really appreciate the help. I'll be looking more also into this class and do some tests. And if I need help again, I'll be keeping this thread up again. :)
Thanks.
dongzkyAuthor
Inspiring
July 9, 2008
Hi Paul and to anyone else,

I have been using the classes and your cfc to display and calculate persian dates within coldfusion. The next thing I did is retrieve persian dates from an Oracle db and display in the client side(cf page) without using the components and the classes.

These are the things I did in cfqueries before querying the dates.

1) alter session set nls_calendar='persian' <- in oracle, this is how to tell oracle that the current session for calendar is in Persian so that it will give me a persian date(which is automatically calculated by oracle)

2) alter session set nls_date_format='yyyy-mm-dd' <-setting date format in oracle for current session

After that, in the same file I tried to query the db using select statement. But when I output the date object from Oracle to a cf page, the date object display becomes gregorian. I'm pretty sure oracle gave me a persian date 'coz i tried doing it inside oracle itself. But when cf page tries to display it, it becomes gregorian. My question is, do I need to use again the i18ncalendars component together with the classes or is there any other easier way that I have to set in order to display the Persian date in cf? My guess would be use again the components. But maybe there is another way.
dongzkyAuthor
Inspiring
July 10, 2008
Hi Paul,

How do I use your cfc to return a persian date object? I mean in your testbed, when you show your column for the i18nDateAdd, it still return a gregorian date object right? How do you convert this to a persian date object and not a string?
Inspiring
July 4, 2008
dongzky wrote:
> I am wrong, but the date base(I mean the basis for date conversion) is the
> gregorian date right? Which means, whatever date I input, the cf app(or maybe

no, it's based on cf(java) datetime, which happens to be gregorian calendar
based (actually it's decimals days since 31-dec-1899 like exel & db2).

> let the app know that the input date is already a Persian date. Any insights,
> ideas or suggestions on this. Thanks.

store your dates as cf datetimes or better yet as java epoch offsets (you will
eventually run into timezone issues if you have to support clients in multiple TZ).

i don't have any time right now to rework the persian calendar CFC so add the
method below to the CFC, it converts jalali calendar datetimes to gregorian
calendar datetimes. just be aware that cf sees all datetimes as being in the
server's TZ.


<cffunction name="jalaliToGregorian" access="public" output="false"
returntype="date" hint="converts Jalali calendar date to gregorian calendar date">
<cfargument name="jalaliYear" type="Numeric" required="true" hint="jalali year">
<cfargument name="jalaliMonth" type="Numeric" required="true" hint="jalali month">
<cfargument name="jalaliDay" type="Numeric" required="true" hint="jalali day">
<cfargument name="hours" type="Numeric" required="false" default=0>
<cfargument name="minutes" type="Numeric" required="false" default=0>
<cfargument name="seconds" type="Numeric" required="false" default=0>
<cfset var gregorianDate="">
<cfset var pc=createObject("java","com.ghasemkiani.util.SimplePersianCalendar")>
<cfset
pc.setDateFields(javaCast("int",arguments.jalaliYear),javaCast("int",arguments.jalaliMonth),javaCast("int",arguments.jalaliDay))>
<cfset gregorianDate=pc.getTime()>
<cfset
gregorianDate=createDateTime(year(gregorianDate),month(gregorianDate),day(gregorianDate),arguments.hours,arguments.minutes,arguments.seconds)>
<cfreturn gregorianDate> <!--- returns datetime in server TZ --->
</cffunction>
dongzkyAuthor
Inspiring
July 4, 2008
Hi Paul,

Thank you so much for the help. I got it working now. So far, so good. Anyway, I won't be having problems with TZ 'coz I will be specifically doing this app in a Persian locale server. I am going to look more info also from Dr. Ghasem's lib to familiarize the class as well. Thanks.
dongzkyAuthor
Inspiring
July 4, 2008
Hi Paul,

Your i18ncalendars is really a great help. There is another thing also I want to clarify about the i18ncalendars and the persiancalendar class. Correct me if I am wrong, but the date base(I mean the basis for date conversion) is the gregorian date right? Which means, whatever date I input, the cf app(or maybe this is done from the classes) will consider date inputs as gregorian and then convert it to the desired calendar format? What if I am entering a date input that is already a persian date, say I enter 1387-04-14(yyyy-mm-dd), is there any way that we can tell the classes(or maybe the i18ncalendars) that the input date is already a persian date(which means tell the app that the basis for date conversion is Persian) ? The thing is, when I enter 1387-04-14(a persian date), the app accepts it as a gregorian date and do some conversions. But I want to let the app know that the input date is already a Persian date. Any insights, ideas or suggestions on this? Thanks.


-Fran
dongzkyAuthor
Inspiring
July 3, 2008
Hi Paul,

everything works fine now. My bad. I was using the old files (which makes me wonder if the old files were correct? or maybe i have messed up those files). Anyway, I tried the latest files which i downloaded from the links you gave and everything is ok. Thank you so much.
Inspiring
July 3, 2008
dongzky wrote:
> http://www.sustainablegis.com/projects/icu4j/calendarsTB.cfm and it gave me the
> output i was expecting. Persian year now is 1387. However, when I tried to
> download all the files for this testbed and ran it from my local PC, the year
> now in Persian is giving me 1386 instead of 1387? Anything more I have to

what date did you pass in? there's no one-to-one year correspondence between the
gregorian & persian calendars:

AP 1386 covers 2007-2008 AD
AP 1387 covers 2008-2009 AD
dongzkyAuthor
Inspiring
July 3, 2008
Hi Paul,

I was actually just using your files. The date passed is now(). When i tried to compare the one in your site and the one i ran here in my PC, it's quite different. Although, the one in my PC outputs the correct date, the calculation at the lower table is different. The screenshots are shown in the links below.

From your given site

From my local PC
Inspiring
July 3, 2008
dongzky wrote:
> @Paul: thanks. i'll be looking into this. So, to clear things, icu4j only
> supports farsi for displaying and not automatic date calculations according
> to the farsi locale, right?

you need a calendar to do date math, etc. icu4j doesn't have the jalali calendar
built in (if i recall correctly some kind of academic politics or the regular
old kind of politics prevented any kind of official persian calendar from being
included in icu4j but it does have a lot of others like coptic, arabic, chinese,
etc).
dongzkyAuthor
Inspiring
July 3, 2008
hi Paul,

I tried your testbed http://www.sustainablegis.com/projects/icu4j/calendarsTB.cfm and it gave me the output i was expecting. Persian year now is 1387. However, when I tried to download all the files for this testbed and ran it from my local PC, the year now in Persian is giving me 1386 instead of 1387? Anything more I have to configure?

These are the things I did:
1. I copy the persianCalendar.cfc and the rest of the cfc's to my cfc path.
2. I copy the files(icu4j_3_2_calendar.jar, icu4j-4_0_d02-src.jar, icu4j-4_0_d02.jar, icu4j-charsets-4_0_d02.jar, icu4j-localespi-4_0_d02.jar and persiancalendar.jar) to \CF8root\WEB-INF\lib\addedClass where addedClass directory is my created dir which is already mapped or added in the java/jvm path in cf admin.
3. I copy the calendarsTB.cfm to my cf apps directory.
4. I restarted CF8 server.
5. I tested calendarsTB.cfm and got year 1386 in persian instead of 1387
dongzkyAuthor
Inspiring
July 3, 2008
@Paul: thanks. i'll be looking into this. So, to clear things, icu4j only supports farsi for displaying and not automatic date calculations according to the farsi locale, right?