# LibNoDave-Komponente DATE_AND_TIME lesen/schreiben



## marcengbarth (28 Oktober 2009)

Hallo,

ich hab die NoDaveComponent.pas mit 2 Funktionen erweitert um DATE_AND_TIME lesen und schreiben zu können.

GetDateAndTime:

```
//Return the DATE_AND_TIME-value read last from the PLC at the specified address.
//~param Address Address of the requested value
//~param Buffer Pointer to the buffer holding the PLC-data. The internal buffer is used, if Nil (default).
//~param BufOffs Offset-address of the buffer within the address-range of the PLC.
//~param BufLen Length of the buffer in bytes.
//~result The requested value or 0, if the requested address was not found within the buffer.
function TNoDave.GetDateAndTime(Address: Integer; Buffer: Pointer; BufOffs,
  BufLen: Integer): TDateTime;
var
  BufPtr: Pointer;
  Year, Month, Day, Hour, Minute, Second, Millisecond: WORD;
  MSEC1, MSEC2: Byte;
begin
  BufPtr:=BufferAt(Address, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then
  begin
    Year := daveGetU8From(BufPtr);
    if (Year >= 0) AND (Year < 89)  then
      Year := Year + 2000
    else
      Year := Year + 1900;
  end else Year:=0;
  BufPtr:=BufferAt(Address + 1, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then Month := daveGetU8From(BufPtr)
    else Month:=0;
  BufPtr:=BufferAt(Address + 2, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then Day := daveGetU8From(BufPtr)
    else Day:=0;
  BufPtr:=BufferAt(Address + 3, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then Hour := daveGetU8From(BufPtr)
    else Hour:=0;
  BufPtr:=BufferAt(Address + 4, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then Minute := daveGetU8From(BufPtr)
    else Minute:=0;
  BufPtr:=BufferAt(Address + 5, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then Second := daveGetU8From(BufPtr)
    else Second:=0;
  BufPtr:=BufferAt(Address + 6, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then MSEC1 := daveGetU8From(BufPtr)
    else MSEC1:=0;
  BufPtr:=BufferAt(Address + 6, 1, Buffer, BufOffs, BufLen);
  If Assigned(BufPtr) then MSEC2 := daveGetU8From(BufPtr)
    else MSEC2:=0;
  Millisecond := (MSEC1 * 10) + ((MSEC2 AND $F0) SHR 4);
  Result := EncodeDate(Year, Month, Day) + EncodeTime(Hour, Minute, Second, Millisecond);
end;
```
WriteDateAndTime:

```
//Write a DATE_AND_TIME-value into the PLC at the specified address without changing the properties of the TNoDave-instance.
//~param Address Byte-address of the value
//~param Value Value to write into the PLC.
procedure TNoDave.WriteDateAndTime(Address: Integer; Value: TDateTime);
var
  Dummy: Array[0..7] of Byte;
  Tmp: Word;
  Year, Month, Day, WeekDay, Hour, Minute, Second, Millisecond: WORD;
begin
  DecodeDate(Value, Year, Month, Day);
  DecodeTime(Value, Hour, Minute, Second, Millisecond);
  WeekDay := DayOfWeek(Value);
  if Year > 2000 then
  begin
    Tmp := Year - 2000;
      if Tmp > 89 then Tmp := 89;
  end;
  if Year < 2000 then
  begin
    Tmp := Year - 1900;
    if Tmp > 99 then Tmp := 99;
    if Tmp < 90 then Tmp := 90;
  end;
  Dummy[0] := Tmp;
  Dummy[1] := Month;
  Dummy[2] := Day;
  Dummy[3] := Hour;
  Dummy[4] := Minute;
  Dummy[5] := Second;
  Dummy[6] := Millisecond DIV 10;
  Dummy[7] := ((Millisecond - ((Millisecond DIV 10) * 10)) SHL 4) + (WeekDay AND $0F);
  DoWriteBytes(FArea, FDBNumber, Address, 8, @Dummy);
end;
```


----------

