# Other Data Types

[Int64] [Cardinal] [SmallInt] [Byte] [TDateTime] [Measuring Elapsed Time]

This page (a late addition to the tutorial) covers some less common inbuilt data types that you may encounter or use yourself. You might skim this page at your first run through the tutorials then return to it when you have studied a few more topics.

This little program outputs the highest and lowest (most negative) numbers of various types as shown below the code.

```program NumberTypes;
begin
writeln('low(int64): ', low(int64));
writeln('high(int64): ', high(int64));
writeln('low(cardinal): ', low(cardinal));
writeln('high(cardinal): ', high(cardinal));
writeln('low(integer): ', low(integer));
writeln('high(integer): ', high(integer));
writeln('low(smallint): ', low(smallint));
writeln('high(smallint): ', high(smallint));
writeln('low(byte): ', low(byte));
writeln('high(byte): ', high(byte));
end.
```
```low(int64): -9223372036854775808
high(int64): 9223372036854775807
low(cardinal): 0
high(cardinal): 4294967295
low(integer): -2147483648
high(integer): 2147483647
low(smallint): -32768
high(smallint): 32767
low(byte): 0
high(byte): 255```

## Int64

You can use the int64 type for variables that might take values greater then 2147483647 and/or less than -2147483648. Factorials are frequently huge numbers so we use the int64 type in our example. Our elapsed time demonstration below requires the use of variables of type int64.

## Cardinal

You can use the 32-bit unsigned cardinal type for variables that might take values greater then 2147483647 but that will not exceed 4294967295. Fibonacci numbers may be large so we use the cardinal type in these examples. Jason McMillen uses the cardinal data type extensively in his Artillery tutorial.

## SmallInt

This 16-bit signed number type is little used nowadays, but is required for initiating a graph. See the student programs that use WinGraph.

## Byte

You are most likely to encounter this 8-bit unsigned number type when processing files or when writing in-line assembler code. See, for example, ChangeColour and Left and Right Shifts.

## TDateTime

TDateTime is a real variable, giving the number of days since 30/12/1899. The decimal part of the real number gives the fraction of a day, so .25 represents 6:00 am.

Many routines manipulate a TDateTime variable (and also real variables without the need to cast them to TDateTime). Add DateUtils to the uses section and see the Lazarus file dateutil.inc for DateOf (to remove the time of day), TimeOf (to remove the number of days), DayOf, MonthOf and YearOf. See also datih.inc for useful functions such as DateToStr, StrToDate, EncodeDate, EncodeTime, Now (for current date and time), Time (for current time without the number of days), Date (for number of days without the time of day) and TryStrToDate. There are also less obvious functions such as MilliSecondOfTheWeek for those that can find a use for them.

DelphiBasics gives DateTimeToString procedure for outputting dates and times in the format that you want.

This first example demonstrates
• formatting a date;
• formatting a time;
• the DayOf, MonthOf, YearOf, Now, EncodeDate, Date and TryStrToDate functions;
• subtraction of TDateTime values;
• date validation.
A copy of the output follows the code.
```program DateDemo;
uses
SysUtils, DateUtils;
var
strDateTime: string;
WholeDaysToChristmas: real;
Date1: string = '28/02/2015';
Date2: string = '29/02/2015';
dt1, dt2: TDateTime;

begin
DateTimeToString(strDateTime, 'dddd d mmmm, yyyy', Now);
writeln('Today''s date: ', strDateTime);
DateTimeToString(strDateTime, 'hh:mm:ss', Now);
writeln('Current time: ', strDateTime);
writeln('Start Date (dd/mm/yyyy): ', DayOf(0), '/', MonthOf(0), '/', YearOf(0));
WholeDaysToChristmas := EncodeDate(2015, 12, 25) - Date;
writeln('Whole days to Christmas: ', trunc(WholeDaysToChristmas));
if TryStrToDate(Date1, dt1) then
writeln(DateToStr(dt1), ' accepted')
else
writeln(Date1, ' invalid');
if TryStrToDate(Date2, dt2) then
writeln(DateToStr(dt2))
else
writeln(Date2, ' invalid');
end.

```
```Today's date: Friday 26 December, 2014
Current time: 16:34:33
Start Date (dd/mm/yyyy): 30/12/1899
Whole days to Christmas: 364
28/02/2015 accepted
29/02/2015 invalid```

## Measuring Elapsed Time

The next example shows how you can subtract TDateTime values to obtain a measure of elapsed time that is sufficiently accurate for most purposes. Here we compare it with the Windows recommended QueryPerformanceCounter, which records the count via a var parameter of type int64.

A copy of a typical output follows the code.

```program DurationDemo;
uses
SysUtils, Windows;
var
StartTime: TDateTime;
Duration, Period: real;
i: integer;
Frequency, StartTicks, EndTicks: int64;
begin
QueryPerformanceFrequency(Frequency);
for i := 1 to 5 do
begin
StartTime := Now;
QueryPerformanceCounter(StartTicks);
Sleep(1000);
Duration := Now - StartTime;
Duration := Duration * 24 * 60 * 60;
QueryPerformanceCounter(EndTicks);
Period := (EndTicks - StartTicks) / frequency;
writeln('Delay measured by subtracting Now values: ', Duration : 8 : 4, ' seconds');
writeln('Delay measured using QueryPerformanceCounter: ', Period : 8 : 4,  ' seconds');
writeln;
end;
end.

```
```Delay measured by subtracting Now values:   1.0150 seconds
Delay measured using QueryPerformanceCounter:   1.0149 seconds

Delay measured by subtracting Now values:   1.0160 seconds
Delay measured using QueryPerformanceCounter:   1.0150 seconds

Delay measured by subtracting Now values:   1.0160 seconds
Delay measured using QueryPerformanceCounter:   1.0151 seconds

Delay measured by subtracting Now values:   1.0150 seconds
Delay measured using QueryPerformanceCounter:   1.0151 seconds

Delay measured by subtracting Now values:   1.0160 seconds
Delay measured using QueryPerformanceCounter:   1.0150 seconds```
Programming - a skill for life!

The various types of constants and variables and how to use them