Module: wine Branch: master Commit: ae8b7109f050754d95c15752741fa29059cd278e URL: https://gitlab.winehq.org/wine/wine/-/commit/ae8b7109f050754d95c15752741fa29...
Author: Akihiro Sagawa sagawa.aki@gmail.com Date: Tue Sep 19 22:39:10 2023 +0900
cmd: Always outputs leading zeros when listing file time information.
---
programs/cmd/directory.c | 118 +++++++++++++++++++++++++++++++++++++++-- programs/cmd/tests/directory.c | 4 -- 2 files changed, 114 insertions(+), 8 deletions(-)
diff --git a/programs/cmd/directory.c b/programs/cmd/directory.c index 6efcbb3a841..4d3ae3f9c7d 100644 --- a/programs/cmd/directory.c +++ b/programs/cmd/directory.c @@ -41,6 +41,10 @@ typedef enum _DISPLAYORDER Date } DISPLAYORDER;
+#define MAX_DATETIME_FORMAT 80 + +static WCHAR date_format[MAX_DATETIME_FORMAT * 2]; +static WCHAR time_format[MAX_DATETIME_FORMAT * 2]; static int file_total, dir_total, max_width; static ULONGLONG byte_total; static DISPLAYTIME dirTime; @@ -345,8 +349,10 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le FileTimeToLocalFileTime (&fd[i].ftCreationTime, &ft); } FileTimeToSystemTime (&ft, &st); - GetDateFormatW(0, DATE_SHORTDATE, &st, NULL, datestring, ARRAY_SIZE(datestring)); - GetTimeFormatW(0, TIME_NOSECONDS, &st, NULL, timestring, ARRAY_SIZE(timestring)); + GetDateFormatW(LOCALE_USER_DEFAULT, 0, &st, date_format, + datestring, ARRAY_SIZE(datestring)); + GetTimeFormatW(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &st, time_format, + timestring, ARRAY_SIZE(timestring));
if (wide) {
@@ -375,7 +381,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le dir_count++;
if (!bare) { - WCMD_output (L"%1!10s! %2!8s! <DIR> ", datestring, timestring); + WCMD_output (L"%1 %2 <DIR> ", datestring, timestring); if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName); if (usernames) WCMD_output(L"%1!-23s!", username); WCMD_output(L"%1",fd[i].cFileName); @@ -394,7 +400,7 @@ static DIRECTORY_STACK *WCMD_list_directory (DIRECTORY_STACK *inputparms, int le file_size.u.HighPart = fd[i].nFileSizeHigh; byte_count.QuadPart += file_size.QuadPart; if (!bare) { - WCMD_output (L"%1!10s! %2!8s! %3!10s! ", datestring, timestring, + WCMD_output (L"%1 %2 %3!14s! ", datestring, timestring, WCMD_filesize64(file_size.QuadPart)); if (shortname) WCMD_output(L"%1!-13s!", fd[i].cAlternateFileName); if (usernames) WCMD_output(L"%1!-23s!", username); @@ -523,6 +529,107 @@ static void WCMD_dir_trailer(const WCHAR *path) { } }
+/* Get the length of a date/time formatting pattern */ +/* copied from dlls/kernelbase/locale.c */ +static int get_pattern_len( const WCHAR *pattern, const WCHAR *accept ) +{ + int i; + + if (*pattern == ''') + { + for (i = 1; pattern[i]; i++) + { + if (pattern[i] != ''') continue; + if (pattern[++i] != ''') return i; + } + return i; + } + if (!wcschr( accept, *pattern )) return 1; + for (i = 1; pattern[i]; i++) if (pattern[i] != pattern[0]) break; + return i; +} + +/* Initialize date format to use abbreviated one with leading zeros */ +static void init_date_format(void) +{ + WCHAR sys_format[MAX_DATETIME_FORMAT]; + int src_pat_len, dst_pat_len; + const WCHAR *src; + WCHAR *dst = date_format; + + GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SSHORTDATE, sys_format, ARRAY_SIZE(sys_format)); + + for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) { + src_pat_len = dst_pat_len = get_pattern_len(src, L"yMd"); + + switch (*src) + { + case ''': + wmemcpy(dst, src, src_pat_len); + break; + + case 'd': + case 'M': + if (src_pat_len == 4) /* full name */ + dst_pat_len--; /* -> use abbreviated one */ + /* fallthrough */ + case 'y': + if (src_pat_len == 1) /* without leading zeros */ + dst_pat_len++; /* -> with leading zeros */ + wmemset(dst, *src, dst_pat_len); + break; + + default: + *dst = *src; + break; + } + } + *dst = '\0'; + + TRACE("date format: %s\n", wine_dbgstr_w(date_format)); +} + +/* Initialize time format to use leading zeros */ +static void init_time_format(void) +{ + WCHAR sys_format[MAX_DATETIME_FORMAT]; + int src_pat_len, dst_pat_len; + const WCHAR *src; + WCHAR *dst = time_format; + + GetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, sys_format, ARRAY_SIZE(sys_format)); + + for (src = sys_format; *src; src += src_pat_len, dst += dst_pat_len) { + src_pat_len = dst_pat_len = get_pattern_len(src, L"Hhmst"); + + switch (*src) + { + case ''': + wmemcpy(dst, src, src_pat_len); + break; + + case 'H': + case 'h': + case 'm': + case 's': + if (src_pat_len == 1) /* without leading zeros */ + dst_pat_len++; /* -> with leading zeros */ + /* fallthrough */ + case 't': + wmemset(dst, *src, dst_pat_len); + break; + + default: + *dst = *src; + break; + } + } + *dst = '\0'; + + /* seconds portion will be dropped by TIME_NOSECONDS */ + TRACE("time format: %s\n", wine_dbgstr_w(time_format)); +} + /***************************************************************************** * WCMD_directory * @@ -727,6 +834,9 @@ void WCMD_directory (WCHAR *args) WCMD_enter_paged_mode(NULL); }
+ init_date_format(); + init_time_format(); + argno = 0; argN = args; GetCurrentDirectoryW(MAX_PATH, cwd); diff --git a/programs/cmd/tests/directory.c b/programs/cmd/tests/directory.c index 6644bc52b35..1a62ce120ca 100644 --- a/programs/cmd/tests/directory.c +++ b/programs/cmd/tests/directory.c @@ -163,7 +163,6 @@ static BOOL match_timestamp(const char* line, void *data) (!strncmp(ptr, "dd", 2) || !strncmp(ptr, "d", 1))) { sprintf(pattern, "%02hd", param->st.wDay); - todo_wine_if(strncmp(ptr, "dd", 2)) ok(!!strstr(line, pattern), "expected day %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -173,7 +172,6 @@ static BOOL match_timestamp(const char* line, void *data) (!strncmp(ptr, "MM", 2) || !strncmp(ptr, "M", 1))) { sprintf(pattern, "%02hd", param->st.wMonth); - todo_wine_if(strncmp(ptr, "MM", 2)) ok(!!strstr(line, pattern), "expected month %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -183,7 +181,6 @@ static BOOL match_timestamp(const char* line, void *data) if (strstr(format, "h") || strstr(format, "H")) { sprintf(pattern, "%02hd", param->st.wHour); - todo_wine_if(!strstr(format, "hh") && !strstr(format, "HH")) ok(!!strstr(line, pattern), "expected hour %s, got %s\n", pattern, wine_dbgstr_a(line)); } else @@ -192,7 +189,6 @@ static BOOL match_timestamp(const char* line, void *data) if (strstr(format, "m")) { sprintf(pattern, "%02hd", param->st.wMinute); - todo_wine_if(!strstr(format, "mm")) ok(!!strstr(line, pattern), "expected minute %s, got %s\n", pattern, wine_dbgstr_a(line)); } else