++ed by:

1 PAUSE user

John Ingram


Data::Maker::Field::DateTime - A Data::Maker field class that generates DateTime values.


  use Data::Maker;
  use Data::Maker::Field::DateTime;

  my $maker = Data::Maker->new(
    record_count => 10,
    fields => [
        name => 'date_of_birth',
        class => 'Data::Maker::Field::DateTime',
        args => {
          relative_to => 'now',
          subtract => { years => [0..80], days => [0..365] ]


Data::Maker::Field::DateTime takes any of the following arguments:

  • start

    By default, DateTime could return ANY date and time. This is usually not what you want. This parameter can be either a DateTime object, or a four-digit year. The DateTime object returned will be no earlier than given date, or January 1st of given year.

  • end

    This is the other end of the range that begins with start() (above). The DateTime object returned will be no later than the given date, or January 1st of the given year.

  • format (CodeRef)

    A code reference that will be passed the generated DateTime object immediately before the random value is returned to Data::Maker. While it is often most useful to return the DateTime object itself, it is occasionally preferred to return a string representation of the date so your other code doesn't have to deal with DateTime objects. This is how you would do this.

  • relative_to

    The name of another Data::Maker::Field::DateTime field in the same Data::Maker object. Combined with the subtract() or add() parameters (below), this allows you to generate a random Data::Maker::Field::DateTime object that is relative to an already-generated Data::Maker::Field::DateTime object in the same record. Note that you can only use this parameter to refer to a field that appears previous to this field in the list.

    In addition to specifying the name of another field, you can also use the keyword 'now' or the keyword 'today', both of which have the same effect: DateTime->now is used as the starting point. This is useful for many things. The example at the beginning of this POD demonstrates how to use this feature to generate a birthday of somebody between 20 and 80 years old.

  • subtract (HashRef)

    This parameter takes a hash reference that is passed to DateTime::Duration as the amount of time to subtract from the Data::Maker::Field::DateTime object referrred to by the relative_to() parameter (see above).

    It can either be a fixed value...

      subtract => { months => 2 }

    ...or it can be a randomly-chosen value, by simply passing an array reference of choices or a range:

      subtract => { years => [0..80], days => [0..365] ]
  • add (HashRef)

    This parameter is just like subtract() (above), but it moves the Data::Maker::Field::DateTime object in the opposite direction.


John Ingram (john@funnycow.com)


Copyright 2013 by John Ingram. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.