Qore Programming Language Reference Manual  1.12.2
Basic Data Types

The following are the basic data types in Qore (see Container Data Types and Code Data Types):

Basic Data Types

Type Description Example Default Value
Boolean True or False value True False
String A sequence of characters with a character encoding "string" Empty string (i.e. "")
Integer A 64-bit signed integer 1 0
Float A double-precision floating-point number 1.00023 0.0
Number An arbitrary-precision number 5.23928173726123e50n 0.0n
Date absolute (with an associated time zone) or relative date/time values, both with resolution to the microsecond 2010-05-10T18:35:21.001456-07:00 1970-01-01Z
Binary An opaque binary object <23deadbeef> an empty object of size 0
NULL Corresponds to a NULL value in a database query (not equivalent to NOTHING NULL NULL
NOTHING Represents the state of a variable having no value or function returning no value (not equivalent to NULL) NOTHING NOTHING

Boolean

Description:
The Boolean type can have two values, True and False.

When converting other types to a Boolean, Qore uses Perl-style boolean conversion, based on an interpretation of the source value and taking into account its type (for example, when this option is set, an empty string, hash, or list is always False, whereas if these are not empty they are interpreted as True); this way is considered to be more intuitive by most prorammers, so much so that the original strict mathematical interpretation of boolean values is considered to be a design bug in qore; see %perl-bool-eval for more information.
Immediate Value Example:
True
Pseudo Class for Type Boolean:
<bool>
Type Code:
Qore::NT_BOOLEAN
Type Name:
"bool"
See also
bool, softbool

True

The keyword True stands for the boolean value true.

False

The keyword False stands for the boolean value false.


String

Description:
String values are specified with text between double or single quotes. Text between double quotes is subject to interpretation of escape characters; text between single quotes is not.

Strings are assumed by default to have the encoding given by the QORE_CHARSET or the LANG environment variable (see Environment Variables). If neither of these variables is set, then all strings will be assumed to have UTF-8 encoding.

For detailed information on Qore character encoding handling, please see Strings and Character Encoding .

It is legal to specify a string literal with newline characters like the following:
str = "this string is
on more than 1 line";

See string escape characters for a description of escape characters in double-quoted strings.

Internally, strings are stored as a pointer to a sequence of bytes terminated by a null (or zero byte), an unsigned integer giving the length of the string, and a pointer to an object giving the string's character encoding.

<string>::strlen() is a constant-time operation (ie O(1)), however if the string has a multi-byte encoding, then <string>::length() (returning the length of the string in characters, not bytes) computational complexity is O(n) (however if the character encoding is a single-byte encoding <string>::length() is also O(1)).

Immediate Value Example:
"this is a string"
Pseudo Class for Type String:
<string>
Type Code:
Qore::NT_STRING
Type Name:
"string"

String slicing:
As an alternative way of acquiring substrings, strings can be "sliced" by dereferencing them using the [] operator with a range or a list, as in the following examples:
string str = "hello";
# Creates a substring with the first two characters of the original string
string str1a = str[0..1];
string str1b = str[0,1];
string str1c = str[..1];
printf("string 1a: %y\n", str1a);
printf("string 1b: %y\n", str1b);
printf("string 1c: %y\n", str1c);
# Creates a substring with the last two characters of the original string in reverse order
string str2a = str[4..3];
string str2b = str[4,3];
printf("string 2a: %y\n", str2a);
printf("string 2b: %y\n", str2b);
# Creates a substring from different characters in the string
string str3 = str[4,3..1,0];
printf("string 3: %y\n", str3);
# -------------------------
# the output is as follows:
string 1a: "he"
string 1b: "he"
string 1c: "he"
string 2a: "ol"
string 2b: "ol"
string 3: "olleh"
string printf(string fmt,...)
Outputs the string passed to standard output, using the first argument as a format string; does not e...

When making a substring with a list of character offsets and referring to a character offset that does not exist, no characters are added to the output string; these references are ignored.
See also

Integer

Description:
Qore integers are 64-bit signed integers.
Immediate Value Example:
100
Pseudo Class for Type Integer:
<int>
Type Code:
Qore::NT_INT
Type Name:
"integer"
See also

Float

Description:
Qore floats are double precision floating-point numbers (C/C++ type double), normally a 64-bit value.
Immediate Value Examples:
  • -500.494
  • 2.35e10
Pseudo Class for Type Float:
<float>
Type Code:
Qore::NT_FLOAT
Type Name:
"float"
See also

Number

Description:
Qore "number" values are arbitrary-precision numbers as provided by the GMP and MPFR libraries. Inside, they are implemented as floating-point values and therefore comparing them entails the same problems as comparing floats. You should not compare two number values for equality directly.

Operations with number values are generally slower than those with floats but support far greater accuracy. To give an immediate number value; write an integer or floating-point value and append an "n" to it, designating a "number" value.

In numeric operations where at least one argument is a number type, the other operands will generally be automatically converted to a number type and the result of the operation will also be a number type. When an operator acts on two values of type number, the result of the operation has the precision of the operand with the greatest precision.
Immediate Value Example:
  • -500.494n
  • 2.35e10n
Pseudo Class for Type Number:
<number>
Type Code:
Qore::NT_NUMBER
Type Name:
"number"
See also
Since
Qore 0.8.6 introduced the number type and integration with the GMP and MPFR libraries

Date

Description:
Qore date/time values have date and time components supporting a resolution to the microsecond and can be either absolute or relative.
Immediate Value Examples:
  • 2012-02-17T19:05:54+01:00
Pseudo Class for Type Date:
<date>
Type Code:
Qore::NT_DATE
Type Name:
"date"
See also

Absolute Date/Time Values

Absolute date/time values specify a specific point in time in a certain time zone, such as January 1, 2005 10:35:00 +01:00. They are stored interally as a 64-bit signed offset from the Qore epoch (1970-01-01Z), a non-negative 4-byte integer for microseconds, and a pointer to a time zone description object that provides the UTC offset and daylight savings time information (see Time Zone Handling for more information). Note that all absolute date/time values in Qore are stored internally in UTC and are converted for display purposes to the representation of wall time in their tagged time zone.

Absolute date/time values can be specified with a syntax based on ISO-8601 date formats as follows:

YYYY-MM-DD[THH:mm:SS[.n*]][Z|[+-]HH[:mm[:SS]]]

Note that if no time zone information is given, the local time zone will be assumed. If a time zone UTC offset is given, it is given in units of time east of UTC (i.e. +05:00 means five hours east of UTC).

Or an alternative format (with a '-' instead of a 'T' to separate the time component):

YYYY-MM-DD[-HH:mm:SS[.n*]][Z|[+-]HH[:mm[:SS]]]

for example, for just the date without a time component (assumed to be midnight on the given date in the local time zone):

  • 2010-05-26

for just the date in UTC, without a time component:

  • 2010-05-26Z

or, for just the time, without a date component (note that in this case the date component will be set to Jan 1, 1970, in order for time arithmetic to function properly and will also be tagged with the local time zone):

  • 20:05:10.458342

Some further examples (note that the date/time values without a time zone specification here are tagged with the local time zone):

prompt% qore -X '2005-03-29-18:12:25'
2005-03-29 18:12:25 Tue +02:00 (CEST)
prompt% qore -X '0512-01-01T01:49:59.002213Z'
0512-01-01 01:49:59.002213 Fri Z (UTC)
prompt% qore -X '2005-03-29'
2005-03-29 00:00:00 Tue +02:00 (CEST)
prompt% qore -X '18:35:26+08:00'
1970-01-01 18:35:26 Thu +08:00 (+08)

The year must be a four-digit number, and all other values except microseconds must be two-digit numbers. If microseconds are present, at least one and up to 6 digits may be given after the decimal point. Pad the numbers with leading zeros if the numbers are smaller than the required number of digits. The hour component must be in 24-hour time format. Except for the month and day values, all other values start with 0 (hour = 00 - 23, minute and second: 00 - 59). Any deviation from this format will cause a parse exception.

When a date/time value is converted to an integer or vice-versa, a 64-bit offset in seconds from the start of the "epoch" is used for the conversion. Qore's "zero date" (the start of Qore's "epoch") is January 1, 1970 UTC. When calculating second offsets from this date, a 64-bit integer is used.

Note
The default local time zone for qore is set when the qore library is initialized; see Time Zone Handling for more information.

Relative Date/Time Values (Durations)

Relative dates (durations) are normally used for date addition and subtraction. See Date/Time Arithmetic for more information.

Internally, durations are stored as a set of seven discrete signed integer values, one each for years, months, days, hours, minutes, seconds, and microseconds.

There are 3 different formats understood by the Qore parser for describing literal durations in Qore as follows:

Single Relative Time Format

A single relative date/time value (or a duration) may be specified as follows (note that this format is specific to Qore and not based on ISO-8601):

  • <integer><date component specifier>

Examples:

  • 250ms: 250 milliseconds
  • 30s: 30 seconds
  • 2m: 2 minutes
  • 19.1s: 19 seconds and 100 milliseconds

Such values are recommended to give to functions and methods taking a timeout value as the units are then clear in the source code (whereas if an integer is given, it may not be clear that the function or method expects a value in milliseconds, for example); for example:

auto val = q.pop(20s);

is clearer than the alternative with an argument given as a value in implied milliseconds to the Queue::pop() method:

auto val = q.pop(20000);

Date Specifiers For Single Values For Relative Dates (non-ISO-8601 syntax)

Component Meaning Example Description
Y Years 2Y 2 Years
M Months 3M 3 Months
D Days 10D 10 Days
h Hours 15h 15 hours
m Minutes 25m 25 minutes
s Seconds 19.1s 19 seconds and 100 milliseconds
ms Milliseconds 250ms 250 milliseconds
us Microseconds 21194us 21194 microseconds
Note
In this relative date/time format, only seconds accept a fractional component

Short Relative Time Format

This and the next duration format for composite relative date/time values are both based on ISO-8601.

This first format has the following syntax:

  • PnYnMnDTnHnMnS

Each element above is optional, but at least one element must be present. Note that "M" means months when before the "T" and minutes when found after the "T". The other elements are years, days, hours, and seconds. Fractional values are accepted as well, but these values are always converted to an integer value in the time component with higher precision (i.e. fractional seconds are converted to microseconds, fractional microseconds are ignored), and additionally the values may be negative.

Note
  • Standard ISO-8601 relative date/time formats only allows for the last component to have a fractional value.
  • Fractional years are converted to days assuming 365 days in a year
  • Fractional months are converted to days assuming an average 30 days in a month

Here are some examples (using qore's -X command-line option to evaluate and expression and print out the result):

prompt% qore -X 'P1Y3MT4S'
<time: 1 year 3 months 4 seconds>
prompt% qore -X 'PT4M551u'
<time: 4 minutes 551 microseconds>
prompt% qore -X 'P3DT21H'
<time: 3 days 21 hours>
prompt% qore -X 'PT19.1S'
<time: 19 seconds 100 milliseconds>
Note
the "u" charater indicating microseconds is a deprecated Qore-specific extension to ISO-8601; use fractional seconds instead

Long Relative Time Format

The second ISO-8601-based format for specifing complex durations with multiple time units has the following syntax:

  • PYYYY-MM-DDTHH:mm:SS

This format is more limited than the first format, in that all values must be non-negative, and furthermore, all values must be present (although they may be zero).

Here are some examples of the second format (equivalent to the first examples):

prompt% qore -X 'P0001-03-00T00:00:04'
<time: 1 year 3 months 4 seconds>
prompt% qore -X 'P0000-00-00T00:04:00.000551'
<time: 4 minutes 551 microseconds>
prompt% qore -X 'P0000-00-03T21:00:00'
<time: 3 days 21 hours>
See also
date, softdate

Relative Date/Time Comparisons

Comparisons with two relative date/time values are made with normalized representations of each component of the date/time value. Because the actual value of a relative date/time value only makes sense when used with an absolute date value (ex: adding 1M to an absolute date value will result in a different number of days being added depending on the month and day of the absolute date value operand), comparisons of relative date/time values with mixed day, month, and year components must be based on approximations. When mixing day, month, and year components, the following approximations are used:

  • one day is treated as equivalent to 24 hours (1D == 24h)
  • one month is treated as equivalent to 31 days (1M == 31D)
  • 365 days are treated as equivalent to one year (365D == 1Y)

These assumptions try to make sense of expressions like:

45D > 1M 

for example. Month lengths can range from 28 to 31 days, therefore while the expression:

30D < 1M 

will be evaluated as True, when adding 30D to an absolute date value, the result could be less than adding 1M (for example, when the operand is an absolute date in February).


Binary

Description:
The binary data type is used to hold binary arbitrary binary data. Internally it is represented by a pointer to a memory location for the data and a size indicator.

Binary literal values are given in between angle brackets as an even series of hexadecimal digits:
binary b = <651144fe3310b5cc84>;
b += <abde77>;
# b now contains <651144fe3310b5cc84abde77>

Binary data can be concatenated with the + and += operators and manipulated with the splice and extract operators.

This data can be manipulated by being written and read from Qore::File, Qore::Socket, Qore::SQL::Datasource, Qore::SQL::DatasourcePool, or Qore::SQL::SQLStatement objects, or converted and parsed to/from base64 encoded strings using the makeBase64String() and parseBase64String() functions, or compressed and decompressed using the compress(), gzip(), bzip2(), etc. functions, and processed by most cryptographic funtions, among others.

Binary objects can be read from a Qore::File object using the Qore::File::readBinary() method and can be written using the Qore::File::write() method. Please see the Qore::File class for more information.

Binary objects can be read from a Qore::Socket object using the Qore::Socket::recvBinary() method and can be written using the Qore::Socket::send() method. Please see the Qore::Socket class for more information.

The Qore::SQL::Datasource, Qore::SQL::DatasourcePool, and Qore::SQL::SQLStatement classes can also be used to read and write binary objects as BLOBs.

Note that this is not an exhaustive list; see the function and class library documentation for more examples.
Immediate Value Example:
<0feba023ffdca6291>
Pseudo Class for Type Binary:
<binary>
Type Code:
Qore::NT_BINARY
Type Name:
"binary"

Binary slicing:
Binary objects can be "sliced" by dereferencing them using the [] operator with a range or a list, as in the following examples:
binary bin = <beadface>;
# Creates a binary slice with the first two bytes of the original binary object
binary bin1a = bin[0..1];
binary bin1b = bin[0,1];
binary bin1c = bin[..1];
printf("binary 1a: %s\n", bin1a.toHex());
printf("binary 1b: %s\n", bin1b.toHex());
printf("binary 1c: %s\n", bin1c.toHex());
# Creates a binary slice with the last two bytes of the original binary object in reverse order
binary bin2a = bin[3..2];
binary bin2b = bin[3,2];
printf("binary 2a: %s\n", bin2a.toHex());
printf("binary 2b: %s\n", bin2b.toHex());
# Creates a binary slice with different bytes of the original binary object
binary bin3 = bin[3,2..1,0];
printf("binary 3: %s\n", bin3.toHex());
# -------------------------
# the output is as follows:
binary 1a: bead
binary 1b: bead
binary 1c: bead
binary 2a: cefa
binary 2b: cefa
binary 3: cefaadbe

When making a binary slice with a list of byte offsets and referring to a byte offset that does not exist, no bytes are added to the output binary slice; these references are ignored.
See also

NULL

Description:
This data type represents an SQL NULL value and can only be accessed directly as an immediate value with the keyword NULL. Note that NULL is not equivalent to NOTHING.
Immediate Value Example:
NULL
Pseudo Class for Type Null:
<value>
Type Code:
Qore::NT_NULL
Type Name:
"NULL"
See also

NULL

The keyword NULL stands for the SQL NULL value.


NOTHING

Description:
This special data type represents no value and can only be accessed directly as an immediate value with the keyword NOTHING. Note that NOTHING is not equivalent to NULL .
Note
The exists operator will return False when given NOTHING as an argument; for example:
prompt% qore -X 'exists NOTHING'
False
Immediate Value Example:
NOTHING
Pseudo Class for Type Nothing:
<value>
Type Code:
Qore::NT_NOTHING
Type Name:
"nothing"

NOTHING

The keyword NOTHING represents no value.

Data Conversions

Boolean, string, integer, date, floating point, and arbitrary-percision numeric data types can be freely converted from one type to the other, although data loss is possible depending on the conversion (particularly when converting to the boolan type as only two possible values are supported).

The special types NULL and NOTHING are not equivalent and are generally not automatically converted to or from any other type.

When date types are converted from strings, any of the following formats can be used: "YYYYMMDDHHmmSS[.us][Z|+-HH[:MM[:SS]]]", "YYYY-MM-DD HH:mm:SS.us", "YYYY-MM-DDTHH:mm:SS", "YYYY-MM-DDTHH:mm:SS[.us][Z|+-HH[:MM[:SS]]]", and most reasonable combinations thereof. If the time zone component is missing, then the local time zone will be assumed (see Time Zone Handling).

When dates are converted to and from integer, floating-point, and arbitrary-percision numeric values, the a 64-bit second offset from January 1, 1970 in the local time zone is used for the conversion. For example

int(2006-01-01)
int int(string str, int base=10)
Converts the string to an integer value with respect to the base.

gives 1136073600 (regardless of the local time zone the date is in). This is for backwards-compatibility with Qore before time zone support was available; to get the second offset of a date from 1970-01-01Z (i.e. the true epoch offset), call get_epoch_seconds() instead.

When an expression requires a certain data type and the source data type cannot be converted to the desired data type, the default value for the desired data type will be used. The default values are given in Basic Data Types.