Locale-dependent formatstring for ordering custom dateformat

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
zorg
Party member
Posts: 3470
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Locale-dependent formatstring for ordering custom dateformat

Post by zorg »

S0lll0s wrote:you can use os.setlocale() (without arguments) to get the current locale settings.
Nice, i missed that functionality, thanks!

Still, this just only means that i can get the used time locale; still have the issue of not being able to grab the format strings, except from an external table like ivan's.. but i guess i can work with that.

I do thank everyone for the help, though i'd like to not set the topic title to solved just yet, maybe someone else might have further ideas; let's say i'll change it next week. :3
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Locale-dependent formatstring for ordering custom dateformat

Post by s-ol »

I just took a look at the BSD strftime.c, and pieced together that all the info is stored in the locale defs on every system. With C you can get the so called d_t_fmt and d_fmt's that are basically format strings with the "lower level" escapes (nl_langinfo(D_T_FMT)).

On my system the local def for german for example is located in /usr/share/i18n/locales/de_DE, here's the LC_TIME section:

Code: Select all

LC_TIME
abday	"<U0053><U006F>";"<U004D><U006F>";/
	"<U0044><U0069>";"<U004D><U0069>";/
	"<U0044><U006F>";"<U0046><U0072>";/
	"<U0053><U0061>"
day	"<U0053><U006F><U006E><U006E><U0074><U0061><U0067>";/
	"<U004D><U006F><U006E><U0074><U0061><U0067>";/
	"<U0044><U0069><U0065><U006E><U0073><U0074><U0061><U0067>";/
	"<U004D><U0069><U0074><U0074><U0077><U006F><U0063><U0068>";/
	"<U0044><U006F><U006E><U006E><U0065><U0072><U0073><U0074><U0061><U0067>";/
	"<U0046><U0072><U0065><U0069><U0074><U0061><U0067>";/
	"<U0053><U0061><U006D><U0073><U0074><U0061><U0067>"
abmon	"<U004A><U0061><U006E>";"<U0046><U0065><U0062>";/
	"<U004D><U00E4><U0072>";"<U0041><U0070><U0072>";/
	"<U004D><U0061><U0069>";"<U004A><U0075><U006E>";/
	"<U004A><U0075><U006C>";"<U0041><U0075><U0067>";/
	"<U0053><U0065><U0070>";"<U004F><U006B><U0074>";/
	"<U004E><U006F><U0076>";"<U0044><U0065><U007A>"
mon	"<U004A><U0061><U006E><U0075><U0061><U0072>";/
	"<U0046><U0065><U0062><U0072><U0075><U0061><U0072>";/
	"<U004D><U00E4><U0072><U007A>";/
	"<U0041><U0070><U0072><U0069><U006C>";/
	"<U004D><U0061><U0069>";/
	"<U004A><U0075><U006E><U0069>";/
	"<U004A><U0075><U006C><U0069>";/
	"<U0041><U0075><U0067><U0075><U0073><U0074>";/
	"<U0053><U0065><U0070><U0074><U0065><U006D><U0062><U0065><U0072>";/
	"<U004F><U006B><U0074><U006F><U0062><U0065><U0072>";/
	"<U004E><U006F><U0076><U0065><U006D><U0062><U0065><U0072>";/
	"<U0044><U0065><U007A><U0065><U006D><U0062><U0065><U0072>"
am_pm   "";""
% Appropriate date and time representation (%c)
%	"%a %d %b %Y %T %Z"
d_t_fmt "<U0025><U0061><U0020><U0025><U0064><U0020><U0025><U0062><U0020><U0025><U0059><U0020><U0025><U0054><U0020><U0025><U005A>"
%
% Appropriate date representation (%x)
%	"%d.%m.%Y"
d_fmt   "<U0025><U0064><U002E><U0025><U006D><U002E><U0025><U0059>"
%
% Appropriate time representation (%X)
%	"%T"
t_fmt   "<U0025><U0054>"
t_fmt_ampm ""
date_fmt	"<U0025><U0061><U0020><U0025><U002D><U0064><U002E><U0020>/
<U0025><U0062><U0020><U0025><U0048><U003A><U0025><U004D><U003A><U0025><U0053>/
<U0020><U0025><U005A><U0020><U0025><U0059>"

week    7;19971130;4
first_weekday 2
first_workday 2
END LC_TIME
the first part are just the weekday and month names in unicode encoding, the part that you are interested, again, are the d_t_fmt, d_fmt and t_fmt (for %c, %x and %X respectively, as stated in the comments).

I think your two options are basically:
a) use ffi and get the appropriate string
b) compile a list of the most common locales (that might not always be accurate to what is the actual value on the system but I doubt these change a lot as the date format for every locale is a pretty much set-in-stone I think).

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Locale-dependent formatstring for ordering custom dateformat

Post by s-ol »

you hooked me, been trying to get it done the "proper" way but it never returns anything?

Code: Select all

local ffi = require "ffi"

ffi.cdef[[
  typedef  int nl_item;
	char	*nl_langinfo(nl_item);
]]

local langinfo = {
  AM_STR      = 131110,
  PM_STR      = 131111,
  D_T_FMT     = 131112,
  D_FMT       = 131113,
  T_FMT       = 131114,
  T_FMT_AMPM  = 131115,
  AM_STR      = 131116,
  PM_STR      = 131117
}

for key, id in pairs(langinfo) do
  local str = ffi.string(ffi.C.nl_langinfo(id))
  print(key, str)
  langinfo[key] = str
end

return langinfo
Edit: wow, I should obviously rather look at my own headers, not some random ones. updated above so it works!

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests