Changeset 9 for trunk/SpotPrice.pas
- Timestamp:
- Apr 14, 2026, 12:57:08 PM (2 weeks ago)
- File:
-
- 1 edited
-
trunk/SpotPrice.pas (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/SpotPrice.pas
r7 r9 4 4 5 5 uses 6 Classes, SysUtils, Generics.Collections, XML, fphttpclient, opensslsockets,7 DOM;6 Classes, SysUtils, Generics.Collections, Generics.Defaults, XML, 7 fphttpclient, opensslsockets, DOM, Common, DateUtils, CsvDocument; 8 8 9 9 type … … 12 12 TSpotPrice = record 13 13 Time: TDateTime; 14 Value: Double;15 class function Create(Time: TDateTime; Value: Double): TSpotPrice; static;14 Value: Currency; 15 class function Create(Time: TDateTime; Value: Currency): TSpotPrice; static; 16 16 procedure LoadFromXmlNode(Node: TDOMNode); 17 17 procedure SaveToXmlNode(Node: TDOMNode); … … 21 21 22 22 TSpotPrices = class(TList<TSpotPrice>) 23 private 24 function FileNameFilter(FileName: string): Boolean; 25 function Comparer(constref Left, Right: TSpotPrice): Integer; 26 procedure LoadSpotReport(FileName: string); 23 27 public 24 28 function GetBlock(var Text: string; StartText, EndText: string): string; 25 procedure LoadSpotPrices(Date: TDate); 29 procedure LoadSpotPricesFromWeb(Date: TDate); 30 procedure Import(Directory: string); 26 31 procedure LoadFromXmlNode(Node: TDOMNode); 27 32 procedure SaveToXmlNode(Node: TDOMNode); 33 function SearchByTime(Time: TDateTime): TSpotPrice; 28 34 end; 29 35 … … 31 37 SpotPriceName = 'SpotPrice'; 32 38 SpotPricesName = 'SpotPrices'; 39 DPH = 1.21; 40 33 41 34 42 implementation … … 36 44 { TSpotPrice } 37 45 38 class function TSpotPrice.Create(Time: TDateTime; Value: Double): TSpotPrice;46 class function TSpotPrice.Create(Time: TDateTime; Value: Currency): TSpotPrice; 39 47 begin 40 48 Result.Time := Time; … … 54 62 end; 55 63 56 procedure TSpotPrices.LoadSpotPrices (Date: TDate);64 procedure TSpotPrices.LoadSpotPricesFromWeb(Date: TDate); 57 65 var 58 66 URL: string; … … 82 90 ValueText := StringReplace(ValueText, 'Kč', '', [rfReplaceAll]).Trim; 83 91 ValueText := StringReplace(ValueText, Chr($c2) + Chr($a0), '', [rfReplaceAll]); 84 Value := StrToInt(ValueText) / 1000 ;92 Value := StrToInt(ValueText) / 1000 * DPH; 85 93 end; 86 94 … … 94 102 end; 95 103 104 function TSpotPrices.FileNameFilter(FileName: string): Boolean; 105 begin 106 Result := ExtractFileExt(FileName) = '.csv'; 107 end; 108 109 function TSpotPrices.Comparer(constref Left, Right: TSpotPrice): Integer; 110 begin 111 Result := CompareDateTime(Left.Time, Right.Time); 112 end; 113 114 procedure TSpotPrices.LoadSpotReport(FileName: string); 115 var 116 CSVDoc: TCSVDocument; 117 R: Integer; 118 Date, Time: TDateTime; 119 Value: Currency; 120 begin 121 CSVDoc := TCSVDocument.Create; 122 try 123 CSVDoc.LoadFromFile(FileName); 124 125 for R := 1 to CSVDoc.RowCount - 1 do begin 126 Date := StrToDate(CSVDoc.Cells[0, R]); 127 Time := Date + StrToTime(Copy(CSVDoc.Cells[1, R], 1, Pos(' ', CSVDoc.Cells[1, R]) - 1)); 128 Value := StrToCurr(CSVDoc.Cells[3, R]) / 1000; 129 Add(TSpotPrice.Create(Time, Value)); 130 end; 131 finally 132 CSVDoc.Free; 133 end; 134 end; 135 136 procedure TSpotPrices.Import(Directory: string); 137 var 138 Reports: TStringList; 139 I: Integer; 140 begin 141 Clear; 142 143 Reports := TStringList.Create; 144 try 145 SearchFiles(Reports, Directory, FileNameFilter); 146 for I := 0 to Reports.Count - 1 do 147 LoadSpotReport(Reports[I]); 148 finally 149 Reports.Free; 150 end; 151 152 Sort(TComparer<TSpotPrice>.Construct(Comparer)); 153 end; 154 96 155 procedure TSpotPrices.LoadFromXmlNode(Node: TDOMNode); 97 156 var … … 117 176 Node.AppendChild(Node2); 118 177 end; 178 end; 179 180 function TSpotPrices.SearchByTime(Time: TDateTime): TSpotPrice; 181 var 182 I: Integer; 183 begin 184 I := 0; 185 while (I < Count) and (Items[I].Time <> Time) do Inc(I); 186 if I < Count then Result := Items[I] 187 else Result := TSpotPrice.Create(0, 0); 119 188 end; 120 189 … … 136 205 end; 137 206 138 139 207 end. 140 208
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/edc-stats/chrome/site/your_project_logo.png)