Browse Source

Bug 2236: Shorter and more friendly formatting of long time intervals

https://winscp.net/tracker/2236

Source commit: d3b1b217bea47a81b2b6bc413eb5184f7c582933
Martin Prikryl 1 year ago
parent
commit
ccaddd6380

+ 15 - 6
source/core/Common.cpp

@@ -3263,16 +3263,25 @@ UnicodeString __fastcall FormatSize(__int64 Size)
   return FormatNumber(Size);
 }
 //---------------------------------------------------------------------------
-UnicodeString __fastcall FormatDateTimeSpan(const UnicodeString TimeFormat, TDateTime DateTime)
+UnicodeString FormatDateTimeSpan(const TDateTime & DateTime)
 {
   UnicodeString Result;
-  if (int(DateTime) > 0)
+  if ((0 <= DateTime) && (DateTime <= MaxDateTime))
   {
-    Result = IntToStr(int(DateTime)) + L", ";
+    TTimeStamp TimeStamp = DateTimeToTimeStamp(DateTime);
+    int Days = TimeStamp.Date - DateDelta;
+    if (abs(Days) >= 4)
+    {
+      Result = FMTLOAD(DAYS_SPAN, (Days));
+    }
+    else
+    {
+      unsigned short Hour, Min, Sec, Dummy;
+      DecodeTime(DateTime, Hour, Min, Sec, Dummy);
+      int TotalHours = static_cast<int>(Hour) + (Days * HoursPerDay);
+      Result = FORMAT(L"%d%s%.2d%s%.2d", (TotalHours, FormatSettings.TimeSeparator, Min, FormatSettings.TimeSeparator, Sec));
+    }
   }
-  // days are decremented, because when there are too many of them,
-  // "integer overflow" error occurs
-  Result += FormatDateTime(TimeFormat, DateTime - int(DateTime));
   return Result;
 }
 //---------------------------------------------------------------------------

+ 1 - 1
source/core/Common.h

@@ -275,7 +275,7 @@ int __fastcall CompareFileTime(TDateTime T1, TDateTime T2);
 int __fastcall TimeToMSec(TDateTime T);
 int __fastcall TimeToSeconds(TDateTime T);
 int __fastcall TimeToMinutes(TDateTime T);
-UnicodeString __fastcall FormatDateTimeSpan(const UnicodeString TimeFormat, TDateTime DateTime);
+UnicodeString FormatDateTimeSpan(const TDateTime & DateTime);
 UnicodeString FormatRelativeTime(const TDateTime & ANow, const TDateTime & AThen, bool DateOnly);
 TStrings * TlsCipherList();
 //---------------------------------------------------------------------------

+ 1 - 1
source/core/FileOperationProgress.cpp

@@ -977,7 +977,7 @@ UnicodeString __fastcall TFileOperationProgressType::GetLogStr(bool Done)
     Time = TimeElapsed();
     TimeLabel = L"Elapsed";
   }
-  UnicodeString TimeStr = FormatDateTimeSpan(Configuration->TimeFormat, Time);
+  UnicodeString TimeStr = FormatDateTimeSpan(Time);
 
   unsigned int ACPS;
   if (!Done)

+ 3 - 3
source/forms/CustomScpExplorer.cpp

@@ -5129,7 +5129,7 @@ void __fastcall TCustomScpExplorerForm::UpdateStatusBar()
   if ((ActiveTerminal != NULL) && (ActiveTerminal->Status == ssOpened))
   {
     const TSessionInfo & SessionInfo = ActiveTerminal->GetSessionInfo();
-    LoginTime = FormatDateTimeSpan(Configuration->TimeFormat, Now() - SessionInfo.LoginTime);
+    LoginTime = FormatDateTimeSpan(Now() - SessionInfo.LoginTime);
   }
   TTBXStatusPanel * DurationStatusPanel = SessionStatusBar->Panels->Items[SessionStatusBar->Panels->Count - 1];
   DurationStatusPanel->Caption = LoginTime;
@@ -6339,8 +6339,8 @@ void __fastcall TCustomScpExplorerForm::FullSynchronize(
     TDateTime Elapsed = (Now() - Start);
     Message +=
       L"\n" +
-      FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Configuration->TimeFormat, Params.CollectElapsed))) + L"\n" +
-      FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Configuration->TimeFormat, Elapsed)));
+      FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 5), (FormatDateTimeSpan(Params.CollectElapsed))) + L"\n" +
+      FORMAT(LoadStrPart(SYNCHRONIZE_SUMMARY, 6), (FormatDateTimeSpan(Elapsed)));
     TMessageParams Params(mpNeverAskAgainCheck);
     unsigned int Result = MessageDialog(Message, qtInformation, qaOK, HELP_NONE, &Params);
     if (Result == qaNeverAskAgain)

+ 2 - 2
source/forms/Progress.cpp

@@ -362,7 +362,7 @@ void __fastcall TProgressForm::UpdateControls()
         {
           TimeLeft = FData.TotalTimeLeft();
         }
-        TimeLeftCaption = FormatDateTimeSpan(Configuration->TimeFormat, TimeLeft);
+        TimeLeftCaption = FormatDateTimeSpan(TimeLeft);
       }
       else
       {
@@ -371,7 +371,7 @@ void __fastcall TProgressForm::UpdateControls()
       TimeLeftLabel->Caption = TimeLeftCaption;
     }
     TDateTime Elapsed = FData.TimeElapsed();
-    TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, Elapsed);
+    TimeElapsedLabel->Caption = FormatDateTimeSpan(Elapsed);
     BytesTransferredLabel->Caption = FormatBytes(FData.TotalTransferred);
     int CPS = FData.CPS();
     CPSLabel->Caption = FORMAT(L"%s/s", (FormatBytes(CPS)));

+ 2 - 2
source/forms/SynchronizeProgress.cpp

@@ -124,13 +124,13 @@ void __fastcall TSynchronizeProgressForm::UpdateControls()
     Elapsed = EncodeTimeVerbose(0, 0, 0, 0);
   }
   Caption = ACaption;
-  TimeElapsedLabel->Caption = FormatDateTimeSpan(Configuration->TimeFormat, Elapsed);
+  TimeElapsedLabel->Caption = FormatDateTimeSpan(Elapsed);
   UnicodeString TimeLeftCaption;
   int Position = OperationProgress->Position;
   if (FStarted && CanShowTimeEstimate(FStartTime) && (Position > 0))
   {
     TDateTime TimeLeft = TDateTime(double(double(Elapsed) * (OperationProgress->Max - Position) / Position));
-    TimeLeftCaption = FormatDateTimeSpan(Configuration->TimeFormat, TimeLeft);
+    TimeLeftCaption = FormatDateTimeSpan(TimeLeft);
   }
   else
   {

+ 1 - 0
source/resource/TextsCore.h

@@ -530,6 +530,7 @@
 #define PUBLIC_KEY_UPLOADED     567
 #define PUBLIC_KEY_PERMISSIONS  568
 #define TIME_RELATIVE           569
+#define DAYS_SPAN               570
 
 #define CORE_VARIABLE_STRINGS   600
 #define PUTTY_BASED_ON          601

+ 1 - 0
source/resource/TextsCore1.rc

@@ -503,6 +503,7 @@ BEGIN
   PUBLIC_KEY_UPLOADED, "**Public key \"%s\" was installed.**\n\nYou can now login to the server using the key pair."
   PUBLIC_KEY_PERMISSIONS, "Though potentially wrong permissions of \"%s\" file and/or its parent folder were detected. Please check them."
   TIME_RELATIVE, "just now|today|yesterday|tomorrow|one second ago|%d seconds ago|one minute ago|%d minutes ago|one hour ago|%d hours ago|one day ago|%d days ago|one week ago|%d weeks ago|one month ago|%d months ago|one year ago|%d years ago"
+  DAYS_SPAN, "%d days"
 
   CORE_VARIABLE_STRINGS, "CORE_VARIABLE"
   PUTTY_BASED_ON, "SSH and SCP code based on PuTTY %s"

+ 2 - 2
source/windows/QueueController.cpp

@@ -280,11 +280,11 @@ static UnicodeString GetTime(TFileOperationProgressType * ProgressData)
   UnicodeString Result;
   if (ProgressData->TotalSizeSet)
   {
-    Result = FormatDateTimeSpan(Configuration->TimeFormat, ProgressData->TotalTimeLeft());
+    Result = FormatDateTimeSpan(ProgressData->TotalTimeLeft());
   }
   else
   {
-    Result = FormatDateTimeSpan(Configuration->TimeFormat, ProgressData->TimeElapsed());
+    Result = FormatDateTimeSpan(ProgressData->TimeElapsed());
   }
   return Result;
 }