[PowerRename] Add 12-hour time format patterns with AM/PM support (#30703) (#38723)

* [PowerRename][Feature] Add new date/time formatting patterns to GetDatedFileName

* [PowerRename][UI] Add date/time shortcut patterns to cheat sheet

* [PowerRename][Tests] Add tests for new date/time formatting patterns

* [PowerRename] [Refactor] Simplify AM/PM string handling in time patterns
This commit is contained in:
Abhyudit
2025-04-16 07:45:03 +05:30
committed by GitHub
parent 0e98cbd57e
commit 4e0db267dc
5 changed files with 152 additions and 7 deletions

View File

@@ -248,7 +248,19 @@ HRESULT GetTransformedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR sour
bool isFileTimeUsed(_In_ PCWSTR source)
{
bool used = false;
static const std::array patterns = { std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$Y" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$M" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$D" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$h" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$m" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$s" }, std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$f" } };
static const std::array patterns = {
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$Y" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$M" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$D" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$h" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$m" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$s" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$f" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$H" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$T" },
std::wregex{ L"(([^\\$]|^)(\\$\\$)*)\\$t" }
};
for (size_t i = 0; !used && i < patterns.size(); i++)
{
if (std::regex_search(source, patterns[i]))
@@ -275,6 +287,12 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY
StringCchCopy(localeName, LOCALE_NAME_MAX_LENGTH, L"en_US");
}
int hour12 = (fileTime.wHour % 12);
if (hour12 == 0)
{
hour12 = 12;
}
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%04d"), L"$01", fileTime.wYear);
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$YYYY"), replaceTerm);
@@ -316,6 +334,18 @@ HRESULT GetDatedFileName(_Out_ PWSTR result, UINT cchMax, _In_ PCWSTR source, SY
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", fileTime.wDay);
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$D"), replaceTerm);
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", hour12);
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$HH"), replaceTerm);
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%d"), L"$01", hour12);
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$H"), replaceTerm);
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", (fileTime.wHour < 12) ? L"AM" : L"PM");
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$TT"), replaceTerm);
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%s"), L"$01", (fileTime.wHour < 12) ? L"am" : L"pm");
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$tt"), replaceTerm);
StringCchPrintf(replaceTerm, MAX_PATH, TEXT("%s%02d"), L"$01", fileTime.wHour);
res = regex_replace(res, std::wregex(L"(([^\\$]|^)(\\$\\$)*)\\$hh"), replaceTerm);