DateTime

Class containing a calendar date and time broken down into its components. For convenience, all attributes has aliases to match struct tm provided in libc.

All class attributes are settable. Out-of-range values roll over when the object is next read (for example, setting day to 32 in January gives February 1).

Instance attributes

second: int
sec: int
tm_sec: int

Seconds after the minute. Can be modified.

minute: int
min: int
tm_min: int

Minutes after the hour. Can be modified.

hour: integer
tm_hour: integer

Hours since midnight. Can be modified.

day: int
mday: int
tm_mday: int

Day of the month (1-31). Can be modified.

month: int
mon: int
tm_mon: int

Months since January (0-11). Can be modified.

year: int
tm_year: int

Year. Can be modified.

dayOfYear: int
yday: int
tm_yday: int

Days since January 1 (0-365). Recalculated from the date; setting it has no effect.

dayOfWeek: int
wday: int
tm_wday: int

Days since Sunday (0-6). Recalculated from the date; setting it has no effect.

isDST: int
isdst: int
tm_isdst: int

Daylight Saving Time flag: positive if DST is in effect, 0 if not, negative if unknown. Can be modified.

isUTC: bool

UTC flag: true if date and time components are expressed in UTC, false if in local time. Changing it keeps the timestamp and recalculates the components.

timestamp: int64

UNIX time (number of seconds since epoch). Can be modified.

Instance methods

format(format): String

Formats a timestamp, and returns a string according to given formatting rules.

Table 1. Parameters

format

String

Formatting string - see this for available options http://www.cplusplus.com/reference/ctime/strftime/. If null or empty, default format %a %b %d %Y %H:%M:%S %Z is used (UTC instead of %Z for UTC time).

Return

Formatted time as a string (empty string if the result exceeds 511 characters).

time = DateTime();
println(time.format("%Y-%m-%d %H:%M")); //Will print: "2016-01-19 12:14"
println(time.format("%Y-%m-%d %H:%M - timezone %Z - offset from UTC - %z")); //Will print: "2016-01-19 12:14 - timezone CET - offset from UTC - +0100"

Constructors

DateTime()

Creates new DateTime object with current local time.

DateTime(timestamp)

Creates new DateTime with provided local time in UNIX time format.

Table 2. Parameters

timestamp

Integer

UNIX time (number of seconds since epoch).

DateTime(timestamp, isUTC)

Creates new DateTime object with provided time. Date and time components will be expressed in UTC if second parameter is true and in local time if second parameter is false.

Table 3. Parameters

timestamp

Integer

UNIX time (number of seconds since epoch).

isUTC

Boolean

If date and time components should be expressed in UTC.