星期一, 5月 23, 2011

取得本地時間及utc時間

DECLARE @LocalDate DATETIME
SET @LocalDate = GETDATE()
-- convert local date to utc date
DECLARE @UTCDate DATETIME
SET @UTCDate = DATEADD(Hour, DATEDIFF(Hour, GETUTCDATE(), GETDATE()), @LocalDate)
-- convert utc date to local date
DECLARE @LocalDate2 DATETIME
SET @LocalDate2 = DATEADD(Hour, DATEDIFF(Hour, GETDATE(), GETUTCDATE()), @UTCDate)
SELECT @LocalDate, @UTCDate, @LocalDate2

星期一, 5月 09, 2011

TStringList儲存及載入Objects內容


unit Unit2;

interface

uses Windows, Classes, SysUtils;

type
SelectPositionRec = Record
io : string;
broker : string;
End;
PSelectPositionRec = ^SelectPositionRec;

TMyStringList = class(TStringList)
private
public
procedure LoadFromFile(const sFileName: string); override;
procedure SaveToFile(const sFileName: string); override;
procedure Delete(Index: Integer); override;
procedure Clear; override;
end;

implementation

{ TMyStringList }

procedure TMyStringList.Clear;
var
i : integer;
begin
for i := 0 to Count - 1 do
Dispose(PSelectPositionRec(Objects[i]));
inherited;
end;

procedure TMyStringList.Delete(Index: Integer);
begin
Dispose(PSelectPositionRec(Objects[Index]));
inherited;
end;

function ReadFSLn(FS: TFileStream): String;
var
Buffer : Char;
LastChr : Char;
begin
//Init!
Result := '';
Buffer := #0;
// LastChr := #0;
//Loop till you find the end of a line
while FS.Position < FS.Size do //Make sure never to pass the EOF
begin
FS.Read(Buffer,1);
if (Buffer=#0) then //(Buffer = #10) or ((LastChr = #13) and (Buffer=#10)) or
exit; //Line found exit!
Result := Result + Buffer;
// LastChr := Buffer;
end;
end;

procedure TMyStringList.LoadFromFile(const sFileName: string);
var
fs: TFileStream;
i : integer;
sStr : string;
ipSelectPositionRec : PSelectPositionRec;
begin
Clear();
if (not FileExists(sFileName)) then
Exit;
fs:=TFileStream.Create(sFileName, fmOpenRead);
try
i:=1;
while FS.Position < FS.Size do
begin
case i of
1: begin
sStr := ReadFSLn(FS);
Inc(i);
end;
2: begin
New(ipSelectPositionRec);
ipSelectPositionRec.io:=ReadFSLn(FS);
Inc(i);
end;
3: begin
ipSelectPositionRec.broker:=ReadFSLn(FS);
AddObject(sStr, TObject(ipSelectPositionRec));
i:=1;
end;
end;
end;
finally
fs.Free;
end;
end;

procedure TMyStringList.SaveToFile(const sFileName: string);
const
CRLF = #0;
var
fs: TFileStream;
i : integer;
sStr : AnsiString;
ipSelectPositionRec : PSelectPositionRec;
begin
fs:=TFileStream.Create(sFileName, fmCreate);
try
for i := 0 to Count - 1 do
begin
ipSelectPositionRec := PSelectPositionRec(Objects[i]);
sStr := strings[i]+CRLF+ipSelectPositionRec^.io+CRLF+ipSelectPositionRec^.broker+CRLF;
fs.WriteBuffer(sStr[1], Length(sStr));
end;
finally
fs.Free;
end;
end;

end.