Characters are garbled on MAC 10.6 while using wxDateTime

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
viveksha
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Nov 20, 2012 7:07 am

Characters are garbled on MAC 10.6 while using wxDateTime

Post by viveksha » Tue May 28, 2013 5:25 am

Hello All,

I have written a code to get the date and time using wxDateTime using the time-stamp.

Lets say time-stamp value is 1368718091 then I am using following function to get the date time :

wxString UIBase_X::ConvertDateTimeToLocalFormat(const wxString &strSetting)
{
wxString strName = _T("");
if(strSetting.size() && wxIsdigit(strSetting[0]))
{
wxDateTime date;
date.SetCountry(wxDateTime::Country_Default);
long lValue = 0;

strSetting.ToLong(&lValue);
//StrFmt::GetNumber(strSetting, lValue);
date.Set((time_t)lValue);

strName = GetStandardDate(date);
strName += _T(" ");
strName += GetStandardTime(date);
}

return strName;
}

Functions called internally are as follows :

wxString UIBase_X::GetStandardDate(const wxDateTime &date)
{
wxString strDate = date.FormatDate();
strDate += wxT(" ") + date.GetWeekDayName(date.GetWeekDay(), wxDateTime::Name_Abbr);

return strDate;
}

wxString UIBase_X::GetStandardTime(const wxDateTime &date)
{
wxString strPMAM = _T("");
wxString strHour =_T(""), strMin = _T(""), strSec = _T("");
GetTimeStrings(date, strPMAM, strHour, strMin, strSec);

wxString strValue;
strValue = strHour;
strValue += wxLT(":");
strValue += strMin;
strValue += wxLT(":");
strValue += strSec;
strValue += _T(" ");
strValue += strPMAM;

return strValue;
}

void UIBase_X::GetTimeStrings(const wxDateTime &date, wxString &strPMAM, wxString &strHour,
wxString &strMin, wxString &strSec)
{
bool bPM = false;
int iValue = 0;
wxString strAM = wxEmptyString, strPM = wxEmptyString;
wxDateTime::GetAmPmStrings(&strAM, &strPM);
iValue = date.GetHour();
if(!strAM.IsEmpty() && !strPM.IsEmpty())
{
wxDateTime dateNoon(date);
dateNoon.SetHour(12);
dateNoon.SetMinute(0);
dateNoon.SetMillisecond(0);
if(date.IsEarlierThan(dateNoon))
{
strPMAM = strAM;
}
else
{
bPM = true;
strPMAM = strPM;
}

if(iValue > 12 && bPM)
{
iValue -= 12;
}
}
strHour = wxString::Format(_T("%d"), iValue);
iValue = date.GetMinute();
strMin = wxString::Format(_T("%d"), iValue);
iValue = date.GetSecond();
strSec = wxString::Format(_T("%d"), iValue);
if(strMin.length() == 1)
{
strMin = _T("0") + strMin;
}
if(strSec.length() == 1)
{
strSec = _T("0") + strSec;
}
}

So this all is working very fine on Windows but somehow on mac I am getting error.
On windows I am getting output like "28-May-13 Tue 10:44:51 AM" but on Mac I am getting string garbled.
I have shared a screenshot of the MAC output. I don't know whats going wrong.
It will be great if anybody can help me for this and suggest me what wrong logic I am using and how to fix it.

Thanks and Regards,
Vivek
Attachments
Expected_.png
This is expected result.
Expected_.png (76.02 KiB) Viewed 3161 times
What_I_am_getting.png
This is what I am getting using my code.
What_I_am_getting.png (43.44 KiB) Viewed 3161 times

User avatar
doublemax
Moderator
Moderator
Posts: 15183
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by doublemax » Tue May 28, 2013 8:17 am

It seems that wxDateTime::GetWeekDayName() and wxDateTime::GetAmPmStrings() don't work correctly in Chinese under OSX.

I can't test on OSX and even if i could, i wouldn't have a Chinese system. First you should strip your sample down to just these two methods and confirm that is the problem. Then you can open a bug report here: http://trac.wxwidgets.org/

I wouldn't expect a quick fix though. I'd recommend to just write your own methods for this.
Use the source, Luke!

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by Radek » Tue May 28, 2013 8:25 am

Ough! Neither MAC nor chinesse fonts aren't my specialization but from what I am seeing, the problem consists in the weekday name and the "AM" and "PM" strings. That means, in the wxDateTime::GetWeekDayName() and wxDateTime::GetAmPmStrings() on MAC and Chinesse. First of all, verify: break at the statements and see what is in strDate, strAM and strPM after the calls. I suspect the calls that both of them return a garbage. If they do then it is a wx-error on MAC definitely and you will need to emulate the calls on MAC.

viveksha
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Nov 20, 2012 7:07 am

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by viveksha » Tue May 28, 2013 8:31 am

Thanks for your reply.
I tried to debug the code and found that from wxDateTime::GetWeekDayName() and wxDateTime::GetAmPmStrings() I am getting garbled values.
And that is the reason I am seeing this issue.
As you guys mentioned that this could be wx Widgets issue, Is there any patch for this ?
Could you guys please help me with some different functions similar to this which can work in my case ?
It would be great if I you can help to resolve this issue.

Thanks and Regards,
Vivek

User avatar
doublemax
Moderator
Moderator
Posts: 15183
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by doublemax » Tue May 28, 2013 9:18 am

Could you guys please help me with some different functions similar to this which can work in my case ?
Just write your own with hardcoded Chinese strings. Should be trivial enough.
You just have to make sure that your code is only used under OSX (conditional compilation) and when the locale is set to Chinese. ( wxLocale::GetSystemLanguage )
Use the source, Luke!

viveksha
Earned a small fee
Earned a small fee
Posts: 21
Joined: Tue Nov 20, 2012 7:07 am

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by viveksha » Tue May 28, 2013 2:08 pm

Your suggestion is perfectly ok but the problem is that the project where i am using this code supports around 16 languages.
So getting the localized strings for all languages and putting them in the code is not a feasible solution in my case.
Could you please suggest me any other C++ API which can provide me WeekDay and AM-PM string based on locale ?
I searched on google but did not find anything which can be useful for me.

Thanks and Regards,
Vivek

User avatar
doublemax
Moderator
Moderator
Posts: 15183
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Characters are garbled on MAC 10.6 while using wxDateTim

Post by doublemax » Tue May 28, 2013 2:37 pm

Your suggestion is perfectly ok but the problem is that the project where i am using this code supports around 16 languages.
You should try for which cases the wx functions return the correct value and only write substitutes for the non-working ones. Maybe Chinese is the only one that's not working.
Could you please suggest me any other C++ API which can provide me WeekDay and AM-PM string based on locale ?
Try boost.
http://www.boost.org/doc/libs/1_51_0/li ... zones.html
Use the source, Luke!

Post Reply