I have the date range (two UNIX timestamps) and I need to find point(s开发者_JAVA百科) at 23:59:59 if exists. How I can do it with Perl?
P.S. I think for() is not good idea because I can have very big range. Anything another variants?
I would use Date::Calc
. Suppose your two timestamps are located in $ts1
and $ts2
:
use Date::Calc qw(Time_to_Date Date_to_Time Delta_Days Add_Delta_Days);
my @date1 = (Time_to_Date($ts1))[0..2];
my @date2 = (Time_to_Date($ts2))[0..2];
my @midnights;
for (my $i = 0; $i < Delta_Days(@date1, @date2); ++$i) {
push @midnights, Date_to_Time(Add_Delta_Days(@date1, $i), 23, 59, 59);
}
@midnights
now contains the UNIX timestamps (epoch seconds) of all 23:59:59 point(s) between the two given timestamps.
Disclaimer: Of course you could also do it with DateTime
.
That's a generic algorithm. X is the 1st timestamp Y is the last one
1) Get the first change date, Z.
If Z > Y , there is no change.
2) Get the last change date, W
If W = Z, there is only one change date.
3) Get the range of dates, R. Considering the range of one day is D
R = W - Z .. The number of points will be the integer of (W - Z)/D
use DateTime;
Here's an example I scrapped together based on my semi-recent knowledge of the UNIX timestamp (school is such a long time ago...)
while (entry) {
if ((timestamp-1) % 86400 == 0) {
#match
} else {
#nomatch
}
}
However, like everybody else, I would suggest using other tools to do that since my example does not take leap seconds into account and I highly doubt it's POSIX-compliant. More info here.
精彩评论