DateTime::Format::Epoch - Convert DateTimes to/from epoch seconds
use DateTime::Format::Epoch; my $dt = DateTime->new( year => 1970, month => 1, day => 1 ); my $formatter = DateTime::Format::Epoch->new( epoch => $dt, # UNIX epoch unit => 'seconds', type => 'int', # or 'float', 'bigint' skip_leap_secs => 1, local_epoch => 0, ); my $dt2 = $formatter->parse_datetime( 1051488000 ); # 2003-04-28T00:00:00 $formatter->format_datetime($dt2); # 1051488000
This module can convert a DateTime object (or any object that can be converted to a DateTime object) to the number of seconds since a given epoch. It can also do the reverse.
new( ... )
Constructor of the formatter/parser object. It can take the following parameters: "epoch", "unit", "type", "skip_leap_seconds", and "local_epoch".
The epoch parameter is the only required parameter. It should be a DateTime object (or at least, it has to be convertible to a DateTime object).
The unit parameter can be "seconds", "milliseconds, "microseconds" or "nanoseconds". The default is "seconds".
The type parameter specifies the type of the return value. It can be "int" (returns integer value), "float" (returns floating point value), or "bigint" (returns Math::BigInt value). The default is either "int" (if the unit is "seconds"), or "bigint" (if the unit is nanoseconds).
The default behaviour of this module is to skip leap seconds. This is what (most versions of?) UNIX do. If you want to include leap seconds, set skip_leap_seconds to false.
Some operating systems use an epoch defined in the local timezone of the computer. If you want to use such an epoch in this module, you have two options. The first is to submit a DateTime object with the appropriate timezone. The second option is to set the local_epoch parameter to a true value. In this case, you should submit an epoch with a floating timezone. The exact epoch used in format_datetime will then depend on the timezone of the object you pass to format_datetime.
format_datetime
format_datetime($datetime)
Given a DateTime object, this method returns the number of seconds since the epoch.
Support for this module is provided via the datetime@perl.org email list. See http://lists.perl.org/ for more details.
Eugene van der Pijll <pijll@gmx.net>
Copyright (c) 2003 Eugene van der Pijll. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
DateTime
datetime@perl.org mailing list
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install DateTime::Format::Epoch, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DateTime::Format::Epoch
CPAN shell
perl -MCPAN -e shell install DateTime::Format::Epoch
For more information on module installation, please visit the detailed CPAN module installation guide.