0

I have a wxString which has a date as its value. The date format is stored depending on the regional setting or locale settings.

For eg. wxString dateStr = "9/10/2013" [dd/mm/yyyy format for Italy as regional locale setting].

When I parse the date string using wxDateTime::ParseDate(dateStr) and try to convert it in time_t using wxDateTime::GetTicks() function. But it swaps the value of day and month when the day is less than or equal to 12 for example 3/10/2013 or 12/11/2013. I am getting month as 3 and 12, and day as 10 and 11 respectively. But it works fine if the date is greater than 12 i.e 14/10/2013 or 28/10/2013.

I want to convert the above date string into time_t depending upon the locale setting. I am using windows as well as linux for development env.

Please help me out from this problem with an example or code snippet.

Gunjan49
  • 53
  • 1
  • 5
  • Perhaps it has something to do with your system's locale settings? If they are set to something like `en_US` then date formats will be interpreted in their stupid date format. – dreamlax Apr 01 '13 at 02:16
  • 1
    ISO 8601 FTW http://xkcd.com/1179/ – John Carter Apr 01 '13 at 02:22

1 Answers1

1

I suggest you use wxDateTime::ParseDateFormat instead, then you can specify the exact format of the date-string.

The reason you have problem with ParseDate is that it first tries to parse the date-string in American format (where the format is mm/dd/yyyy), and if it fails it tries other formats.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for your reply J. Pileborg, but I have an application which is suppose to run in different countries with different system locale setting, so I can't pass the date format in wxDateTime::ParseDateFormat as it should be picked from the locale setting of the system. Can you suggest some way that the locale setting is picked from the system regional setting and the date string is parsed using that date format. – Gunjan49 Apr 01 '13 at 13:11
  • wxDateTime::ParseFormat()[http://docs.wxwidgets.org/2.8/wx_wxdatetime.html#wxdatetimeparseformat] with "%x" as format can be used. – Gunjan49 Apr 01 '13 at 19:21
  • @Gunjan49 Yes, it uses the same formatting as [`std::strftime`](http://en.cppreference.com/w/cpp/chrono/c/strftime). – Some programmer dude Apr 02 '13 at 02:16