Time Calculator for windows

Today I got an idea on a proram I could make that would be useful and unqic, so here it is a calculator that lets you add, subtract and multiply dates and times together. It uses Delphi’s TDateTime variable type that the integer part is the days and fraction part is time.

program timecalc;

{$APPTYPE CONSOLE}
{$RESOURCE TIMECALC_RES.RES}
uses
  SysUtils,
  Windows,
  Classes;
type
TDateTimeOp=function(S1//First parameter for the op
,S2//second parameter for the op
:string):TDatetime;//Answer for the op
var systime:systemtime;//used for UTC Time
exename:string;
ops,//stringlist with op functions in TDateTimeOp type
timeconsts//string list for the constants As PDateTime type
:tstringlist;
pdt:pdatetime;
timefunc:TDateTimeOp;
yMinutes:DWord;
noresult:boolean;

FUNCTION GetDateTime(s:string):tdatetime;
begin
if timeconsts.IndexOf(s)>-1then result:=PDateTime(timeconsts.Objects[
timeconsts.IndexOf(s)])^else
if((strscan(pchar(s),DateSeparator)<>nil)and(strscan(pchar(s),TimeSeparator)<>nil))then
result:=strtodatetime(s)else if strscan(pchar(s),timeseparator)<>nil then result:=strtotime(
s) else if(strscan(pchar(s),DateSeparator)<>nil)then result:=strtodate(s)else if
strtointdef(s,-1)<>-1 then result:=strtoint(s)*encodetime(0,1,0,0)else
NoResult:=true;
end;

function addtime(S1,S2:string):TDateTime;
begin
result:=Getdatetime(s1)+GetDateTime(S2);
end;

function SubTime(S1,S2:string):tdatetime;
begin
result:=GetDateTime(s1)-GetDateTime(s2);
end;
function Multiply(s1,s2:String):TDatetime;
begin
result:=getdatetime(s1)*strtofloat(s2);
end;

begin
exename:=extractfilename(paramstr(0));
if paramcount=0then begin
writeln('Usage:',exename,' [datetimeX] [op] [datetimeY]');
writeln('Ops:');
writeln('+            Addition.');
writeln('-            Subtraction.');
writeln('*            Multiply([datetimeY] is a number not a date or time when this op is used)');
writeln;
writeln('[datetimeX] and [datetimeY] can be date, a time or both');
writeln('If [datetimeX] or [datetimeY] have both date and time they must be surrounded with quotes "hh:mm:ss mm/dd/yyyy"');
writeln;
writeln('[datetimeX] or [datetimeY] can also be one of these operators:');
writeln('NOW                Current local datetime.');
writeln('UTC                UTC Time.');
writeln('WEEK               Length of one week');
writeln('DAY                Length of one day');
writeln('MONTH              Length of one month');
writeln('YEAR               Length of one year');
writeln('DATE               Current local date');
writeln('TIME               Current local time');
writeln('%ERRORLEVEL%       Use the time and date from a previous answer. Use %ERRORLEVEL% variable from the NT Command prompt');
writeln;
writeln('Examples:');
writeln('Add one week from now: ',exename,' NOW + WEEK');
writeln('How much longer until 1:00pm: ',exename,' TIME - 1:00PM');
write('Press enter to quit...');
readln;
exitprocess(0);
end;
ops:=tstringlist.Create;
ops.AddObject('+',tobject(@addtime));
ops.Addobject('-',tobject(@subtime));
ops.AddObject('*',tobject(@multiply));
timeconsts:=tstringlist.Create;
new(pdt);
pdt^:=now;
timeconsts.AddObject('NOW',tobject(pdt));
new(pdt);
getsystemtime(systime);
pdt^:=systemtimetodatetime(systime);
timeconsts.AddObject('UTC',tobject(pdt));
new(pdt);
pdt^:=7;
timeconsts.AddObject('WEEK',tobject(pdt));
new(pdt);
pdt^:=1;
timeconsts.AddObject('DAY',tobject(pdt));
new(pdt);
pdt^:=30;
timeconsts.AddObject('MONTH',tobject(pdt));
new(pdt);
pdt^:=365;
timeconsts.AddObject('YEAR',tobject(pdt));
new(pdt);
pdt^:=DATE;
timeconsts.AddObject('DATE',tobject(pdt));
new(pdt);
pdt^:=TIME;
timeconsts.AddObject('TIME',tobject(pdt));
yminutes:=0;
case ParamCount of
1:begin writeln('Need more parameters');exitprocess(0);end;
2:begin
if ops.IndexOf(paramstr(2))=-1then begin
 writeln(paramstr(2),' is not a valid op');exitprocess(0);
 end else begin
 @timefunc:=Pointer(ops.Objects[ops.indexof(paramstr(2))]);
 yminutes:=trunc(timefunc(paramstr(1),'0')/encodetime(0,1,0,0));
 end;
 end;
3:begin if ops.IndexOf(paramstr(2))=-1then begin
 writeln(paramstr(2),' is not a valid op');exitprocess(0);
 end else begin
 @timefunc:=Pointer(ops.Objects[ops.indexof(paramstr(2))]);
 yminutes:=trunc(timefunc(paramstr(1),paramstr(3))/encodetime(0,1,0,0));
 end;
 end;
else begin writeln('Too Many Parameters');exitprocess(0);end;
end;
if noresult then begin writeln('Bad [datetimeX] or [datetimeY] parameters');
exitprocess(0);end;
writeln(datetimetostr(yminutes*encodetime(0,1,0,0)));
exitprocess(yminutes);

end.

Published by Justin Roeder

I am an electronics engineer and computer programmer that has autism. I learned by myself

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *

4 + 11 =

All in one
Start
Amazon.com, Inc. VA Ashburn
Your cart is empty.
Loading...