From 5faa7796d0d60b750bf97052a91e52233622e12d Mon Sep 17 00:00:00 2001 From: David Lowndes Date: Mon, 13 May 2024 21:04:06 +0100 Subject: [PATCH] Updated to V2.3.2.0 Rework FormatSystemTime as it wasn't handling strictly invalid dates well. By handling the return values it can at least return empty strings rather than rubbish. Handle the invalid date in FileTimeToText as SystemTimeToTzSpecificLocalTime would fail (with a bad date) even though FileTimeToSystemTime would succeed. --- .../dll/CppCustomVisualizer.rc | 8 ++-- .../dll/FileAndSystemTimeViz.cpp | 44 ++++++++++++------- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/CppCustomVisualizer/dll/CppCustomVisualizer.rc b/CppCustomVisualizer/dll/CppCustomVisualizer.rc index 501c131..79ffc5d 100644 --- a/CppCustomVisualizer/dll/CppCustomVisualizer.rc +++ b/CppCustomVisualizer/dll/CppCustomVisualizer.rc @@ -51,8 +51,8 @@ END // VS_VERSION_INFO VERSIONINFO - FILEVERSION 2,3,1,0 - PRODUCTVERSION 2,3,1,0 + FILEVERSION 2,3,2,0 + PRODUCTVERSION 2,3,2,0 FILEFLAGSMASK 0x3fL #ifdef _DEBUG FILEFLAGS 0x1L @@ -69,12 +69,12 @@ BEGIN BEGIN VALUE "CompanyName", "David Lowndes" VALUE "FileDescription", "Dave's Custom Visualizers" - VALUE "FileVersion", "2.3.1.0" + VALUE "FileVersion", "2.3.2.0" VALUE "InternalName", "DavesCustomVisualizer.dll" VALUE "LegalCopyright", "(c) David Lowndes 2024. All rights reserved." VALUE "OriginalFilename", "DavesCustomVisualizer.dll" VALUE "ProductName", "Dave's Custom Visualizers" - VALUE "ProductVersion", "2.3.1.0" + VALUE "ProductVersion", "2.3.2.0" END END BLOCK "VarFileInfo" diff --git a/CppCustomVisualizer/dll/FileAndSystemTimeViz.cpp b/CppCustomVisualizer/dll/FileAndSystemTimeViz.cpp index 3f81cd4..3b84420 100644 --- a/CppCustomVisualizer/dll/FileAndSystemTimeViz.cpp +++ b/CppCustomVisualizer/dll/FileAndSystemTimeViz.cpp @@ -12,22 +12,29 @@ static CString FormatSystemTime( const SYSTEMTIME& st ) TCHAR szDate[50]; // Use long date format to allow the day name to show if that's in that format. Short date formats don't include the day. - size_t NumCharsD = GetDateFormat( GetThreadLocale(), DATE_LONGDATE, &st, NULL, szDate, _countof( szDate ) ); + const size_t NumCharsD = GetDateFormat( GetThreadLocale(), DATE_LONGDATE, &st, NULL, szDate, _countof( szDate ) ); - TCHAR szTime[50]; + if ( NumCharsD == 0 ) + { + // Normally GetLastError is ERROR_INVALID_PARAMETER indicating that the SYSTEMTIME invalid. - auto NumCharsT = GetTimeFormat( GetThreadLocale(), TIME_FORCE24HOURFORMAT, &st, NULL, szTime, _countof( szTime ) ); + // Date is invalid, so clear the buffer + szDate[0] = _T('\0'); + } - CString str; + TCHAR szTime[50]; + + const auto NumCharsT = GetTimeFormat( GetThreadLocale(), TIME_FORCE24HOURFORMAT, &st, NULL, szTime, _countof( szTime ) ); - // If one of these isn't there, something's wrong, return an empty string - if ( (NumCharsD != 0) || (NumCharsT != 0) ) + if ( NumCharsT == 0 ) { - str = szDate; - str += _T( ' ' ); - str += szTime; + // Failed to get the time, so clear the buffer + szTime[0] = _T('\0'); } + CString str; + str.Format( _T( "%s %s" ), szDate, szTime ); + return str; } @@ -35,18 +42,23 @@ CString FileTimeToText( const FILETIME& ftUtc, UINT nBase ) { CString text; + bool bValidFileTime{ false }; + // convert to SystemTime SYSTEMTIME SysTime; if ( FileTimeToSystemTime( &ftUtc, &SysTime ) ) { - CString strUtc = FormatSystemTime( SysTime ); + const CString strUtc = FormatSystemTime( SysTime ); SYSTEMTIME lst; + // Even though FileTimeToSystemTime succeeds, and FormatSystemTime can + // return something, SystemTimeToTzSpecificLocalTime can fail if the + // time is invalid. So check the return value. if ( SystemTimeToTzSpecificLocalTime( NULL, &SysTime, &lst ) ) { - CString strLoc = FormatSystemTime( lst ); + const CString strLoc = FormatSystemTime( lst ); // Determine if there's a difference between local & utc so that I can display the most appropriate zone name LONGLONG diffInMins; @@ -65,13 +77,12 @@ CString FileTimeToText( const FILETIME& ftUtc, UINT nBase ) LPCWSTR ZoneName = diffInMins != 0 ? tzi.DaylightName : tzi.StandardName; text.Format( _T( "[utc] %s [%ls] %s" ), static_cast(strUtc), ZoneName, static_cast(strLoc) ); - } - else - { - text.Format( _T( "utc: %s ???" ), static_cast(strUtc) ); + + bValidFileTime = true; } } - else + + if ( !bValidFileTime ) { // Prior to VS2017 RC2, VS only ever passed 10 for nBase, newer versions should support base values of 2, 8, 10, or 16 char szHigh[33]; // 32 chars max for base 2 from a DWORD value @@ -100,7 +111,6 @@ CString FileTimeToText( const FILETIME& ftUtc, UINT nBase ) } text.Format( _T( "Invalid [%hs%hs:%hs%hs]" ), Prefix, szHigh, Prefix, szLow ); - // sprintf_s( pResult, BufferMax, ); } return text; }