[wxDateTime] Formatting output according to user's regional & language options

Volker Bartheld dr_versaeg at freenet.de
Mon Apr 23 09:36:57 PDT 2007


Hi Vadim!

> VB> A quick test with
> VB>     wxDateTime now(time(NULL));
> VB>     now.SetCountry(wxDateTime::Country_Default);
> VB>     wxString s=now.Format(wxT("%c"));
> VB> made me end up with "04/20/07 17:58:22" (US default, I guess)

On Fri, 20 Apr 2007 18:49:44 +0200, Vadim Zeitlin wrote:
>  Did you use wxLocale or call setlocale(LC_ALL, "")?

Actually, I didn't because I was assuming wxDateTime::SetCountry(wxDateTime::Country_Default)
is enough. After some iterations, I came up with the following code, that shows a "short"
string (time only) if the referenced wxDateTime-object has the current date and a "long"
string (time and date, system default formatting) if it is a different date:

#include <wx/datetime.h>
#include <wx/intl.h>
wxString FormatDateTime(wxDateTime DateTime)
{
  wxString s;
  wxLocale Locale;
  Locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_CONV_ENCODING);
  if(DateTime.IsSameDate(wxDateTime::Today())) s=DateTime.FormatTime();
  else                                         s=DateTime.Format(wxT("%c"));
  return s;
}
class MyApp : public wxApp
{
public:
  virtual int OnRun()
  {
    wxString s;
    s=FormatDateTime(wxDateTime(14, wxDateTime::Apr, 2007, 10, 00, 00, 00));
    s=FormatDateTime(wxDateTime::Now()+wxTimeSpan(1, 0, 0, 0));
    return 0;
  }
}

Of course, you can make the wxLocale instance a member variable etc. - I just
wanted to keep the sample code short.

Thanks for your help!

Volker

-- 
mailto:  V B A R T H E L D at G M X dot D E






More information about the wx-users mailing list