Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math::Round/Date::Calc rounding question 1

Status
Not open for further replies.

travs69

MIS
Dec 21, 2006
1,431
0
0
US
I am trying to round epoch timestamps to 5 min intervals. I was looking into Math::Round (nearest_floor) and then converting it back to real timestamps, or converting them to timestamps, zeroing out the seconds, and then just manually converting them (if min >= 0 and <5 set it to 00, >=5 and <10 set it to 05 and so on).



Has anyone already done something like this cause it feels like I am over complicating it.

Thanks in advance

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Epoch time is in seconds since Jan 1 1970. So you could just do something like
Code:
my $fiveMinuteSlot = int(($epochSeconds + 150)/300) * 300;
to round it to the nearest five minutes.

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I was reluctant to suggest such a simple solution because I didn't know how Unix Time handled Leap Seconds. However, after reading the above, I find that it doesn't represent them, so that solution would work just fine.

- Miller
 
Thanks Steve! I actually don't want to round but only round down.

I need min's 0-4 to come out to 0, 5-9 to come out to 5, 10-14, to 10 etc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
In that case, just leave out the + 150.

Miller - apparently strict posix-compliant do handle leap seconds, but we haven't lost any since 2006. I apologise for the crudity of the solution, however...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Steve,

My concern was an overreaction. It actually doesn't matter that Unixtime doesn't represent leap seconds. That would only matter if you were trying to find the exact difference in two times that happened to span a leap second boundary.

My reluctance was simply seeded in my tendency to avoid dealing with datetimes in raw format. It can be quite a mess, especially when dealing with things like DST. So I try to let modules do all the hardwork (and easy work) whenever possible.

In this case, your solution works just fine though.

- Miller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top