DateTime
Synopsis
Date and time values.
Syntax
$Date$$Time$$DateTime$
Types
datetime
Description
Date, time, and datetime values are represented by the datetime type.
datetime literals start with a $ and are made up of either a date, given in year, month, day of month order;
a time, preceded by T and given in hour, minute, second, millisecond, (optional) timezone offset order;
or a datetime, which is a date and a time, in the orders given above, and separated by a T.
The following fields provide access to information about the value, but cannot be set:
isDate: returnstrueif the value is a date value,falseif the value is a datetime or time value.isTime: returnstrueif the value is a time value,falseif the value is a date or datetime value.isDateTime: returnstrueif the value is a datetime value,falseif the value is a date or time value.justTime: returns the date component of a date or datetime value.justDate: returns the time component of a time or datetime value.century: returns the century component of a year for date or datetime values.
The following fields provide access to the individual components of date, time and datetime values, and can be accessed using FieldSelection and be assigned using FieldSelection:
yearmonthdayhourminutesecondmillisecondtimezoneOffsetHourstimezoneOffsetMinutes
Not all fields are available on all values as indicated by the following table:
| Field | date | datetime | time |
|---|---|---|---|
year | x | x | |
month | x | x | |
day | x | x | |
hour | x | x | |
minute | x | x | |
second | x | x | |
millisecond | x | x | |
timezoneOffsetHours | x | x | |
timezoneOffsetMinutes | x | x |
The isDate, isTime, and isDateTime fields can be checked in advance to determine what
kind of value is stored in a variable of type datetime.
The following operators are defined for DateTime:
There are also library functions available for DateTime.
Examples
Examples of datetime values are:
rascal>$2010-07-15$
datetime: $2010-07-15$
rascal>$2010-07-15T07:15:23.123+0100$;
datetime: $2010-07-15T07:15:23.123+01:00$
Now introduce a datetime value and assign it to DT.
rascal>DT = $2010-07-15T09:15:23.123+03:00$;
datetime: $2010-07-15T09:15:23.123+03:00$
Here are examples of some datatime fields:
rascal>DT.isDateTime;
bool: true
rascal>DT.justDate;
datetime: $2010-07-15$
rascal>DT.justTime;
datetime: $T09:15:23.123+03:00$
rascal>DT.century;
int: 20
Pitfalls
- In normal parlance, the year 2010 is in the 21th century. The
centuryfield, however, just returns the century component of a given year, e.g., for 2010 this is 20. DT.justTimeprints a time literal that currently can not be parsed back into a value to due to issue #1443.