A date object, with as little code as possible
# Create a date manually $christmas = DateTime::Tiny->new( year => 2006, month => 12, day => 25, hour => 10, minute => 45, second => 0, ); # Show the current date my $now = DateTime::Tiny->now; print "Year : " . $now->year . "\n"; print "Month : " . $now->month . "\n"; print "Day : " . $now->day . "\n"; print "Hour : " . $now->hour . "\n"; print "Minute : " . $now->minute . "\n"; print "Second : " . $now->second . "\n";
DateTime::Tiny is a most prominent member of the DateTime::Tiny suite of time modules.
It implements an extremely lightweight object that represents a datetime.
Many \s-1CPAN\s0 modules which provide the best implementation of a certain concepts are 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 useful subset of their 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 (although this is just a guideline), 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 arguably only module to implement it completely correct is DateTime. However, to implement it properly DateTime is quite slow and requires 3-4 megabytes of memory to load.
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, manipulate or convert the object.
As a result, DateTime::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 type work.
The problem with this is that having no ability to modify date limits the usefulness greatly.
To make up for this, if you have DateTime installed, any DateTime::Tiny module can be inflated into the equivalent DateTime as needing, loading DateTime on the fly if necessary.
This is somewhat similar to DateTime::LazyInit, but unlike that module DateTime::Tiny is not modifiable.
For the purposes of date/time logic, all DateTime::Tiny objects exist in the \*(L"C\*(R" locale, and the \*(L"floating\*(R" time zone. This may be improved in the future if a suitably tiny way of handling timezones is found.
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 \s-1API\s0 be as close as possible to the \s-1API\s0 for DateTime. Except, of course, that this module implements less of it.
my $date = DateTime::Tiny->new( year => 2006, month => 12, day => 31, hour => 10, minute => 45, second => 32, );
The \*(C`new\*(C' constructor creates a new DateTime::Tiny object.
It takes six named params. \*(C`day\*(C' should be the day of the month (1-31), \*(C`month\*(C' should be the month of the year (1-12), \*(C`year\*(C' as a 4 digit year. \*(C`hour\*(C' should be the hour of the day (0-23), \*(C`minute\*(C' should be the minute of the hour (0-59) and \*(C`second\*(C' should be the second of the minute (0-59).
These are the only params accepted.
Returns a new DateTime::Tiny object.
my $current_date = DateTime::Tiny->now;
The \*(C`now\*(C' 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 DateTime::Tiny object.
The \*(C`year\*(C' accessor returns the 4-digit year for the date.
The \*(C`month\*(C' accessor returns the 1-12 month of the year for the date.
The \*(C`day\*(C' accessor returns the 1-31 day of the month for the date.
The \*(C`hour\*(C' accessor returns the hour component of the time as an integer from zero to twenty-three (0-23) in line with 24-hour time.
The \*(C`minute\*(C' accessor returns the minute component of the time as an integer from zero to fifty-nine (0-59).
The \*(C`second\*(C' accessor returns the second component of the time as an integer from zero to fifty-nine (0-59).
The \*(C`ymdhms\*(C' method returns the most common and accurate stringified date format, which returns in the form \*(L"2006-04-12\*(R".
The \*(C`from_string\*(C' method creates a new DateTime::Tiny object from a string.
The string is expected to be an \s-1ISO\s0 8601 time, with seperators.
my $almost_midnight = DateTime::Tiny->from_string( '2006-12-20T23:59:59' );
Returns a new DateTime::Tiny object, or throws an exception on error.
The \*(C`as_string\*(C' method converts the date to the default string, which at present is the same as that returned by the \*(C`ymd\*(C' method above.
This string matches the \s-1ISO\s0 8601 standard for the encoding of a date as a string.
The \*(C`DateTime\*(C' method is used to create a DateTime object that is equivalent to the DateTime::Tiny object, for use in comversions and caluculations.
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.
Returns a DateTime object, or throws an exception if DateTime is not installed on the current host.
Bugs should be reported via the \s-1CPAN\s0 bug tracker at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DateTime-Tiny <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DateTime-Tiny>
For other issues, or commercial enhancement or support, contact the author.
Adam Kennedy <[email protected]>
DateTime, Date::Tiny, Time::Tiny, Config::Tiny, ali.as
Copyright 2006 - 2009 Adam Kennedy.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the \s-1LICENSE\s0 file included with this module.