Data::...ateTime(3pm)

πŸ’– NAME πŸ’–

Data::Faker::DateTime β€” bestie, it's a Data::Faker plugin. That's the whole vibe. It makes fake dates and times for you. πŸŽ€

(also the header says this is User Contributed Perl Documentation and the page lives at Data::...ateTime(3pm) β€” yes the title is literally truncated like that, no that's not me being lazy, that's just how the page header is written πŸ’€)


✨ SYNOPSIS AND USAGE ✨

okay so like, go see Data::Faker. That's it. That's the usage. fr fr. πŸ¦‹


🌸 DATA PROVIDERS 🌸

These are the little functions you can call. One bullet each, we're not skipping a single one, periodt.

  • unixtime β€” hands you a unix time (that's seconds since the epoch, iykyk) for a random moment somewhere between the epoch and right now.
  • date β€” a random date, as a string, using a random date format (peep date_format for that).
  • time β€” a random time, as a string, using a random time format (see time_format).
  • rfc822 β€” a random date all formatted up in RFC 822 style. ⚠️ heads up: this one might not work on systems running a non-GNU strftime, and the author is like "kindly let me know if that's the case." So if it breaks, tell him, bestie.
  • ampm β€” returns am or pm at random, in the current locale, using one of the formats listed in ampm_format.
  • time_format β€” a random time format.
  • date_format β€” a random date format.
  • ampm_format β€” a random am/pm format.
  • datetime_format β€” a random date-and-time format.
  • month β€” a random month name, unabbreviated, in the current locale.
  • month_abbr β€” a random month name, abbreviated, in the current locale.
  • weekday β€” a random weekday name, unabbreviated, in the current locale.
  • weekday_abbr β€” a random weekday name, abbreviated, in the current locale.
  • sqldate β€” a random date in the ISO8601 format that SQL servers are obsessed with (YYYY-MM-DD).
  • datetime_locale β€” a datetime string in the preferred date representation for the current locale, for a random date.
  • date_locale β€” a date string in the preferred date representation for the current locale, for a random date.
  • time_locale β€” a time string in the preferred date representation for the current locale, for a random date.
  • century β€” a random century number.
  • dayofmonth β€” a random day of the month.

🧚 UTILITY METHODS 🧚

Data::Faker::DateTime::timestr($format);

You hand it a strftime format specifier, and it passes that specifier through to POSIX::strftime along with a random date, so the date shows up in whatever format you asked for. iconic behaviour honestly.

Perl passes this through to the strftime function of your system library, so lowkey it's possible that some of the formatting tokens used here just won't work on your system. not me warning you twice, but here we are. πŸ™ˆ


πŸ’€ NOTES AND CAVEATS πŸ’€

Be careful building timestamps from pieces

Bestie. Listen. Be very careful about building date/time representations in formats that aren't already listed here. For example, if you wanted a date that's just the month and the day? You should NOT do this:

my $faker = Data::Faker->new();
print join(' ',$faker->month,$faker->dayofmonth)."\n";

This is bad, and here's why: you might end up with 'February 31'. Yes. A date that does not exist. 😭

Instead you should use the timestr utility function to get a formatted time for a valid date, or β€” better still β€” write a plugin function that does it for you:

my $faker = Data::Faker->new();
print $faker->my_short_date()."\n";

package Data::Faker::MyExtras;
use base qw(Data::Faker);
use Data::Faker::DateTime;
__PACKAGE__->register_plugin(
  my_short_date => sub { Data::Faker::DateTime::timestr('%M %e') },
);

look at that, a whole plugin, main character energy. ✨

POSIX::strftime

See the documentation above re: the timestr utility method for some caveats related to strftime and your system library. (yes it's the same caveat, no I will not elaborate, click up ⬆️)


🌈 SEE ALSO 🌈

  • Data::Faker

πŸ‘‘ AUTHOR πŸ‘‘

Jason Kohles, <email@jasonkohles.com>


Copyright 2004-2005 by Jason Kohles

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page babes πŸ’… this was perl v5.36.0, dated 2022-10-22, and it's a Data::...ateTime(3pm) situation through and through. go forth and generate fake dates responsibly, mwah πŸ“

πŸ“¦ where this page comes from