Skip to main content

module DateTime

Usage

import DateTime;

Synopsis

Library functions for date and time.

Description

For operators on datetime see DateTime in the Rascal Language Reference.

The following functions are defined for datetime:

function now

datetime now()

Synopsis

Get the current datetime.

Examples

rascal>import DateTime;
ok
rascal>now();
datetime: $2022-11-22T15:18:25.632+00:00$

function createDate

datetime createDate(int year, int month, int day)

Synopsis

Create a new date.

Examples

rascal>import DateTime;
ok
rascal>createDate(2012,1,1);
datetime: $2012-01-01$

function createTime

datetime createTime(int hour, int minute, int second, int millisecond)

datetime createTime(int hour, int minute, int second, int millisecond,
int timezoneHourOffset, int timezoneMinuteOffset)

datetime createTime(int hour, int minute, int second, int millisecond,
int timezoneHourOffset, int timezoneMinuteOffset)

Synopsis

Create a new time (with optional timezone offset).

Examples

rascal>import DateTime;
ok
rascal>createTime(8,15,30,55);
datetime: $T08:15:30.055+01:00$
rascal>createTime(8,15,30,55,2,0);
datetime: $T08:15:30.055+02:00$

function createDateTime

datetime createDateTime(int year, int month, int day, int hour, int minute, 
int second, int millisecond)

datetime createDateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond)

datetime createDateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond, int timezoneHourOffset,
int timezoneMinuteOffset)

datetime createDateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond, int timezoneHourOffset,
int timezoneMinuteOffset)

datetime createDateTime(int year, int month, int day, int hour, int minute,
int second, int millisecond, int timezoneHourOffset,
int timezoneMinuteOffset)

Synopsis

Create a new datetime (with optional timezone offset).

Examples

rascal>import DateTime;
ok
rascal>createDateTime(2012,1,1,8,15,30,55);
datetime: $2012-01-01T08:15:30.055+01:00$
rascal>createDateTime(2012,1,1,8,15,30,55,2,0);
datetime: $2012-01-01T08:15:30.055+02:00$

function joinDateAndTime

datetime joinDateAndTime(datetime date, datetime time)

Synopsis

Create a new datetime by combining a date and a time.

Examples

rascal>import DateTime;
ok
rascal>D = createDate(2012, 1, 1);
datetime: $2012-01-01$
rascal>T = createTime(8, 15, 45, 30);
datetime: $T08:15:45.030+01:00$
rascal>joinDateAndTime(D, T);
datetime: $2012-01-01T08:15:45.030+01:00$

function splitDateTime

tuple[datetime date, datetime time] splitDateTime(datetime dt)

Synopsis

Split an existing datetime into a tuple with the date and the time.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:25.893+00:00$
rascal>splitDateTime(N);
tuple[datetime date,datetime time]: <$2022-11-22$,$T15:18:25.893+00:00$>

function incrementYears

datetime incrementYears(datetime dt, int n)

datetime incrementYears(datetime dt)

Synopsis

Increment the years by given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:25.940+00:00$
rascal>incrementYears(N);
datetime: $2023-11-22T15:18:25.940+00:00$
rascal>incrementYears(N, 5);
datetime: $2027-11-22T15:18:25.940+00:00$

function incrementMonths

datetime incrementMonths(datetime dt, int n)

datetime incrementMonths(datetime dt)

Synopsis

Increment the months by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.080+00:00$
rascal>incrementMonths(N);
datetime: $2022-12-22T15:18:26.080+00:00$
rascal>incrementMonths(N, 5);
datetime: $2023-04-22T15:18:26.080+00:00$

function incrementDays

datetime incrementDays(datetime dt, int n)

datetime incrementDays(datetime dt)

Synopsis

Increment the days by given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.133+00:00$
rascal>incrementDays(N);
datetime: $2022-11-23T15:18:26.133+00:00$
rascal>incrementDays(N, 5);
datetime: $2022-11-27T15:18:26.133+00:00$

function incrementHours

datetime incrementHours(datetime dt, int n)

datetime incrementHours(datetime dt)

Synopsis

Increment the hours by a given amount or by 1.`

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.182+00:00$
rascal>incrementHours(N);
datetime: $2022-11-22T16:18:26.182+00:00$
rascal>incrementHours(N, 5);
datetime: $2022-11-22T20:18:26.182+00:00$

function incrementMinutes

datetime incrementMinutes(datetime dt, int n)

datetime incrementMinutes(datetime dt)

Synopsis

Increment the minutes by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.228+00:00$
rascal>incrementMinutes(N);
datetime: $2022-11-22T15:19:26.228+00:00$
rascal>incrementMinutes(N, 5);
datetime: $2022-11-22T15:23:26.228+00:00$

function incrementSeconds

datetime incrementSeconds(datetime dt, int n)

datetime incrementSeconds(datetime dt)

Synopsis

Increment the seconds by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.285+00:00$
rascal>incrementSeconds(N);
datetime: $2022-11-22T15:18:27.285+00:00$
rascal>incrementSeconds(N, 5);
datetime: $2022-11-22T15:18:31.285+00:00$

function incrementMilliseconds

datetime incrementMilliseconds(datetime dt, int n)

datetime incrementMilliseconds(datetime dt)

Synopsis

Increment the milliseconds by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.343+00:00$
rascal>incrementMilliseconds(N);
datetime: $2022-11-22T15:18:26.344+00:00$
rascal>incrementMilliseconds(N, 5);
datetime: $2022-11-22T15:18:26.348+00:00$

function decrementYears

datetime decrementYears(datetime dt, int n)

datetime decrementYears(datetime dt)

Synopsis

Decrement the years by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.402+00:00$
rascal>decrementYears(N);
datetime: $2021-11-22T15:18:26.402+00:00$
rascal>decrementYears(N, 5);
datetime: $2017-11-22T15:18:26.402+00:00$

function decrementMonths

datetime decrementMonths(datetime dt, int n)

datetime decrementMonths(datetime dt)

Synopsis

Decrement the months by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.461+00:00$
rascal>decrementMonths(N);
datetime: $2022-10-22T15:18:26.461+00:00$
rascal>decrementMonths(N, 5);
datetime: $2022-06-22T15:18:26.461+00:00$

function decrementDays

datetime decrementDays(datetime dt, int n)

datetime decrementDays(datetime dt)

Synopsis

Decrement the days by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.519+00:00$
rascal>decrementDays(N);
datetime: $2022-11-21T15:18:26.519+00:00$
rascal>decrementDays(N, 3);
datetime: $2022-11-19T15:18:26.519+00:00$

function decrementHours

datetime decrementHours(datetime dt, int n)

datetime decrementHours(datetime dt)

Synopsis

Decrement the hours by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.574+00:00$
rascal>decrementHours(N);
datetime: $2022-11-22T14:18:26.574+00:00$
rascal>decrementHours(N, 5);
datetime: $2022-11-22T10:18:26.574+00:00$

function decrementMinutes

datetime decrementMinutes(datetime dt, int n)

datetime decrementMinutes(datetime dt)

Synopsis

Decrement the minutes by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.634+00:00$
rascal>decrementMinutes(N);
datetime: $2022-11-22T15:17:26.634+00:00$
rascal>decrementMinutes(N, 5);
datetime: $2022-11-22T15:13:26.634+00:00$

function decrementSeconds

datetime decrementSeconds(datetime dt, int n)

datetime decrementSeconds(datetime dt)

Synopsis

Decrement the seconds by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.697+00:00$
rascal>decrementSeconds(N);
datetime: $2022-11-22T15:18:25.697+00:00$
rascal>decrementSeconds(N, 5);
datetime: $2022-11-22T15:18:21.697+00:00$

function decrementMilliseconds

datetime decrementMilliseconds(datetime dt, int n)

datetime decrementMilliseconds(datetime dt)

Synopsis

Decrement the milliseconds by a given amount or by 1.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:26.757+00:00$
rascal>decrementMilliseconds(N);
datetime: $2022-11-22T15:18:26.756+00:00$
rascal>decrementMilliseconds(N, 5);
datetime: $2022-11-22T15:18:26.752+00:00$

data interval

data interval  
= Interval(datetime begin, datetime end)
;

Synopsis

A closed interval on the time axis.

function createInterval

interval createInterval(datetime begin, datetime end)

Synopsis

Given two datetime values, create an interval.

Examples

rascal>import DateTime;
ok
rascal>B = now();
datetime: $2022-11-22T15:18:26.818+00:00$
rascal>E = incrementDays(B, 2);
datetime: $2022-11-24T15:18:26.818+00:00$
rascal>createInterval(B, E);
interval: Interval($2022-11-22T15:18:26.818+00:00$,$2022-11-24T15:18:26.818+00:00$)

data Duration

data Duration  
= duration(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
;

Synopsis

A duration of time, measured in individual years, months, etc.

function createDurationInternal

tuple[int,int,int,int,int,int,int] createDurationInternal(datetime begin, datetime end)

function createDuration

Duration createDuration(datetime begin, datetime end)

Duration createDuration(interval i)

Synopsis

Create a new duration representing the duration between the begin and end dates.

Examples

rascal>import DateTime;
ok
rascal>B = now();
datetime: $2022-11-22T15:18:26.881+00:00$
rascal>E1 = incrementHours(B);
datetime: $2022-11-22T16:18:26.881+00:00$
rascal>createDuration(B, E1);
Duration: duration(0,0,0,1,0,0,0)
rascal>E2 = incrementMinutes(B);
datetime: $2022-11-22T15:19:26.881+00:00$
rascal>createDuration(B, E2);
Duration: duration(0,0,0,0,1,0,0)

function daysInInterval

int daysInInterval(interval i)

Synopsis

Return the number of days in an interval, including the begin and end days.

Examples

rascal>import DateTime;
ok
rascal>B = now();
datetime: $2022-11-22T15:18:26.946+00:00$
rascal>E = incrementDays(B, 2);
datetime: $2022-11-24T15:18:26.946+00:00$
rascal>I = createInterval(B, E);
interval: Interval($2022-11-22T15:18:26.946+00:00$,$2022-11-24T15:18:26.946+00:00$)
rascal>daysInInterval(I);
int: 2

function daysDiff

int daysDiff(datetime begin, datetime end)

Synopsis

Return the difference between two dates and/or datetimes in days.

Examples

rascal>import DateTime;
ok
rascal>B = now();
datetime: $2022-11-22T15:18:27.007+00:00$
rascal>E = incrementDays(B, 2);
datetime: $2022-11-24T15:18:27.007+00:00$
rascal>daysDiff(B, E);
int: 2

function dateRangeByDay

list[datetime] dateRangeByDay(interval i)

Synopsis

Given an interval, return a list of days.

Description

Given an interval i, return a list of days [i.begin, ..., i.end].

Examples

rascal>import DateTime;
ok
rascal>B = now();
datetime: $2022-11-22T15:18:27.071+00:00$
rascal>E = incrementDays(B, 2);
datetime: $2022-11-24T15:18:27.071+00:00$
rascal>I = createInterval(B, E);
interval: Interval($2022-11-22T15:18:27.071+00:00$,$2022-11-24T15:18:27.071+00:00$)
rascal>dateRangeByDay(I);
list[datetime]: [$2022-11-22$,$2022-11-23$,$2022-11-24$]

function parseDate

datetime parseDate(str inputDate, str formatString)

Synopsis

Parse an input date given as a string using the given format string.

Examples

rascal>import DateTime;
ok
rascal>parseDate("2011-12-23", "yyyy-MM-dd");
datetime: $2011-12-23$
rascal>parseDate("20111223", "yyyyMMdd");
datetime: $2011-12-23$

function parseDateInLocale

datetime parseDateInLocale(str inputDate, str formatString, str locale)

Synopsis

Parse an input date given as a string using a specific locale and format string.

function parseTime

datetime parseTime(str inputTime, str formatString)

Synopsis

Parse an input time given as a string using the given format string.

Examples

rascal>import DateTime;
ok
rascal>parseTime("11/21/19", "HH/mm/ss");
datetime: $T11:21:19.000+01:00$

function parseTimeInLocale

datetime parseTimeInLocale(str inputTime, str formatString, str locale)

Synopsis

Parse an input time given as a string using a specific locale and format string.

function parseDateTime

datetime parseDateTime(str inputDateTime, str formatString)

Synopsis

Parse an input datetime given as a string using the given format string.

Examples

rascal>import DateTime;
ok
rascal>parseDateTime("2011/12/23/11/19/54", "YYYY/MM/dd/HH/mm/ss");
datetime: $2010-12-26T11:19:54.000+01:00$

function parseDateTimeInLocale

datetime parseDateTimeInLocale(str inputDateTime, str formatString, str locale)

Synopsis

Parse an input datetime given as a string using a specific locale and format string.

function printDate

str printDate(datetime inputDate, str formatString)

str printDate(datetime inputDate)

Synopsis

Print an input date using the given format string.

Examples

rascal>import DateTime;
ok
rascal>printDate(now());
str: "2022-11-22"
rascal>printDate(now(), "YYYYMMdd");
str: "20221122"

function printDateInLocale

str printDateInLocale(datetime inputDate, str formatString, str locale)

str printDateInLocale(datetime inputDate, str locale)

Synopsis

Print an input date using a specific locale and format string.

Examples

rascal>import DateTime;
ok
rascal>printDateInLocale(now(), "Europe/Netherlands");
str: "2022-11-22"
rascal>printDateInLocale(now(), "French");
str: "2022-11-22"

function printTime

str printTime(datetime inputTime, str formatString)

str printTime(datetime inputTime)

Synopsis

Print an input time using the given format string.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:27.440+00:00$
rascal>printTime(N);
str: "15:18:27.440+0000"
rascal>printTime(N, "HH/mm/ss");
str: "15/18/27"

function printTimeInLocale

str printTimeInLocale(datetime inputTime, str formatString, str locale)

str printTimeInLocale(datetime inputTime, str locale)

Synopsis

Print an input time using a specific locale and format string.

function printDateTime

str printDateTime(datetime inputDateTime, str formatString)

str printDateTime(datetime inputDateTime)

Synopsis

Print an input datetime using the given format string.

Examples

rascal>import DateTime;
ok
rascal>N = now();
datetime: $2022-11-22T15:18:27.510+00:00$
rascal>printDateTime(N);
str: "2022-11-22 15:18:27.510+0000"
rascal>printDateTime(N, "yyyy-MM-dd\'T\'HH:mm:ss.SSSZZ");
str: "2022-11-22T15:18:27.510+0000"
rascal>printDateTime(N, "YYYY/MM/dd/HH/mm/ss");
str: "2022/11/22/15/18/27"

function printDateTimeInLocale

str printDateTimeInLocale(datetime inputDateTime, str formatString, str locale)

str printDateTimeInLocale(datetime inputDateTime, str locale)

Synopsis

Print an input datetime using a specific locale and format string.

function arbDateTime

datetime arbDateTime()

Synopsis

Create a new arbitrary datetime.

Examples

rascal>import DateTime;
ok
rascal>arbDateTime();
datetime: $2023-10-10T19:49:43.000+00:00$