开发者

get timestamp in perl

开发者 https://www.devze.com 2023-02-17 16:37 出处:网络
my yesterday date is 201103116 I want to take starttime timestamp and endtime time stamp for yes开发者_高级运维terday in perl.Using DateTime:

my yesterday date is 201103116

I want to take starttime timestamp and endtime time stamp for yes开发者_高级运维terday in perl.


Using DateTime:

use DateTime;

my $start = DateTime->now(time_zone => 'local')->subtract(days => 1)
                ->set(hour => 0, minute => 0, second => 0);
my $end   = $start->clone()->add(days => 1, seconds => -1);

print $start,       " - ",$end,"\n";            # 2011-03-15T00:00:00 - 2011-03-15T23:59:59
print $start->epoch," - ",$end->epoch,"\n";     # 1300147200 - 1300233599


The Perl-core way would be:

my ( $y, $m, $d ) = unpack 'A4 A2 A2', $date;
my $start_ts      = POSIX::mktime( 0, 0, 0, $d,     $m - 1, $y - 1900 );
my $end_ts        = POSIX::mktime( 0, 0, 0, $d + 1, $m - 1, $y - 1900 );

see POSIX

And with mktime it's perfectly okay to just add negatives to values. So if you need to have 23:59:59 as your end date as suggested in the comments, you can just fix it up with this:

my $end_ts = POSIX::mktime( -1, 0, 0, $d + 1, $m - 1, $y - 1900 );

(Although, I would just like to note that the excluded endpoint is not an unknown case in programming.)


I did not understand your question. However, have a look at DateTime.

0

精彩评论

暂无评论...
验证码 换一张
取 消