Calendar date with time
does Dateish
For handling points in civil time, a DateTime
object stores year, month, day, hour, minute (all Int), second (potentially fractional) and a time zone.
It provides methods for calculating with date and time.
DateTime
methods are immutable; if you are tempted to modify one, create a modified copy instead.
Time zones are handled as Integers in seconds offset from UTC, not by time zone name.
my = DateTime.new(year => 2015,month => 11,day => 21,hour => 16,minute => 1,);say ; # OUTPUT: «2015-11-21T16:01:00Z»say .later(days => 20); # OUTPUT: «2015-12-11T16:01:00Z»say .truncated-to('hour'); # OUTPUT: «2015-11-21T16:00:00Z»say .in-timezone(-8 * 3600); # OUTPUT: «2015-11-21T08:01:00-0800»my = DateTime.now(formatter => );say ; # 12:45 (or something like that)
Since version 6.d, using synthetic codepoints such as 7̈ will result in an error.
Defined as:
multi method new(Int :!, Int : = 1, Int : = 1,Int : = 0, Int : = 0, : = 0,Int : = 0, :)multi method new(Date :!,Int : = 0, Int : = 0, : = 0,Int : = 0, :)multi method new(Int() , Int() , Int() ,Int() , Int , ,Int() : = 0, :)multi method new(Instant , :=0, :)multi method new(Numeric , :=0, :)multi method new(Str , :=0, :)
Creates a new DateTime
object. One option for creating a new DateTime object is from the components (year, month, day, hour, ...) separately. Another is to pass a Date object for the date component, and specify the time component-wise. Yet another is to obtain the time from an Instant, and only supply the time zone and formatter. Or instead of an Instant you can supply an Numeric as a UNIX timestamp.
You can also supply a Str formatted in ISO 8601 timestamp notation or as a full RFC 3339 date and time. Strings should be formatted as yyyy-mm-ddThh:mm:ssZ
or yyyy-mm-ddThh:mm:ss+0100
. We are somewhat less restrictive than the ISO 8601 standard, as we allow Unicode digits and mixing of condensed and extended time formats.
An invalid input string throws an exception of type X::Temporal::InvalidFormat. If you supply a string that includes a time zone and supply the timezone
named argument, an exception of type X::DateTime::TimezoneClash is thrown.
my = DateTime.new(year => 2015,month => 1,day => 1,hour => 1,minute => 1,second => 1,timezone => 1);= DateTime.new(date => Date.new('2015-12-24'),hour => 1,minute => 1,second => 1,timezone => 1);= DateTime.new(2015, 1, 1, # First January of 20151, 1, 1); # Hour, minute, second with default time zone= DateTime.new(now); # Instant.# from a Unix timestampsay = DateTime.new(1470853583.3); # OUTPUT: «2016-08-10T18:26:23.300000Z»= DateTime.new("2015-01-01T03:17:30+0500") # Formatted string
Defined as:
method now(: = , : --> DateTime)
Creates a new DateTime
object from the current system time. A custom formatter and timezone can be provided. The :$timezone
is the offset in seconds from GMT and defaults to the value of $*TZ
variable.
say DateTime.now; # OUTPUT: «2018-01-08T13:05:32.703292-06:00»
Note that one may use the methods shown below chained to the .now
to easily express current values, e.g.,
say DateTime.now.year; # OUTPUT: «2018»
Defined as:
method clone(DateTime: :, :, :, :, :, :, :, :)
Creates a new DateTime
object based on the invocant, but with the given arguments overriding the values from the invocant.
say DateTime.new('2015-12-24T12:23:00Z').clone(hour => 0);# OUTPUT: «2015-12-24T00:23:00Z»
Note that this can lead to invalid dates in some circumstances:
say DateTime.new("2012-02-29T12:34:56Z").clone(year => 2015);CATCH ;# OUTPUT: «X::OutOfRange: Day out of range. Is: 29, should be in 1..28»
Defined as:
method hh-mm-ss(DateTime: --> Str)
Returns the time represented by the object as a string in 24-hour HH:MM:SS format:
say DateTime.new("2052-02-29T22:34:56Z").hh-mm-ss;# OUTPUT: «22:34:56»
Defined as:
method hour(DateTime: --> Int)
Returns the hour component.
say DateTime.new('2012-02-29T12:34:56Z').hour; # OUTPUT: «12»
Defined as:
method minute(DateTime: --> Int)
Returns the minute component.
say DateTime.new('2012-02-29T12:34:56Z').minute; # OUTPUT: «34»
Defined as:
method second(DateTime:)
Returns the second component, including potentially fractional seconds.
say DateTime.new('2012-02-29T12:34:56Z').second; # OUTPUT: «56»say DateTime.new('2012-02-29T12:34:56.789Z').second; # OUTPUT: «56.789»say DateTime.new('2012-02-29T12:34:56,789Z').second; # comma also ok
Defined as:
method whole-second(DateTime:)
Returns the second component, rounded down to an Int.
say DateTime.new('2012-02-29T12:34:56.789Z').whole-second; # OUTPUT: «56»
Defined as:
method timezone(DateTime: --> Int)
Returns the time zone in seconds as an offset from UTC.
say DateTime.new('2015-12-24T12:23:00+0200').timezone; # OUTPUT: «7200»
Defined as:
method offset(DateTime: --> Int)
Returns the time zone in seconds as an offset from UTC. This is an alias for #method timezone.
say DateTime.new('2015-12-24T12:23:00+0200').offset; # OUTPUT: «7200»
Defined as:
method offset-in-minutes(DateTime: --> Real)
Returns the time zone in minutes as an offset from UTC.
say DateTime.new('2015-12-24T12:23:00+0200').offset-in-minutes; # OUTPUT: «120»
Defined as:
method offset-in-hours(DateTime: --> Real)
Returns the time zone in hours as an offset from UTC.
say DateTime.new('2015-12-24T12:23:00+0200').offset-in-hours; # OUTPUT: «2»
Defined as:
method Str(DateTime: --> Str)
Returns a string representation of the invocant, as done by the formatter. If no formatter was specified, an ISO 8601 timestamp will be returned.
say DateTime.new('2015-12-24T12:23:00+0200').Str;# OUTPUT: «2015-12-24T12:23:00+02:00»
Defined as:
method Instant(DateTime: --> Instant)
Returns an Instant object based on the invocant.
say DateTime.new('2015-12-24T12:23:00+0200').Instant; # OUTPUT: «Instant:1450952616»
Defined as:
method posix(Bool: = False --> Int)
Returns the date and time as a POSIX/UNIX timestamp (seconds since the Epoch, 1st January 1970 UTC).
If $ignore-timezone
is True
, the DateTime
object will be treated as if the time zone offset is zero.
say DateTime.new('2015-12-24T12:23:00Z').posix; # OUTPUT: «1450959780»
Defined as:
method truncated-to(DateTime: Cool )
Returns a copy of the invocant, with everything smaller than the specified unit truncated to the smallest possible value.
my = DateTime.new("2012-02-29T12:34:56.946314Z");say .truncated-to('second'); # OUTPUT: «2012-02-29T12:34:56Z»say .truncated-to('minute'); # OUTPUT: «2012-02-29T12:34:00Z»say .truncated-to('hour'); # OUTPUT: «2012-02-29T12:00:00Z»say .truncated-to('day'); # OUTPUT: «2012-02-29T00:00:00Z»say .truncated-to('month'); # OUTPUT: «2012-02-01T00:00:00Z»say .truncated-to('year'); # OUTPUT: «2012-01-01T00:00:00Z»
DateTimes with fractional seconds can be truncated to whole seconds with .truncated-to('second')
.
Defined as:
multi method Date(DateTime --> Date)multi method Date(DateTime --> Date)
Converts the invocant to Date
.
say DateTime.new("2012-02-29T12:34:56.946314Z").Date; # OUTPUT: «2012-02-29»say DateTime.Date; # OUTPUT: «(Date)»
Defined as:
method DateTime(--> DateTime)
Returns the invocant.
say DateTime.new("2012-02-29T12:34:56.946314Z").DateTime;# OUTPUT: «2012-02-29T12:34:56.946314Z»say DateTime.DateTime;# OUTPUT: «(DateTime)»
Defined as:
method utc(DateTime: --> DateTime)
Returns a DateTime object for the same time, but in time zone UTC.
say DateTime.new('2015-12-24T12:23:00+0200').utc;# OUTPUT: «2015-12-24T10:23:00Z»
Defined as:
method in-timezone(DateTime: Int(Cool) = 0 --> DateTime)
Returns a DateTime object for the same time, but in the specified $timezone
, which is the offset in seconds from GMT.
say DateTime.new('2015-12-24T12:23:00Z').in-timezone(3600 + 1800); # OUTPUT: «2015-12-24T13:53:00+0130»
Per RFC 7164, leap seconds do not respect local time and always occur at the end of the UTC day:
say DateTime.new: '2017-01-01T00:59:60+01:00'# OUTPUT: «2017-01-01T00:59:60+01:00»
Defined as:
method local(DateTime: --> DateTime)
Returns a DateTime object for the same time, but in the local time zone ($*TZ
).
my = -3600;say DateTime.new('2015-12-24T12:23:00+0200').local; # OUTPUT: «2015-12-24T09:23:00-0100»
multi sub infix:<-> (DateTime, Duration --> DateTime)multi sub infix:<-> (DateTime, DateTime --> Duration)
Takes a DateTime
to subtract from and either a Duration
or another DateTime
object. Returns a new DateTime
object or the Duration
between the two dates, respectively. When subtracting Duration
, time zone of the original DateTime
is preserved in the returned DateTime
object.
say raku DateTime.new(:2016year) - DateTime.new(:2015year):;# OUTPUT: «Duration.new(31536001.0)»say DateTime.new(:2016year, :3600timezone) - Duration.new(31536001.0);# OUTPUT: «2015-01-01T00:00:00+01:00»
multi sub infix:<+> (DateTime, Duration --> DateTime)multi sub infix:<+> (Duration, DateTime --> DateTime)
Takes a DateTime
and increases it by the given Duration
, preserving the time zone.
say DateTime.new(:2015year) + Duration.new(31536001.0);# OUTPUT: «2016-01-01T00:00:00Z»say Duration.new(42) + DateTime.new(:2015year, :3600timezone);# OUTPUT: «2015-01-01T00:00:42+01:00»
multi sub infix:«<=>»(DateTime \a, DateTime \b --> Order)
Compares the equivalent instant, returns the Order.
say DateTime.now <=> DateTime.now; # OUTPUT: «Less»
multi sub infix:<cmp>(DateTime \a, DateTime \b --> Order)
Compares the equivalent instant, returns the Order.
multi sub infix:«<»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool
multi sub infix:«>»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool
multi sub infix:«<=»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool
multi sub infix:«>=»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool
multi sub infix:«==»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool
multi sub infix:«!=»(DateTime \a, DateTime \b --> Bool)
Compares the equivalent instant, returns a Bool