views:

349

answers:

2

I need to match on a date using the LIKE operator. The date should be in current user's local format, not in the general format.

So I use the strftime function like this:

WHERE strftime('%d.%m.%Y %H:%M', createdDate, 'localtime') LIKE '%{searchTerm}%'

Unfortunately this works only for fixed format '%d.%m.%Y %H:%M'. But I want to use the user's current locale format.

So I need to either: 1) Get the strftime format string from a C++ locale object 2) Make SQLite format the date using current locale itself

Spent already several hours on this to no avail. Would be grateful if anyone would point me in the right direction on how to tackle this problem.

Example: Given current locale is German I want to get something like "%d.%m.%Y %H:%m". For an US locale I want to get "%m/%d/%Y %H:%m"

+1  A: 

Normally the local date format can be obtained with the Windows GetDateFormat API (or GetDateFormatEx API for Vista). Your program could interrogate the API then transform the date accordingly. Following that, the date can be recorded in SQLite.

However, once can question the validity of storing timestamps in a specific format. That basically means a lot of code to manipulate each date, or no date manipulation at all. May I suggest, if it is possible, storing in a plain format (say ISO or UNIX timestamp) and working from that, outputing with whichever flavour of GetDateFormat is required?

MPelletier
I already store in the '2010-02-25 11:19' format, I just want the user to search by date using LIKE on his native format and I don't know how to do that.
Alex Jenter
Then you should let your framework do that. If, for example, you use a .NET DateTimePicker, it displays the time to the user in their local format, but the internal time is in a conventional form. You get the internal time and run the query based on that. Even for a LIKE clause, it is better to wrap it in a control than to record in a database. Much easier to maintain.
MPelletier
So you mean I need to try to transform the partial date entered by the user and then run the LIKE search? The search is incremental and done after each keystroke, I doubt there's an easy way to do this..
Alex Jenter
It's just a suggestion. The GetDateFormatEx API is still a possibility, and perhaps you can build its result into your query. However, I'm a database purist, my rule is don't record it if you can derive it from another field on the same entry. What is your GUI control? Is it a masked field? Do you *trust* the user to enter the date unattended in the correct format all the time? If you have a mask, then you can determine which parts are being filled, and build a partial ISO date out of that, then run *that* in your LIKE query. Transform the result to local and show those.
MPelletier
It's a plain edit control, if the user makes a mistake there will be no match and that's all.I guess it could be possible to pre-fill a TEMP TABLE with localized dates and then do a LIKE search on that? There will be about 2x100,000 of time records max.
Alex Jenter
GetDateFormat doesn't quite fit the bill since it is platform-specific and just formats dates and doesn't allow to get the "format picture string". If go platform-dependent, it will be easier to read the format directly from the registry (value HKCU/Control Panel/International/sShortDate).
Alex Jenter
Yes, you could do it with a temp table or a view.
MPelletier
Thanks for your help!
Alex Jenter
A: 

OK, different answer.

Suppose you have your MyTable:

CREATE TABLE MyTable (
    MyPrimaryKeyHnd INTEGER PRIMARY KEY,
    ...
    CreatedDate TEXT);

(Where CreatedDate is in ISO format. I suppose you could also use a Unix timestamp. Your choice.)

Then a list of possible formats

CREATE TABLE TimeFormat (
    TimeFormatHnd INTEGER PRIMARY KEY,
    TimeFormatString TEXT NOT NULL,
    TimeFormatDescriptor TEXT);

You allow your user to chose a format of their choice and keep that in a seperate table or INI file. TimeformatString would be your strftime() compatible format string (such as '%d.%m.%Y %H:%M'). You just need to build your query with whatever the user's choice is.

MPelletier
This means I'll have to build some extra UI to let the user choose the format. This is unnecessary, the user has already chosen the preferred format in the Control Panel and applications should respect this choice. I just need to use the system date/time format.
Alex Jenter