<commit_before>=begin pod

=TITLE class DateTime

=SUBTITLE Calendar date with time

    class DateTime does Dateish { ... }

For handling points in civil time, a C<DateTime> object stores year, month,
day, hour, minute (all L<Int|/type/Int>), second (potentially fractional) and
a time zone.

It provides methods for calculating with date and time.

C<DateTime> methods are immutable; if you are tempted to modify one, create a
modified copy instead.

Time zones are handled as L<Integers|/type/Int> in B<seconds> offset from UTC,
not by time zone name.

=begin code
use v6;
my $dt = DateTime.new(
    year    => 2015,
    month   => 11,
    day     => 21,
    hour    => 16,
    minute  => 1,
);

say $dt;                            # 2015-11-21T16:01:00Z
say $dt.later(days => 20);          # 2015-12-11T16:01:00Z
say $dt.truncated-to('hour');       # 2015-11-21T16:00:00Z
say $dt.in-timezone(-8 * 3600);     # 2015-11-21T08:01:00-0800
=end code

=head1 Methods

=head2 method new

Defined as:

    multi method new(Int :$year!, Int :$month = 1, Int :$day = 1,
                     Int :$hour = 0, Int :$minute = 0, :$second = 0,
                     Int :$timezone = 0, :&formatter)
    multi method new(Date :$date!,
                     Int :$hour = 0, Int :$minute = 0, :$second = 0,
                     Int :$timezone = 0, :&formatter)
    multi method new(Int() $year, Int() $month, Int() $day,
                     Int() $hour, Int $minute, $second,
                     Int() $timezone, :&formatter)
    multi method new(Instant:D $i,  :$timezone=0, :&formatter)
    multi method new(Int:D $posix,  :$timezone=0, :&formatter)
    multi method new(Str:D $format, :$timezone=0, :&formatter)

Usage:

    $datetime = DateTime.new(YEAR, MONTH?, DAY?, HOUR?, MINUTE?, SECOND?,
      TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(DATE, HOUR?, MINUTE?, SECOND?, TIMEZONE?,
      FORMATTER?)
    $datetime = DateTime.new(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND,
      TIMEZONE, FORMATTER?)
    $datetime = DateTime.new(INSTANT, TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(TIMESTAMP, TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(FORMAT, TIMEZONE?, FORMATTER?)

Creates a new C<DateTime> object. One option for creating a new DateTime object
is from the components (year, month, day, hour, ...) separately. Another is to
pass a L<Date|/type/Date> object for the date component, and specify the time
component-wise. Yet another is to obtain the time from an
L<Instant|/type/Instant>, and only supply the time zone and formatter. Or
instead of an Instant you can supply an L<Int|/type/Int> as a UNIX timestamp.

You can also supply a C<Str|/type/Str> formatted in ISO 8601 timestamp
notation or as a full L<RFC 3339|http://tools.ietf.org/html/rfc3339>
date and time.  Strings should be formatted as C<yyyy-mm-ddThh:mm::ssZ>
or C<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
L<X::Temporal::InvalidFormat>. If you supply a string that includes a time
zone and supply the C<timezone> named argument, an exception of type
L<X::DateTime:TimezoneClash> is thrown.

=head2 method now

Defined as:

    method now(:$timezone=$*TZ, &formatter) returns DateTime:D

Usage:

    DateTime.now

Creates a new C<DateTime> object from the current system time, optionally with
a different timezone than the default attached.

=head2 method clone

Defined as:

    method clone(:$year, :$month, :$day, :$hour, :$minute, :$second,
                 :$timezone, :&formatter)

Usage:

    DATETIME.clone(YEAR, MONTH, DATE, HOUR, MINUTE, SECOND, TIMEZONE,
    FORMATTER)

Creates a new C<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);
        # 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);
        # day of 2015/2 out of range. Is: 29, should be in 1..28

=head2 method hour

Defined as:

    method hour(DateTime:D:) returns Int:D

Usage:

   DATETIME.hour

Returns the hour component.

    say DateTime.new('2012-02-29T12:34:56Z').hour;      # 12

=head2 method minute

Defined as:

    method minute(DateTime:D:) returns Int:D

Usage:

    DATETIME.minute

Returns the minute component.

    say DateTime.new('2012-02-29T12:34:56Z').minute;     # 34

=head2 method second

Defined as:

    method second(DateTime:D:)

Usage:

    DATETIME.second

Returns the second component, including potentially fractional seconds.

    say DateTime.new('2012-02-29T12:34:56Z').second;     # 56
    say DateTime.new('2012-02-29T12:34:56.789Z').second; # 56.789
    say DateTime.new('2012-02-29T12:34:56,789Z').second; # comma also ok

=head2 method whole-second

Defined as:

    method whole-second(DateTime:D:)

Usage:

    DATETIME.whole-second

Returns the second component, rounded down to an L<Int|/type/Int>.

    say DateTime.new('2012-02-29T12:34:56.789Z').whole-second;      # 56

=head2 method timezone

Defined as:

    method timezone(DateTime:D:) returns Int:D

Usage:

    DATETIME.timezone

Returns the time zone in seconds as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').timezone;      # 7200

=head2 method offset

Defined as:

    method offset(DateTime:D:) returns Int:D

Usage:

    DATETIME.offset

Returns the time zone in seconds as an offset from UTC. This is an alias for
L<#method timezone>.

    say DateTime.new('2015-12-24T12:23:00+0200').offset;            # 7200

=head2 method offset-in-minutes

Defined as:

    method offset-in-minutes(DateTime:D:) returns Real:D

Usage:

    DATETIME.offset-in-minutes

Returns the time zone in minutes as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').offset-in-minutes; # 120

=head2 method offset-in-hours

Defined as:

    method offset-in-hours(DateTime:D:) returns Real:D

Usage:

    DATETIME.offset-in-hours

Returns the time zone in hours as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').offset-in-hours;   # 2

=head2 method Str

Defined as:

    method Str(DateTime:D:) returns Str:D

Usage:

    DATETIME.Str

Returns a string representation of the invocant, as done by
L<the formatter|#method formatter>.  If no formatter was specified, an
ISO 8601 timestamp will be returned.

=head2 method Instant

Defined as:

    method Instant(DateTime:D:) returns Instant:D

Usage:

    DATETIME.Instant

Returns an L<Instant|/type/Instant> object based on the invocant.

=head2 method posix

Defined as:

    method posix(DateTime:D: $ignore-timezone = False) returns Int:D

Usage:

    DATETIME.posix

Returns the date and time as a POSIX/UNIX timestamp.

    say DateTime.new('2015-12-24T12:23:00Z').posix;     # 1450959780

=head2 method later

Defined as:

    method later(DateTime:D: *%unit)

Usage:

    DATETIME.later(DELTA)

Returns a DateTime object based on the current one, but with a time delta
applied. The time delta can be passed as a named argument where the argument
name is the unit.

Allowed units are C<second>, C<seconds>, C<minute>, C<minutes>, C<hour>,
C<hours>, C<day>, C<days>, C<week>, C<weeks>, C<month>, C<months>, C<year>,
C<years>.

Please note that the special ":2nd" named parameter syntax can be a compact
and self-documenting way of specifying the delta

    say DateTime.new('2015-12-24T12:23:00Z').later(:2years);
        # 2017-12-24T12:23:00Z

Since addition of several different time units is not commutative, only one
unit may be passed.

    my $d = DateTime.new(date => Date.new('2015-02-27'));
    say $d.later(month => 1).later(:2days);  # 2015-03-29T00:00:00Z
    say $d.later(days => 2).later(:1month);  # 2015-04-01T00:00:00Z
    say $d.later(days => 2).later(:month);   # same, as +True === 1

Negative offsets are allowed, though L<#method earlier> is more idiomatic for
that.

=head2 method earlier

Defined as:

    method earlier(DateTime:D: *%unit)

Usage:

    DATETIME.earlier(DELTA)

Returns a DateTime object based on the current one, but with a time delta
towards the past applied. See L<#method later> for usage.

=head2 method truncated-to

Defined as:

    method truncated-to(DateTime:D: Cool $unit)

Usage:

    DATETIME.truncated-to(UNIT)

Returns a copy of the invocant, with everything smaller than the specified
unit truncated to the smallest possible value.

    my $d = DateTime.new("2012-02-29T12:34:56Z");
    say $d.truncated-to('minute');      # 2012-02-29T12:34:00Z
    say $d.truncated-to('hour');        # 2012-02-29T12:00:00Z
    say $d.truncated-to('day');         # 2012-02-29T00:00:00Z
    say $d.truncated-to('month');       # 2012-02-01T00:00:00Z
    say $d.truncated-to('year');        # 2012-01-01T00:00:00Z

DateTimes with fractional seconds can be truncated to whole seconds with
C<.truncated-to('second')>.

=head2 method Date

Defined as:

    method Date(DateTime:D:) returns Date:D

Usage:

    DATETIME.Date

Returns a L<Date|/type/Date> object for this DateTime object. Which obviously
lacks the time component.

=head2 method utc

Defined as:

    method utc(DateTime:D:) returns DateTime:D

Usage:

    DATETIME.utc

Returns a DateTime object for the same time, but in time zone UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').utc";  # 2015-12-24T10:23:00Z

=head2 method in-timezone

Defined as:

    method in-timezone(DateTime:D: $timezone = 0) returns DateTime:D

Usage:

    DATETIME.in-timezone

Returns a DateTime object for the same time, but in the specified time zone.

    say DateTime.new('2015-12-24T12:23:00Z').in-timezone(3600 + 1800);
        # 2015-12-24T13:53:00+0130

=head2 method local

Defined as:

    method local(DateTime:D:) returns DateTime:D

Usage:

    DATETIME.local

Returns a DateTime object for the same time, but in the local time zone
(C<$*TZ>).

    my $*TZ = -3600;
    say DateTime.new('2015-12-24T12:23:00+0200').local;
            # 2015-12-24T09:23:00-0100
=end pod
<commit_msg>Add example of formatter, Floyd-ATC++<commit_after>=begin pod

=TITLE class DateTime

=SUBTITLE Calendar date with time

    class DateTime does Dateish { ... }

For handling points in civil time, a C<DateTime> object stores year, month,
day, hour, minute (all L<Int|/type/Int>), second (potentially fractional) and
a time zone.

It provides methods for calculating with date and time.

C<DateTime> methods are immutable; if you are tempted to modify one, create a
modified copy instead.

Time zones are handled as L<Integers|/type/Int> in B<seconds> offset from UTC,
not by time zone name.

=begin code
use v6;
my $dt = DateTime.new(
    year    => 2015,
    month   => 11,
    day     => 21,
    hour    => 16,
    minute  => 1,
);

say $dt;                            # 2015-11-21T16:01:00Z
say $dt.later(days => 20);          # 2015-12-11T16:01:00Z
say $dt.truncated-to('hour');       # 2015-11-21T16:00:00Z
say $dt.in-timezone(-8 * 3600);     # 2015-11-21T08:01:00-0800

my $now = DateTime.now(formatter => { .hour ~ ":" ~ .minute });
say $now;                           # 12:45 (or something like that)
=end code

=head1 Methods

=head2 method new

Defined as:

    multi method new(Int :$year!, Int :$month = 1, Int :$day = 1,
                     Int :$hour = 0, Int :$minute = 0, :$second = 0,
                     Int :$timezone = 0, :&formatter)
    multi method new(Date :$date!,
                     Int :$hour = 0, Int :$minute = 0, :$second = 0,
                     Int :$timezone = 0, :&formatter)
    multi method new(Int() $year, Int() $month, Int() $day,
                     Int() $hour, Int $minute, $second,
                     Int() $timezone, :&formatter)
    multi method new(Instant:D $i,  :$timezone=0, :&formatter)
    multi method new(Int:D $posix,  :$timezone=0, :&formatter)
    multi method new(Str:D $format, :$timezone=0, :&formatter)

Usage:

    $datetime = DateTime.new(YEAR, MONTH?, DAY?, HOUR?, MINUTE?, SECOND?,
      TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(DATE, HOUR?, MINUTE?, SECOND?, TIMEZONE?,
      FORMATTER?)
    $datetime = DateTime.new(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND,
      TIMEZONE, FORMATTER?)
    $datetime = DateTime.new(INSTANT, TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(TIMESTAMP, TIMEZONE?, FORMATTER?)
    $datetime = DateTime.new(FORMAT, TIMEZONE?, FORMATTER?)

Creates a new C<DateTime> object. One option for creating a new DateTime object
is from the components (year, month, day, hour, ...) separately. Another is to
pass a L<Date|/type/Date> object for the date component, and specify the time
component-wise. Yet another is to obtain the time from an
L<Instant|/type/Instant>, and only supply the time zone and formatter. Or
instead of an Instant you can supply an L<Int|/type/Int> as a UNIX timestamp.

You can also supply a C<Str|/type/Str> formatted in ISO 8601 timestamp
notation or as a full L<RFC 3339|http://tools.ietf.org/html/rfc3339>
date and time.  Strings should be formatted as C<yyyy-mm-ddThh:mm::ssZ>
or C<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
L<X::Temporal::InvalidFormat>. If you supply a string that includes a time
zone and supply the C<timezone> named argument, an exception of type
L<X::DateTime:TimezoneClash> is thrown.

=head2 method now

Defined as:

    method now(:$timezone=$*TZ, &formatter) returns DateTime:D

Usage:

    DateTime.now(TIMEZONE?, FORMATTER?)

Creates a new C<DateTime> object from the current system time, optionally with
a different timezone than the default attached.


=head2 method clone

Defined as:

    method clone(:$year, :$month, :$day, :$hour, :$minute, :$second,
                 :$timezone, :&formatter)

Usage:

    DATETIME.clone(YEAR, MONTH, DATE, HOUR, MINUTE, SECOND, TIMEZONE,
    FORMATTER)

Creates a new C<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);
        # 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);
        # day of 2015/2 out of range. Is: 29, should be in 1..28

=head2 method hour

Defined as:

    method hour(DateTime:D:) returns Int:D

Usage:

   DATETIME.hour

Returns the hour component.

    say DateTime.new('2012-02-29T12:34:56Z').hour;      # 12

=head2 method minute

Defined as:

    method minute(DateTime:D:) returns Int:D

Usage:

    DATETIME.minute

Returns the minute component.

    say DateTime.new('2012-02-29T12:34:56Z').minute;     # 34

=head2 method second

Defined as:

    method second(DateTime:D:)

Usage:

    DATETIME.second

Returns the second component, including potentially fractional seconds.

    say DateTime.new('2012-02-29T12:34:56Z').second;     # 56
    say DateTime.new('2012-02-29T12:34:56.789Z').second; # 56.789
    say DateTime.new('2012-02-29T12:34:56,789Z').second; # comma also ok

=head2 method whole-second

Defined as:

    method whole-second(DateTime:D:)

Usage:

    DATETIME.whole-second

Returns the second component, rounded down to an L<Int|/type/Int>.

    say DateTime.new('2012-02-29T12:34:56.789Z').whole-second;      # 56

=head2 method timezone

Defined as:

    method timezone(DateTime:D:) returns Int:D

Usage:

    DATETIME.timezone

Returns the time zone in seconds as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').timezone;      # 7200

=head2 method offset

Defined as:

    method offset(DateTime:D:) returns Int:D

Usage:

    DATETIME.offset

Returns the time zone in seconds as an offset from UTC. This is an alias for
L<#method timezone>.

    say DateTime.new('2015-12-24T12:23:00+0200').offset;            # 7200

=head2 method offset-in-minutes

Defined as:

    method offset-in-minutes(DateTime:D:) returns Real:D

Usage:

    DATETIME.offset-in-minutes

Returns the time zone in minutes as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').offset-in-minutes; # 120

=head2 method offset-in-hours

Defined as:

    method offset-in-hours(DateTime:D:) returns Real:D

Usage:

    DATETIME.offset-in-hours

Returns the time zone in hours as an offset from UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').offset-in-hours;   # 2

=head2 method Str

Defined as:

    method Str(DateTime:D:) returns Str:D

Usage:

    DATETIME.Str

Returns a string representation of the invocant, as done by
L<the formatter|#method formatter>.  If no formatter was specified, an
ISO 8601 timestamp will be returned.

=head2 method Instant

Defined as:

    method Instant(DateTime:D:) returns Instant:D

Usage:

    DATETIME.Instant

Returns an L<Instant|/type/Instant> object based on the invocant.

=head2 method posix

Defined as:

    method posix(DateTime:D: $ignore-timezone = False) returns Int:D

Usage:

    DATETIME.posix

Returns the date and time as a POSIX/UNIX timestamp.

    say DateTime.new('2015-12-24T12:23:00Z').posix;     # 1450959780

=head2 method later

Defined as:

    method later(DateTime:D: *%unit)

Usage:

    DATETIME.later(DELTA)

Returns a DateTime object based on the current one, but with a time delta
applied. The time delta can be passed as a named argument where the argument
name is the unit.

Allowed units are C<second>, C<seconds>, C<minute>, C<minutes>, C<hour>,
C<hours>, C<day>, C<days>, C<week>, C<weeks>, C<month>, C<months>, C<year>,
C<years>.

Please note that the special ":2nd" named parameter syntax can be a compact
and self-documenting way of specifying the delta

    say DateTime.new('2015-12-24T12:23:00Z').later(:2years);
        # 2017-12-24T12:23:00Z

Since addition of several different time units is not commutative, only one
unit may be passed.

    my $d = DateTime.new(date => Date.new('2015-02-27'));
    say $d.later(month => 1).later(:2days);  # 2015-03-29T00:00:00Z
    say $d.later(days => 2).later(:1month);  # 2015-04-01T00:00:00Z
    say $d.later(days => 2).later(:month);   # same, as +True === 1

Negative offsets are allowed, though L<#method earlier> is more idiomatic for
that.

=head2 method earlier

Defined as:

    method earlier(DateTime:D: *%unit)

Usage:

    DATETIME.earlier(DELTA)

Returns a DateTime object based on the current one, but with a time delta
towards the past applied. See L<#method later> for usage.

=head2 method truncated-to

Defined as:

    method truncated-to(DateTime:D: Cool $unit)

Usage:

    DATETIME.truncated-to(UNIT)

Returns a copy of the invocant, with everything smaller than the specified
unit truncated to the smallest possible value.

    my $d = DateTime.new("2012-02-29T12:34:56Z");
    say $d.truncated-to('minute');      # 2012-02-29T12:34:00Z
    say $d.truncated-to('hour');        # 2012-02-29T12:00:00Z
    say $d.truncated-to('day');         # 2012-02-29T00:00:00Z
    say $d.truncated-to('month');       # 2012-02-01T00:00:00Z
    say $d.truncated-to('year');        # 2012-01-01T00:00:00Z

DateTimes with fractional seconds can be truncated to whole seconds with
C<.truncated-to('second')>.

=head2 method Date

Defined as:

    method Date(DateTime:D:) returns Date:D

Usage:

    DATETIME.Date

Returns a L<Date|/type/Date> object for this DateTime object. Which obviously
lacks the time component.

=head2 method utc

Defined as:

    method utc(DateTime:D:) returns DateTime:D

Usage:

    DATETIME.utc

Returns a DateTime object for the same time, but in time zone UTC.

    say DateTime.new('2015-12-24T12:23:00+0200').utc";  # 2015-12-24T10:23:00Z

=head2 method in-timezone

Defined as:

    method in-timezone(DateTime:D: $timezone = 0) returns DateTime:D

Usage:

    DATETIME.in-timezone

Returns a DateTime object for the same time, but in the specified time zone.

    say DateTime.new('2015-12-24T12:23:00Z').in-timezone(3600 + 1800);
        # 2015-12-24T13:53:00+0130

=head2 method local

Defined as:

    method local(DateTime:D:) returns DateTime:D

Usage:

    DATETIME.local

Returns a DateTime object for the same time, but in the local time zone
(C<$*TZ>).

    my $*TZ = -3600;
    say DateTime.new('2015-12-24T12:23:00+0200').local;
            # 2015-12-24T09:23:00-0100
=end pod

{'fraction_non_alphanumeric': 0.11700468018720749, 'fraction_numerical': 0.06630265210608424, 'mean_word_length': 3.401931330472103, 'pattern_counts': {'":': 3, '<': 85, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1510126', 'n_tokens_mistral': 8051, 'n_tokens_neox': 6816, 'n_words': 2215}