http://www.perlmonks.org?node_id=601208


in reply to Sorting Puzzle

The numbers are too large for numeric comparison without using bigint

Replies are listed 'Best First'.
Re^2: Sorting Puzzle
by willfould (Novice) on Feb 20, 2007 at 21:19 UTC
    Thank you! I am creating a numeric sorting key on start/end dates and times. I was originally wanting to convert the times to unix timestamp but could not find a fast way to do that. With the unix timestamp, this would be trivial but require the extra conversion step. Is there a datetime to unix timestamp function?
      Datetime. It also provides sorting. It also provides intervals which sounds like what you're interested in.

      Less applicable to the question but more important, imho, is that it handles all the nasty edgecases that any developer who didn't write Datetime shouldn't be bothered to know about handling dates and times. This would include the situations when a minute can have 59 seconds, 61 seconds, and even 62 seconds. (Yes, those situations do exist in really odd circumstances, but it would suck if you accidentally ran into one of them, right?)


      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Time::Piece makes it fairly easy, but you would have to benchmark it against other solutions to see what works for you. How much data are you dealing with?
      use Time::Piece; use strict; use warnings; my $date = '20070301103000'; my $start = Time::Piece->strptime($date, "%Y%m%d%H%M%S")->epoch; print $start, "\n";