Date Time Formats

Dave Rolsky edited this page Jan 30, 2017 · 1 revision

How Do I Convert between Epoch Times and DateTime Objects?

DateTime provides a constructor from_epoch(...) that takes an epoch argument with a value giving the count since the epoch. The exact definition of the epoch varies depending on the system, but if you use time() and friends to get the value then this will behave correctly. (Note that the constructor also takes time_zone and locale parameters).

See also DateTime::Format::Epoch for more control over the exact definition of the epoch you are using. You might need to use this if you are getting epoch times from another system. e.g. on Unix the epoch is defined as Jan 1st, 1970 at 00:00:00 and the epoch times are given as a count of seconds since then (disregarding leap seconds).

To get the epoch corresponding to a given DateTime use the epoch() method. If you want a high resolution time that includes nanoseconds use hires_epoch().

When you create a time from an epoch, the time zone is automatically set to 'UTC', unless you supply a time_zone parameter to override this.

my $time = 1057632876;
my $dt = DateTime->from_epoch( epoch => $time );
print $dt->datetime(), "\n";

my $epoch = $dt->epoch();
print $dt->epoch(), "\n";

How Do I Convert between Times from the Perl Builtin Functionality and DateTime Objects?

See How Do I Convert between Epoch Times and DateTime Objects? for the recipe. Use the time() function to get epoch times or Time::Local if you have the time pieces from gmtime or localtime.

How Do I Convert between POSIX Times and DateTime Objects?

See How Do I Convert between Epoch Times and DateTime Objects? for the recipe.

How Do I Convert between Time::HiRes and DateTime Objects?

See How Do I Convert between Epoch Times and DateTime Objects? for the recipe. Note that DateTime->from_epoch(...) will preserve the nanoseconds when doing the conversion, but you have to use hires_epoch() to get the correct time back.

How Do I Convert between Times from the TimeDate Family and DateTime Objects?

See How Do I Convert between Epoch Times and DateTime Objects? for the recipe.

How Do I Convert between Time::Piece Times and DateTime Objects?

See How Do I Convert between Epoch Times and DateTime Objects? for the basic recipe. However you need to call Time::Piece's epoch method to get the epoch from the object. To create a Time::Piece object you will need to call gmtime or localtime (which is overriden by Time::Piece) with the value from DateTime's epoch().

use Time::Piece;

my $lt1 = localtime(1057632876);
my $dt = DateTime->from_epoch( epoch => $lt1->epoch() );
print $dt->datetime(), "\n";    # Prints: 2003-07-08T02:54:36

my $lt2 = gmtime( $dt->epoch() );
print $lt2, "\n";               # Prints: Tue Jul  8 02:54:36 2003

How Do I Convert between Date::Manip and DateTime Objects?

Use the DateTime::Format::DateManip module. Note that you can also use the same module to convert Date::Manip durations.

use DateTime::Format::DateManip;
use Date::Manip;

my $dt = DateTime::Format::DateManip->parse_datetime(
    "Jan 1st, 2001 12:30 AM GMT");
my $dm = DateTime::Format::DateManip->format_datetime($dt);

# Similarly we have to use the epoch to do comparisons because
# Date::Manip stores the times in the local time zone
my $ep = UnixDate( $dm, "%s" );
is(
    $ep,
    '978309000',
    "DateTime::Format::Manip->format_datetime()"
);

How Do I Convert between Date::Calc Times and DateTime Objects?

This is a little tricky because Date::Calc allows you to have GMT based times and local times, but you have to track which is which manually. The best way to handle this is to use the DateToTime(...) or Mktime(...) functions to convert a GMT or Local time to an epoch, then use the recipe in "How Do I Convert between Epoch Times and DateTime Objects?"

To convert from a DateTime into the Date::Calc form, use the Gmtime() or Localtime() functions with the DateTime's epoch() depending on what kind of time you want returned.

To work with a Date::Calc::Object the methods are similar, just the calling style changes.