The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Mojar::Cron::Datetime - Lightweight datetime with small footprint

SYNOPSIS

  use Mojar::Cron::Datetime;
  say Mojar::Cron::Datetime->now->to_string;
  my $d = Mojar::Cron::Datetime->from_string('2001-12-25 00:00:01');
  $d->day($d->day + 14);
  $d->normalise;
  say "$d";

DESCRIPTION

CONSTRUCTORS

new

Construct a datetime from passed arguments.

  $d = Mojar::Cron::Datetime->new;  # zero datetime
  $d = $datetime->new;  # clone
  $d = Mojar::Cron::Datetime->new([00, 00, 20, 26, 06, 112]);
  $d = Mojar::Cron::Datetime->new(00, 00, 20, 26, 06, 112);

The first constructs the zero datetime '1900-01-01 00:00:00'. The second clones the value of $datetime. The third uses the passed value (2012-07-27 21:00:00 London time expressed in UTC). The fourth does the same but using its own reference.

now

  $d = Mojar::Cron::Datetime->now;
  $d = Mojar::Cron::Datetime->now($use_local);
  $d = $d->now;

Constructs a datetime for now. Uses UTC clock unless passed a true value (indicating to use local clock). If called as an object method, ignores the value of the object, so it gives the same result as the class method. (Compare to new which uses the object's value.)

from_string

  $d = Mojar::Cron::Datetime->from_string('2012-07-27 20:00:00');
  $d = Mojar::Cron::Datetime->from_string('2012-07-28T01:00:00', 1);

Constructs a datetime by parsing an ISO 8601 string. (The method only supports the formats shown, where 'T' is optional, and not any of the other 8601 variants.) Uses UTC clock unless passed a true value (indicating to use local clock). Both examples result in the same value if the machine's clock is in UTC+5.

METHODS

copy

  $second = Mojar::Cron::Datetime->new->copy($first);

Copies the constituent values from another datetime object.

normalise

to_string

  say "$dt";
  say $dt->to_string;
  say $dt->to_string('%Y-%m-%d %H:%M:%S');
  say Mojar::Cron::Datetime->to_string($dt, '%Y-%m-%d');
  say Mojar::Cron::Datetime->to_string([00,00,00, 25,11,101], '%A');

Stringifies the datetime object using the given format. The default format is '%Y-%m-%d %H:%M:%S'. The first three examples are equivalent.

COPYRIGHT AND LICENCE

Copyright (C) 2012--2016, Nic Sandfield.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

DateTime, Time::Moment.