瀏覽代碼

The %TIMESTAMP% syntax allows formating time strings for a time relative to present time

Source commit: cab8c70acd555994927b7aeb9008da1f778cd9a5
Martin Prikryl 9 年之前
父節點
當前提交
809e928548
共有 4 個文件被更改,包括 35 次插入17 次删除
  1. 10 6
      source/core/Common.cpp
  2. 1 1
      source/core/Common.h
  3. 1 1
      source/core/FileMasks.cpp
  4. 23 9
      source/windows/ConsoleRunner.cpp

+ 10 - 6
source/core/Common.cpp

@@ -1586,7 +1586,7 @@ __int64 __fastcall Round(double Number)
   return static_cast<__int64>(((Number - Floor) > (Ceil - Number)) ? Ceil : Floor);
 }
 //---------------------------------------------------------------------------
-bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime)
+bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime, bool Add)
 {
   S = S.Trim();
   int Index = 1;
@@ -1599,29 +1599,33 @@ bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime)
   bool Result = TryStrToInt(NumberStr, Number);
   if (Result)
   {
+    if (!Add)
+    {
+      Number = -Number;
+    }
     S.Delete(1, Index - 1);
     S = S.Trim().UpperCase();
     DateTime = Now();
     // These may not overlap with ParseSize (K, M and G)
     if (S == "S")
     {
-      DateTime = IncSecond(DateTime, -Number);
+      DateTime = IncSecond(DateTime, Number);
     }
     else if (S == "N")
     {
-      DateTime = IncMinute(DateTime, -Number);
+      DateTime = IncMinute(DateTime, Number);
     }
     else if (S == "H")
     {
-      DateTime = IncHour(DateTime, -Number);
+      DateTime = IncHour(DateTime, Number);
     }
     else if (S == "D")
     {
-      DateTime = IncDay(DateTime, -Number);
+      DateTime = IncDay(DateTime, Number);
     }
     else if (S == "Y")
     {
-      DateTime = IncYear(DateTime, -Number);
+      DateTime = IncYear(DateTime, Number);
     }
     else
     {

+ 1 - 1
source/core/Common.h

@@ -125,7 +125,7 @@ bool __fastcall IsWin10();
 bool __fastcall IsWine();
 TLibModule * __fastcall FindModule(void * Instance);
 __int64 __fastcall Round(double Number);
-bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime);
+bool __fastcall TryRelativeStrToDateTime(UnicodeString S, TDateTime & DateTime, bool Add);
 LCID __fastcall GetDefaultLCID();
 UnicodeString __fastcall DefaultEncodingName();
 UnicodeString __fastcall WindowsProductName();

+ 1 - 1
source/core/FileMasks.cpp

@@ -585,7 +585,7 @@ void __fastcall TFileMasks::CreateMask(
 
       TDateTime Modification;
       if (TryStrToDateTime(PartStr, Modification, FormatSettings) ||
-          TryRelativeStrToDateTime(PartStr, Modification))
+          TryRelativeStrToDateTime(PartStr, Modification, false))
       {
         TMask::TMaskBoundary & ModificationMask =
           (Low ? Mask.LowModificationMask : Mask.HighModificationMask);

+ 23 - 9
source/windows/ConsoleRunner.cpp

@@ -1877,18 +1877,32 @@ UnicodeString TConsoleRunner::ExpandCommand(UnicodeString Command, TStrings * Sc
   int P2;
   do
   {
-    int P = Pos(UpperCase(L"%" + TimestampVarName + L"#"), UpperCase(Command.SubString(Offset, Command.Length() - Offset + 1)));
+    int P = Pos(UpperCase(L"%" + TimestampVarName), UpperCase(Command), Offset);
     if (P > 0)
     {
-      P += Offset - 1;
-      Offset = P + 1 + TimestampVarName.Length() + 1;
-      P2 = Pos(L"%", Command.SubString(Offset, Command.Length() - Offset + 1));
-      if (P2 > 0)
+      Offset = P + 1 + TimestampVarName.Length();
+      P2 = Pos(L"%", Command, Offset);
+      int P3 = Pos(L"#", Command, Offset);
+      if ((P2 > 0) && (P3 > 0) && (P3 < P2) &&
+          ((P3 == Offset) || (Command[Offset] == L'+') || (Command[Offset] == L'-')))
       {
-        UnicodeString TimestampFormat = Command.SubString(Offset, P2 - 1);
-        UnicodeString TimestampValue = FormatDateTime(TimestampFormat, N);
-        Command = Command.SubString(1, P - 1) + TimestampValue + Command.SubString(Offset + P2, Command.Length() - Offset - P2 + 1);
-        Offset = P + TimestampValue.Length();
+        bool Valid = true;
+        TDateTime T = N;
+        if (P3 > Offset)
+        {
+          bool Add = (Command[Offset] == L'+');
+          Offset++;
+          Valid = TryRelativeStrToDateTime(Command.SubString(Offset, P3 - Offset), T, Add);
+        }
+
+        Offset = P3 + 1;
+        if (Valid)
+        {
+          UnicodeString TimestampFormat = Command.SubString(Offset, P2 - Offset);
+          UnicodeString TimestampValue = FormatDateTime(TimestampFormat, T);
+          Command = Command.SubString(1, P - 1) + TimestampValue + Command.SubString(P2 + 1, Command.Length() - P2);
+          Offset = P + TimestampValue.Length();
+        }
       }
     }
     else