[comp.sys.ibm.pc] updating DOS date/time from AT clock

leonard@bucket.UUCP (Leonard Erickson) (12/02/87)

Below is the my source code for a utility to read the date and time
from the AT's onboard clock and set the DOS date and time accordingly.
I'm posting the sources here as they are short. I'll be trying to post
both source and executable to comp.binaries.ibm.pc (or whatever the
group is called.)

Note that the AT clock stores things in BCD format....<arrgh!>

Turbo Pascal 3.1a version
---------------------------------------------------------------------
program datefix;

type
  regpack = record
              case integer of
                1: (AX,BX,CX,DX,BP,SI,DI,DS,ES,Flags : integer);
                2: (AL,AH,BL,BH,CL,CH,DL,DH : byte);
            end;

var
  regs: regpack;
  month,day,hour,minute,second : byte;
  year :integer;

function dec(bcd:byte):byte;
var h,l : byte;
begin
  h   := trunc(bcd/16);
  l   := bcd-h*16;
  dec := 10*h+l;
end;

begin
  with regs do
  begin
    {read time from AT clock}
    AH := $02;
    intr($1A,regs);
    hour    := dec(CH);
    minute  := dec(CL);
    second  := dec(DH);

    {read date from AT clock}
    AH := $04;
    intr($1A,regs);
    day     := dec(DL);
    month   := dec(DH);
    year    := dec(CL)+100*dec(CH);

    {set DOS time}
    AH := $2D;
    CH := hour;
    CL := minute;
    DH := second;
    DL := $00;
    msdos(regs);

    {set DOS date}
    AH := $2B;
    CX := year;
    DH := month;
    DL := day;
    msdos(regs);

{    writeln (hour,':',minute,':',second);
    writeln(month,'/',day,'/',year);
}  end;
end.
---------------------------------------------------------------------

Turbo Pascal 4.0 version
---------------------------------------------------------------------
{$R-}    {Range checking off}
{$B+}    {Boolean complete evaluation on}
{$S+}    {Stack checking on}
{$I+}    {I/O checking on}
{$N-}    {No numeric coprocessor}
{$M 65500,16384,655360} {Turbo 3 default stack and heap}

program datefix;

Uses
  Dos;

var
  regs: registers;
  year,month,day,hour,minute,second : word;

function dec(bcd:byte):byte;
var h,l : byte;
begin
  h   := trunc(bcd/16);
  l   := bcd-h*16;
  dec := 10*h+l;
end;

begin
  with regs do
  begin
    {read time from AT clock}
    AH := $02;
    intr($1A,Dos.Registers(regs));
    hour    := dec(CH);
    minute  := dec(CL);
    second  := dec(DH);

    {read date from AT clock}
    AH := $04;
    intr($1A,Dos.Registers(regs));
    day     := dec(DL);
    month   := dec(DH);
    year    := dec(CL)+100*dec(CH);

    SetTime(hour,minute,second,$0000);{set DOS time}

    SetDate(year,month,day);{set DOS date}

{    writeln (hour,':',minute,':',second);
    writeln(month,'/',day,'/',year);
}  end;
end.
-- 
Leonard Erickson		...!tektronix!reed!percival!bucket!leonard
CIS: [70465,203]
"I used to be a hacker. Now I'm a 'microcomputer specialist'.
You know... I'd rather be a hacker."