Selaa lähdekoodia

* Reworked patch from Werner Pamler (bug ID 26168) to add support for periods

git-svn-id: trunk@27801 -
michael 11 vuotta sitten
vanhempi
commit
046b7626dd
2 muutettua tiedostoa jossa 50 lisäystä ja 17 poistoa
  1. 34 11
      rtl/objpas/sysutils/dati.inc
  2. 16 6
      rtl/objpas/sysutils/datih.inc

+ 34 - 11
rtl/objpas/sysutils/dati.inc

@@ -872,24 +872,26 @@ end;
 
 
 {   FormatDateTime formats DateTime to the given format string FormatStr   }
 {   FormatDateTime formats DateTime to the given format string FormatStr   }
 
 
-function FormatDateTime(const FormatStr: string; DateTime: TDateTime): string;
+function FormatDateTime(const FormatStr: string; DateTime: TDateTime; Options : TFormatDateTimeOptions = []): string;
 begin
 begin
-  DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings);
+  DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings,Options);
 end;
 end;
 
 
-function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
+function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings; Options : TFormatDateTimeOptions = []): string;
 begin
 begin
-  DateTimeToString(Result, FormatStr, DateTime, FormatSettings);
+  DateTimeToString(Result, FormatStr, DateTime, FormatSettings,Options);
 end;
 end;
 
 
 {   DateTimeToString formats DateTime to the given format in FormatStr   }
 {   DateTimeToString formats DateTime to the given format in FormatStr   }
 
 
-procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
+procedure DateTimeToString(out Result: string; const FormatStr: string; 
+  const DateTime: TDateTime; Options : TFormatDateTimeOptions = []);
 begin
 begin
-  DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings);
+  DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings, Options);
 end;
 end;
 
 
-procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings);
+procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; 
+  const FormatSettings: TFormatSettings; Options : TFormatDateTimeOptions = []);
 var
 var
   ResultLen: integer;
   ResultLen: integer;
   ResultBuffer: array[0..255] of char;
   ResultBuffer: array[0..255] of char;
@@ -977,13 +979,14 @@ var
 
 
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
   var
   var
-    Token, lastformattoken: char;
+    Token, lastformattoken, prevlasttoken: char;
     FormatCurrent: pchar;
     FormatCurrent: pchar;
     FormatEnd: pchar;
     FormatEnd: pchar;
     Count: integer;
     Count: integer;
     Clock12: boolean;
     Clock12: boolean;
     P: pchar;
     P: pchar;
     tmp: integer;
     tmp: integer;
+    isInterval: Boolean;
 
 
   begin
   begin
     if Nesting > 1 then  // 0 is original string, 1 is included FormatString
     if Nesting > 1 then  // 0 is original string, 1 is included FormatString
@@ -992,6 +995,7 @@ var
     FormatCurrent := PChar(FormatStr);
     FormatCurrent := PChar(FormatStr);
     FormatEnd := FormatCurrent + Length(FormatStr);
     FormatEnd := FormatCurrent + Length(FormatStr);
     Clock12 := false;
     Clock12 := false;
+    isInterval := false;
     P := FormatCurrent;
     P := FormatCurrent;
     // look for unquoted 12-hour clock token
     // look for unquoted 12-hour clock token
     while P < FormatEnd do
     while P < FormatEnd do
@@ -1019,6 +1023,7 @@ var
     end ;
     end ;
     token := #255;
     token := #255;
     lastformattoken := ' ';
     lastformattoken := ' ';
+    prevlasttoken := 'H';
     while FormatCurrent < FormatEnd do
     while FormatCurrent < FormatEnd do
     begin
     begin
       Token := UpCase(FormatCurrent^);
       Token := UpCase(FormatCurrent^);
@@ -1060,6 +1065,8 @@ var
         end ;
         end ;
         '/': StoreStr(@FormatSettings.DateSeparator, 1);
         '/': StoreStr(@FormatSettings.DateSeparator, 1);
         ':': StoreStr(@FormatSettings.TimeSeparator, 1);
         ':': StoreStr(@FormatSettings.TimeSeparator, 1);
+	'[': if (fdoInterval in Options) then isInterval := true else StoreStr(FormatCurrent, 1);
+	']': if (fdoInterval in Options) then isInterval := false else StoreStr(FormatCurrent, 1);
         ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y', 'Z', 'F' :
         ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y', 'Z', 'F' :
         begin
         begin
           while (P < FormatEnd) and (UpCase(P^) = Token) do
           while (P < FormatEnd) and (UpCase(P^) = Token) do
@@ -1074,6 +1081,9 @@ var
                 StoreInt(Year mod 100, 2);
                 StoreInt(Year mod 100, 2);
             end;
             end;
             'M': begin
             'M': begin
+	      if isInterval and ((prevlasttoken = 'H') or TimeFlag) then
+	        StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, 0)
+	      else
               if (lastformattoken = 'H') or TimeFlag then
               if (lastformattoken = 'H') or TimeFlag then
               begin
               begin
                 if Count = 1 then
                 if Count = 1 then
@@ -1103,7 +1113,11 @@ var
                 StoreFormat(FormatSettings.LongDateFormat, Nesting+1, False);
                 StoreFormat(FormatSettings.LongDateFormat, Nesting+1, False);
               end ;
               end ;
             end ;
             end ;
-            'H': if Clock12 then
+            'H': 
+	      if isInterval then
+	        StoreInt(Hour + trunc(abs(DateTime))*24, 0)
+	      else
+	      if Clock12 then
               begin
               begin
                 tmp := hour mod 12;
                 tmp := hour mod 12;
                 if tmp=0 then tmp:=12;
                 if tmp=0 then tmp:=12;
@@ -1118,11 +1132,17 @@ var
                 else
                 else
                   StoreInt(Hour, 2);
                   StoreInt(Hour, 2);
               end;
               end;
-            'N': if Count = 1 then
+            'N': if isInterval then
+	           StoreInt(Minute + (Hour + trunc(abs(DateTime))*24)*60, 0)
+		 else
+		 if Count = 1 then
                    StoreInt(Minute, 0)
                    StoreInt(Minute, 0)
                  else
                  else
                    StoreInt(Minute, 2);
                    StoreInt(Minute, 2);
-            'S': if Count = 1 then
+            'S': if isInterval then
+	           StoreInt(Second + (Minute + (Hour + trunc(abs(DateTime))*24)*60)*60, 0)
+	         else 
+	         if Count = 1 then
                    StoreInt(Second, 0)
                    StoreInt(Second, 0)
                  else
                  else
                    StoreInt(Second, 2);
                    StoreInt(Second, 2);
@@ -1159,6 +1179,7 @@ var
                      Count := P - FormatCurrent;
                      Count := P - FormatCurrent;
                      StoreString(ConvertEraYearString(Count,Year,Month,Day));
                      StoreString(ConvertEraYearString(Count,Year,Month,Day));
                    end;
                    end;
+		 prevlasttoken := lastformattoken;  
                  lastformattoken:=token;
                  lastformattoken:=token;
                end;
                end;
              'G':
              'G':
@@ -1172,10 +1193,12 @@ var
                      Count := P - FormatCurrent;
                      Count := P - FormatCurrent;
                      StoreString(ConvertEraString(Count,Year,Month,Day));
                      StoreString(ConvertEraString(Count,Year,Month,Day));
                    end;
                    end;
+		 prevlasttoken := lastformattoken;
                  lastformattoken:=token;
                  lastformattoken:=token;
                end;
                end;
 {$ENDIF MSWindows}
 {$ENDIF MSWindows}
           end;
           end;
+	  prevlasttoken := lastformattoken;
           lastformattoken := token;
           lastformattoken := token;
         end;
         end;
         else
         else

+ 16 - 6
rtl/objpas/sysutils/datih.inc

@@ -18,7 +18,9 @@
 type
 type
   PDayTable = ^TDayTable;
   PDayTable = ^TDayTable;
   TDayTable = array[1..12] of Word;
   TDayTable = array[1..12] of Word;
-
+  TFormatDateTimeOption = (fdoInterval);
+  TFormatDateTimeOptions =  set of TFormatDateTimeOption;
+  
 const
 const
    HoursPerDay = 24;
    HoursPerDay = 24;
    MinsPerHour = 60;
    MinsPerHour = 60;
@@ -64,14 +66,22 @@ var
       yyyy   : year (four digits, with century)
       yyyy   : year (four digits, with century)
       h      : hour
       h      : hour
       hh     : hour (leading zero)
       hh     : hour (leading zero)
+      [h]    : hours including the hours of the full days, also: [hh]
+               Only available if fdoInterval is specified in options argument to FormatDateTime.
       n      : minute
       n      : minute
       nn     : minute (leading zero)
       nn     : minute (leading zero)
+      [n]    : minutes including the minutes of the full hours and days,
+               also: [nn] (or [m] or [mm] if in time part of the format string)
+               Only available if fdoInterval is specified in options argument to FormatDateTime.
       s      : second
       s      : second
       ss     : second (leading zero)
       ss     : second (leading zero)
+      [s]    : seconds (including the seconds of the full minutes, hours and days.
+               also: [ss]. 
+               Only available if fdoInterval is specified in options argument to FormatDateTime.
       t      : shorttimeformat
       t      : shorttimeformat
       tt     : longtimeformat
       tt     : longtimeformat
       am/pm  : use 12 hour clock and display am and pm accordingly
       am/pm  : use 12 hour clock and display am and pm accordingly
-                a/p    : use 12 hour clock and display a and p accordingly
+      a/p    : use 12 hour clock and display a and p accordingly
       /      : insert date seperator
       /      : insert date seperator
       :      : insert time seperator
       :      : insert time seperator
       "xx"   : literal text
       "xx"   : literal text
@@ -138,10 +148,10 @@ function StrToDate(const S: PChar; Len : integer; const useformat : string; sepa
 function StrToDateTime(const S: AnsiString): TDateTime;
 function StrToDateTime(const S: AnsiString): TDateTime;
 function StrToDateTime(const s: ShortString; const FormatSettings : TFormatSettings): TDateTime;
 function StrToDateTime(const s: ShortString; const FormatSettings : TFormatSettings): TDateTime;
 function StrToDateTime(const s: AnsiString; const FormatSettings : TFormatSettings): TDateTime;
 function StrToDateTime(const s: AnsiString; const FormatSettings : TFormatSettings): TDateTime;
-function FormatDateTime(const FormatStr: string; DateTime: TDateTime):string;
-function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
-procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
-procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings);
+function FormatDateTime(const FormatStr: string; DateTime: TDateTime; Options : TFormatDateTimeOptions = []): string;
+function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings; Options : TFormatDateTimeOptions = []): string;
+procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; Options : TFormatDateTimeOptions = []);
+procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings; Options : TFormatDateTimeOptions = []);
 Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
 Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
 Function FileDateToDateTime (Filedate : Longint) :TDateTime;
 Function FileDateToDateTime (Filedate : Longint) :TDateTime;
 function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;                         {$ifdef SYSUTILSINLINE}inline;{$endif}
 function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;                         {$ifdef SYSUTILSINLINE}inline;{$endif}