57: How can I calculate
the difference between two points of time?
A: This is an unconfirmed answer so be a little careful
with it.
But at the very least it shows some interesting information about
Turbo Pascal date/time conventions and how to declare and
initialize
typed constants if they are records.
program TimDifTest;
uses Dos;
const a : DateTime
=
(year:1992; month:10;
day:24; hour:5; min:29; sec:38);
b : DateTime
=
(year:1993; month:11;
day:23; hour:6; min:30; sec:51);
var aLong, bLong, cLong : longint;
c : DateTime;
begin
PackTime (a, aLong);
PackTime (b, bLong);
cLong := bLong - aLong;
UnpackTime (cLong, c);
writeln (c.year-1980, ' ', c.month, ' ', c.day, ' ',
c.hour,
' ', c.min, ' ', c.sec);
end.
More generally than for dates between 1980 and 2079, or for more
accurate results, the difference between two date/times can be
calculated using Zeller's congruence (see the item "I want
code that
gives the weekday of the given date"). First calculate Zeller's
for
both the dates, convert them, and the hour, min, and sec into
seconds, subtract, and convert back.