确定 ABAP 中的活动格式设置
Determining the active formatting settings in ABAP
正如 ABAP documentation of Formatting Settings 解释的那样:
The formatting settings are set as follows:
At the start of an internal session they are determined by the related default settings in the fixed values in the user master record of the current user.
Using the statement SET COUNTRY, this default setting for the current internal session can be overwritten using country-specific formats.
但是正如 ABAP documentation of SET COUNTRY
所表明的那样,无法查询使用此语句实际设置的内容:
Do not confuse the statement SET COUNTRY with the obsolete addition COUNTRY of the statement SET LOCALE LANGUAGE, used for setting the text environment. In particular, it does not have a corresponding GET COUNTRY statement.
确实,ABAP documentation of GET LOCALE
- obsolete parameters 提到:
The addition COUNTRY was intended for reading the country key of the current text environment explicitly. cntry expects a character-like data object. The function of this addition was not implemented in full and the result is undefined.
The addition COUNTRY of the statement GET LOCALE does not extract the formatting setting that can be set using SET COUNTRY.
这让我有点困惑。我可以使用 FM SUSR_GET_USER_DEFAULTS
确定我的用户默认设置。我可以从 table T005X
中找出国家/地区的设置。 但我无法弄清楚设置了哪个特定国家/地区格式,甚至if是在活动会话中设置的!
如何确定当前激活的格式设置?
奖金问题:有没有办法在调试器中解决这个问题?
也许你可以使用功能模块CLSE_SELECT_USR01
。
下面的例子:
REPORT test.
START-OF-SELECTION.
DATA: decimal_sign , separator.
PERFORM output.
SET COUNTRY 'US'.
PERFORM output.
FORM output.
CALL FUNCTION 'CLSE_SELECT_USR01'
* EXPORTING
* USERNAME = sy-uname
* IV_DELETE_BUFFER = ' '
IMPORTING
* X_USR01 =
* DATE_FORMAT =
decimal_sign = decimal_sign
separator = separator.
WRITE: / 'DECIMAL_SIGN', decimal_sign, 'separator', separator.
ENDFORM.
显示:
我的默认语言环境是德国,所以我得到了小数的实际设置。
来自您的评论:
Unfortunately I have to parse and analyse output data that's prepared for screen display from potentially dozens of different form sources.
您是在 运行 时间还是在之前的 运行 获得输出?因为没有时间机器从过去的电话中获取语言环境:)
ABAP 语句 SET COUNTRY
可能会更改日期格式、时间格式(自 ABAP 7.02 起)和数字格式,但官方没有相反的方法来获取当前活动的国家/地区代码(如您在您的问题,基于 ABAP 文档)。挺符合逻辑的,比如当前的数字格式可能和当前的国家代码不一样,所以最好直接测试你需要使用哪种格式,如下。
检测当前的日期格式,使用官方的方式returns一个字符,其可能的取值在[=20] =]):
DATA(current_date_format) = CL_ABAP_DATFM=>GET_DATFM( ).
检测当前时间格式,使用官方方式returns一个字符:
DATA(current_time_format) = CL_ABAP_TIMEFM=>GET_ENVIRONMENT_TIMEFM( ).
它returns以下值之一,示例值对应中午+5分10秒(至少11个字符输出时给出示例值):
- 0 : 12:05:10(0 到 23)
- 1 : 12:05:10 下午(0 到 12)
- 2 : 12:05:10 下午(0 到 12)
- 3 : 00:05:10 下午(0 到 11)
- 4 : 00:05:10 下午(0 到 11)
要检测当前的数字格式,根据@Gert Beukema的想法,您可以这样做:
DATA(current_number_format) = SWITCH usr01-dcpfm(
|{ 1000 NUMBER = ENVIRONMENT DECIMALS = 1 }|
WHEN '1.000,00' THEN ' '
WHEN '1,000.00' THEN 'X'
WHEN '1 000,00' THEN 'Y' ).
注意:此表达式返回的值
、X
和 Y
与表列 USR01-DCPFM
和 T005X-XDEZP
.
正如 ABAP documentation of Formatting Settings 解释的那样:
The formatting settings are set as follows:
At the start of an internal session they are determined by the related default settings in the fixed values in the user master record of the current user.
Using the statement SET COUNTRY, this default setting for the current internal session can be overwritten using country-specific formats.
但是正如 ABAP documentation of SET COUNTRY
所表明的那样,无法查询使用此语句实际设置的内容:
Do not confuse the statement SET COUNTRY with the obsolete addition COUNTRY of the statement SET LOCALE LANGUAGE, used for setting the text environment. In particular, it does not have a corresponding GET COUNTRY statement.
确实,ABAP documentation of GET LOCALE
- obsolete parameters 提到:
The addition COUNTRY was intended for reading the country key of the current text environment explicitly. cntry expects a character-like data object. The function of this addition was not implemented in full and the result is undefined.
The addition COUNTRY of the statement GET LOCALE does not extract the formatting setting that can be set using SET COUNTRY.
这让我有点困惑。我可以使用 FM SUSR_GET_USER_DEFAULTS
确定我的用户默认设置。我可以从 table T005X
中找出国家/地区的设置。 但我无法弄清楚设置了哪个特定国家/地区格式,甚至if是在活动会话中设置的!
如何确定当前激活的格式设置?
奖金问题:有没有办法在调试器中解决这个问题?
也许你可以使用功能模块CLSE_SELECT_USR01
。
下面的例子:
REPORT test.
START-OF-SELECTION.
DATA: decimal_sign , separator.
PERFORM output.
SET COUNTRY 'US'.
PERFORM output.
FORM output.
CALL FUNCTION 'CLSE_SELECT_USR01'
* EXPORTING
* USERNAME = sy-uname
* IV_DELETE_BUFFER = ' '
IMPORTING
* X_USR01 =
* DATE_FORMAT =
decimal_sign = decimal_sign
separator = separator.
WRITE: / 'DECIMAL_SIGN', decimal_sign, 'separator', separator.
ENDFORM.
显示:
我的默认语言环境是德国,所以我得到了小数的实际设置。
来自您的评论:
Unfortunately I have to parse and analyse output data that's prepared for screen display from potentially dozens of different form sources.
您是在 运行 时间还是在之前的 运行 获得输出?因为没有时间机器从过去的电话中获取语言环境:)
ABAP 语句 SET COUNTRY
可能会更改日期格式、时间格式(自 ABAP 7.02 起)和数字格式,但官方没有相反的方法来获取当前活动的国家/地区代码(如您在您的问题,基于 ABAP 文档)。挺符合逻辑的,比如当前的数字格式可能和当前的国家代码不一样,所以最好直接测试你需要使用哪种格式,如下。
检测当前的日期格式,使用官方的方式returns一个字符,其可能的取值在[=20] =]):
DATA(current_date_format) = CL_ABAP_DATFM=>GET_DATFM( ).
检测当前时间格式,使用官方方式returns一个字符:
DATA(current_time_format) = CL_ABAP_TIMEFM=>GET_ENVIRONMENT_TIMEFM( ).
它returns以下值之一,示例值对应中午+5分10秒(至少11个字符输出时给出示例值):
- 0 : 12:05:10(0 到 23)
- 1 : 12:05:10 下午(0 到 12)
- 2 : 12:05:10 下午(0 到 12)
- 3 : 00:05:10 下午(0 到 11)
- 4 : 00:05:10 下午(0 到 11)
要检测当前的数字格式,根据@Gert Beukema的想法,您可以这样做:
DATA(current_number_format) = SWITCH usr01-dcpfm( |{ 1000 NUMBER = ENVIRONMENT DECIMALS = 1 }| WHEN '1.000,00' THEN ' ' WHEN '1,000.00' THEN 'X' WHEN '1 000,00' THEN 'Y' ).
注意:此表达式返回的值
X
和Y
与表列USR01-DCPFM
和T005X-XDEZP
.