Skip Menu |

This queue is for tickets about the Time-Piece CPAN distribution.

Report information
The Basics
Id: 62189
Status: resolved
Priority: 0/
Queue: Time-Piece

People
Owner: Nobody in particular
Requestors: ifomichev [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 1.20
Fixed in: 1.31_04



Subject: truncate method
Thank you for a great module. The only thing I really miss in it is a truncate method, similar to the one that DateTime has. my $dt = Time::Piece->gmtime; my $midnight = $dt->truncate(to => 'day'); # or whatever
I wrote a monkey patch, that is sufficient to me. If you're interested to add this functionality , I would be glad to make a patch and write tests for the distribution. ---- #!/usr/bin/perl use strict; use warnings; use Time::Piece; local *Time::Piece::truncate; *Time::Piece::truncate = sub { package Time::Piece; my ($time, $to) = @_; return $time->_mktime([(0) x $to, @$time[$to..c_isdst]], $time-> [c_islocal]); }; my $now = Time::Piece->gmtime; my $midnight = $now->truncate(Time::Piece::c_mday); print $midnight, "\n";
Hmm... Sorry, I've found a bug :) local *Time::Piece::truncate; *Time::Piece::truncate = sub { package Time::Piece; my ($time, $to) = @_; my @t = (0, 0, 0, 1, 0); return $time->_mktime([@t[0..$to-1], @$time[$to..c_isdst]], $time-> [c_islocal]); };
On 2010-10-18 10:44:47, IFOMICHEV wrote: Show quoted text
> Hmm... Sorry, I've found a bug :) > > local *Time::Piece::truncate; > *Time::Piece::truncate = sub { > package Time::Piece; > my ($time, $to) = @_; > my @t = (0, 0, 0, 1, 0); > return $time->_mktime([@t[0..$to-1], @$time[$to..c_isdst]], $time-> > [c_islocal]); > };
A patch in the form originally described (to => 'days') would be welcome. -- rjbs
I've put a PR together to implement this. See https://github.com/rjbs/Time-Piece/pull/25