Date::Tiny - A date object, with as little code as possible
# Create a date manually $christmas = Date::Tiny->new( year => 2006, month => 12, day => 25, ); # Show the current date $today = Date::Tiny->now; print "Year : " . $today->year . "\n"; print "Month: " . $today->month . "\n"; print "Day : " . $today->day . "\n";
Date::Tiny is a member of the DateTime::Tiny suite of time modules.
It implements an extremely lightweight object that represents a date, without any time data.
Many CPAN modules which provide the best implementation of a concept can be very large. For some reason, this generally seems to be about 3 megabyte of ram usage to load the module.
For a lot of the situations in which these large and comprehensive implementations exist, some people will only need a small fraction of the functionality, or only need this functionality in an ancillary role.
The aim of the Tiny modules is to implement an alternative to the large module that implements a subset of the functionality, using as little code as possible.
Typically, this means a module that implements between 50% and 80% of the features of the larger module, but using only 100 kilobytes of code, which is about 1/30th of the larger module.
Due to the inherent complexity, Date and Time is intrinsically very difficult to implement properly.
The challenge in implementing a Tiny equivalent to DateTime is to do so without making the functionality critically flawed, and to carefully select the subset of functionality to implement.
If you look at where the main complexity and cost exists, you will find that it is relatively cheap to represent a date or time as an object, but much much more expensive to modify or convert the object.
As a result, Date::Tiny provides the functionality required to represent a date as an object, to stringify the date and to parse it back in, but does not allow you to modify the dates.
The purpose of this is to allow for date object representations in situations like log parsing and fast real-time work.
The problem with this is that having no ability to modify date limits the usefulness greatly.
For the purposes of date/time logic, all Date::Tiny objects exist in the "C" locale, and the "floating" time zone (although obviously in a pure date context, the time zone largely doesn't matter).
When converting up to full DateTime objects, these local and time zone settings will be applied (although an ability is provided to override this).
In addition, the implementation is strictly correct and is intended to be very easily to sub-class for specific purposes of your own.
In general, the intent is that the API be as close as possible to the API for DateTime. Except, of course, that this module implements less of it.
my $date = Date::Tiny->new( year => 2006, month => 12, day => 31, );
new constructor creates a new Date::Tiny object.
It takes three named parameters.
day should be the day of the month (1-31),
month should be the month of the year (1-12),
year as a 4 digit year.
These are the only parameters accepted.
Returns a new Date::Tiny object.
my $current_date = Date::Tiny->now;
now method creates a new date object for the current date.
The date created will be based on localtime, despite the fact that the date is created in the floating time zone.
Returns a new Date::Tiny object.
year accessor returns the 4-digit year for the date.
month accessor returns the 1-12 month of the year for the date.
day accessor returns the 1-31 day of the month for the date.
ymd method returns the most common and accurate stringified date format, which returns in the form "2006-04-12".
as_string method converts the date to the default string, which at present is the same as that returned by the
ymd method above.
This string matches the ISO 8601 standard for the encoding of a date as a string.
from_string method creates a new Date::Tiny object from a string.
The string is expected to be a "yyyy-mm-dd" ISO 8601 time string.
my $almost_christmas = Date::Tiny->from_string( '2006-12-23' );
Returns a new Date::Tiny object, or throws an exception on error.
DateTime method is used to create a DateTime object that is equivalent to the Date::Tiny object, for use in conversions and calculations.
As mentioned earlier, the object will be set to the 'C' locate, and the 'floating' time zone.
If installed, the DateTime module will be loaded automatically.
This module was written by Adam Kennedy in 2006. In 2016, David Golden adopted it as a caretaker maintainer.
Please report any bugs or feature requests through the issue tracker at https://github.com/dagolden/Date-Tiny/issues. You will be notified automatically of any progress on your issue.
This is open source software. The code repository is available for public review and contribution under the terms of the license.
git clone https://github.com/dagolden/Date-Tiny.git
Adam Kennedy <firstname.lastname@example.org>
David Golden <email@example.com>
This software is copyright (c) 2006 by Adam Kennedy.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.