Qore Programming Language Reference Manual 0.8.8

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

All Classes Namespaces Functions Variables Groups Pages
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 | Qore::NULL | Qore::NULL
NOTHING | Represents the state of a variable having no value or function returning no value (not equivalent to NULL) | Qore::NOTHING | Qore::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 Qore::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:
  Qore::True

Pseudo Class for Type Boolean:
  <bool>

Type Code:
  Qore::NT_BOOLEAN

Type Name:
  `"bool"`

See Also
  bool, softbool

# 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 with the exception of the single quote character, which is the only character that may be escaped (ex: `'hello \'there\''`).
  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"`

See Also
  - string, softstring
  - String Functions

# 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
  - int, softint
  - Qore::MAXINT, Qore::MININT

# 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
  - float, softfloat
  - Math Functions

# Number

Description:
  Qore "number" values are arbitrary-precision numbers as provided by the GMP and MPFR libraries.
  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
  - number, softnumber
  - Math Functions

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
  - Date and Time Functions for a list of functions related to date/time processing
  - Date/Time Arithmetic
  - Time Zone Handling

## 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 (ex: `750ms`; Qore-specific)
- Short Relative Time Format (ex: `P3DT21H`; based on ISO-8601)
- Long Relative Time Format (ex: `P0001-03-00T00:00:04`; based on ISO-8601)

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
  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:
  my any $val = $q.pop(20s);
  is clearer than the alternative with an argument given as a value in implied milliseconds to the Queue::pop() method:
  my any $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 | `19s` | 19 seconds
  `ms` | Milliseconds | `250ms` | 250 milliseconds
  `us` | Microseconds | `21194us` | 21194 microseconds

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:
  - `PnYnMnDTnHnMnSnu`
  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, seconds, and, as an extension to ISO-8601, `"u"` for microseconds. Additionally, the values may be negative.
  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>

Note
  the `"u"` charater is a Qore-specific extension to ISO-8601

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

# 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 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"`

See Also
  binary

# NULL

Description:
  This data type represents an SQL `NULL` value and can only be accessed directly as an immediate value with the constant Qore::NULL. Note that Qore::NULL is not equivalent to NOTHING.

Immediate Value Example:
  Qore::NULL

Pseudo Class for Type Null:
  <value>

Type Code:
  Qore::NT_NULL

Type Name:
  `"NULL"`

See Also
  - Qore::SQL::Datasource
  - Qore::SQL::DatasourcePool
  - Qore::SQL::SQLStatement
  - DBI Functions

# NOTHING

Description:
  This special data type represents no value and can only be accessed directly as an immediate value with the constant Qore::NOTHING. Note that Qore::NOTHING is not equivalent to NULL.

Note
  The exists operator will return False when given Qore::NOTHING as an argument; for example:

      prompt% qore -X 'exists NOTHING'
      False

Immediate Value Example:
  Qore::NOTHING

Pseudo Class for Type Nothing:
  <value>

Type Code:
  Qore::NT_NOTHING

Type Name:
  `"nothing"`

# 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)

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.