Content-Transfer-Encoding: quoted-printable content-type: text/html; charset="utf-8" X-RT-Original-Encoding: gbk Content-Length: 11074
I think I encounter a problem when using the DIG command on the WINDOWS system, the command output contains some bytes which cannot be encoded in any formats, it should be either the time-zone name or time zone abbreviation, depending on registry settings.
My operating system is Windows with Chinese language package, time zone is the Chinese Standard Time.
My analysis of the code is as follows:
In the corresponding source code for the DIG program (BIND-9.11.2) dig.c Line 260, the library function strftime () is used.
if (strftime(time_str, sizeof(time_str),
"%a %b %d %H:%M:%S %Z %Y", &tmnow) > 0U)
printf(";; WHEN: %s\n", time_str);
It normally works fine, but on the Windows system with Chinese language package, it is not very platform neutral, the strftime function cannot get the correct time zone name by formatting the parameter %Z.
The time zone name is usually changed depending on the environment variable $TZ, but current systems don't use $TZ variable for any common purpose now. Nowadays, Linux / BSD make use of zoneinfo database which automatically handles GMT offset and DST, and $TZ can also be optionally set to location of these zoneinfo files under /usr/share/zoneinfo to override system setting. In my environment, the time zone name is CST (China Standard Time), written in English.
But the situation on Windows is different, the time zone name will change according to the settings of Control Panel. In my environment, Chinese characters is obtained:
In order to store the Chinese character encoding information correctly, you must use the wcsftime function rather than strftime, and set the first parameter type wchar_t * rather than char * . wcsftime is the wide-character equivalent of strftime; its string-pointer argument points to a wide-character string. strftime cannot return the correct encoding bytes, leading to the final output displaying Chinese characters incorrectly , because the bytes returned do not match any character encoding formats.
Interestingly, if you directly use the char * string copied from the _tzname structure, it shows Chinese characters correctly.
Here is my example code to debug on Windows and the output:
// strftime example
#include <stdio.h> // printf
#include <time.h> // time_t, struct tm, time, localtime, strftime
#include <string.h> // strcpy
int main()
{
time_t rawtime;
struct tm * timeinfo;
char buffer1[80];
wchar_t wide_char_buffer[80];
char buffer2[80];
time(&rawtime);
timeinfo = localtime(&rawtime);
wcsftime(wide_char_buffer, 80, L"%Z", timeinfo);
strftime(buffer1, 80, "%Z", timeinfo);
printf("%ls\n", wide_char_buffer); // output is OK, we got China Standard Time in Chinese
printf ("%s\n", buffer1); // output is abnormal, cannot recognize time zone information
strcpy(buffer2, _tzname[0]);
printf("%s\n", buffer2); // output is also OK
return 0;
}