动态设置系统日期格式
1 问题提出
在操作数据库表的时候,通常需要对表中的日期类型的数据做处理,对于系统来说存在多种
日期格式,数据库的日期字段也存在多种格式,当往数据库表中插入日期字段的时候必须两者的日期
格式匹配才能插入数据,在这种情况下,一般需要更改系统的日期格式,改成与数据库表中的日期字段
一致的格式(因为表的设计已经定好,对于日期的格式也已经唯一确定,所以一般情况下都是更改系统
的日期格式),那么就必须能够在程序中自动改变系统的日期格式,使其和表的日期格式匹配!
2 程序中修改日期格式的系统api的说明
主要需要使用下面的windows api
bool setlocaleinfo(
lcid locale, // locale identifier
lctype lctype, // type of information to set
lpctstr lplcdata // pointer to information to set
);
本函数主要用于windows系统中设置系统的区域选项,主要包括时间,语言等选项的设置。
lcid:locale identifier (区域标志)
在系统中有两个默认值:
locale_system_default:系统默认的区域选项;
locale_user_default:当前用户的区域选项;
同时用户也可以通过 makelcid宏动态创建lcid;
lctype:需要设定的系统信息类型;
主要包括下面的值:
locale_icalendartype locale_sdate
locale_icurrdigits locale_sdecimal
locale_icurrency locale_sgrouping
locale_idigits locale_slist
locale_ifirstdayofweek locale_slongdate
locale_ifirstweekofyear locale_smondecimalsep
locale_ilzero locale_smongrouping
locale_imeasure locale_smonthousandsep
locale_inegcurr locale_snegativesign
locale_inegnumber locale_spositivesign
locale_itime locale_sshortdate
locale_s1159 locale_sthousand
locale_s2359 locale_stime
locale_scurrency locale_stimeformat
lplcdata:需要设定的信息的存放地址;
与起对应的函数为
int getlocaleinfo(
lcid locale, // locale identifier
lctype lctype, // type of information
lptstr lplcdata, // address of buffer for information
int cchdata // size of buffer
);
主要用于取得当前系统的区域设置,各个参数和使用过程不再一一说明。
3 使用举例
//取得当前系统的短日期格式;
function tfrmmain.getsysdateformat: string;
var
sgs:string;
begin
setlength(sgs,12);
getlocaleinfo(locale_system_default,locale_sshortdate ,pchar(sgs),12);
result:=string(pchar(sgs));
end;
//设定系日期格式;
procedure tfrmmain.setsysdateformat(s: string);
begin
setlocaleinfo(locale_system_default,locale_sshortdate,pchar(s));
end;