lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Today, I wanted to test the C setlocale function, and thought to use Lua
that implement it and strftime, to get quick results.
It worked well, except for one point:
when called with a NULL locale, setlocale must return the current locale and
don't change it.
The current implementation of Lua' setlocale don't allow to give a NULL
pointer.
So I checked the source, and tried to change the line:

  lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));

into:

  lua_pushstring(L, setlocale(cat[op], lua_tostring(L, 1)));

because if the argument 1 is incorrect, lua_tostring returns NULL, which is
what I want. luaL_check_string raises an error in this case.

Now, I can write:

print(setlocale(nil, "all")) -- Report the default locale, always "C"
print(date("%B"))
print(date("%#c"))
print(date("%#x"))
print(setlocale("", "all")) -- Set to the user preference
print(date("%B"))
print(date("%#c"))
print(date("%#x"))
print(setlocale({}, "all"))

Result on my system (WinNT in English, with French locale):
C
January
Monday, January 28, 2002 17:59:37
Monday, January 28, 2002
French_France.1252
janvier
lundi 28 janvier 2002 17:59:37
lundi 28 janvier 2002
French_France.1252

Note that I can use an invalid string, here a table, in place of nil. This
can be bad, but it is not very annoying IMO.

I hope this small patch, if it doesn't lead to other problems, will go in
the official code...

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

Sent through GMX FreeMail - http://www.gmx.net