Loading ...
Sorry, an error occurred while loading the content.

22927[PBML] Re: Time string parsing

Expand Messages
  • Damien Carbery
    Mar 1, 2006
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, Stan Horwitz <stan@...> wrote:
      >
      >
      > On Mar 1, 2006, at 4:13 AM — 3/1/06, acummingsus wrote:
      >
      > > On Tuesday 28 February 2006 10:49, Shawn Hirn wrote:
      > > > I am working with the Time::Interval CPAN module's getInterval
      > > feature and
      > > [ . . ]
      > > > What I want is a variable called $y that's just the total seconds
      > > that's
      > > > represented by $x.
      > > >
      > > > For example, if ...
      > > >
      > > > $x = 1 hours, 2 minutes, 30 seconds" I want
      > > > $y = 3750
      > > >
      > > > So, how can I get to $y from $x? I have been reading the Perl
      > > cookbook and
      > >
      > > http://search.cpan.org/~ahicox/Time-Interval-1.0.5/Interval.pod
      > >
      > > if you want to use the same module in order to get your desired $y
      > > result:
      > >
      > > $number_of_seconds = convertInterval(
      > > hours => 1,
      > > minutes => 2,
      > > seconds => 30,
      > > ConvertTo => "seconds"
      > > );
      > >
      > > # ------------------
      > >
      > > yes, at that above url, its doc says that convertinterval will do
      > > that.
      > >
      > > Now, whether or not scalar variable can be used instead of the
      > > explicit
      > > (example: the explicit 2 for minutes), I don't know. Also you will
      > > need to
      > > parse $x in order to get each of the 3 numbers.
      > >
      > > Maybe there's another way, easier. But if scalar variable can be
      > > used, as I mentioned, then it could be done in this way.
      >
      > Thanks. For some reason, when I leave off the third argument, I get
      > "HASH(0xa8220)"
      > as a result. I also get the same thing if I use "0" as the third
      > argument.
      >
      > Sorry, I should have indicated that information in my original posting.
      >
      > Does anyone see what I am doing wrong, or is this CPAN module buggy?
      >

      I helped Shawn off-list and got a solution.

      #!/usr/bin/perl -w

      use Time::Interval;
      use warnings;
      use strict;

      my $diff_time_seconds = getInterval('02/26/06 05:23:30','02/27/06
      08:20:35');
      # Note the '%' before the '$' because $diff_time_seconds is a ref.
      print "Seconds: ", convertInterval( %$diff_time_seconds,
      'ConvertTo'=>'seconds'), "\n";
    • Show all 5 messages in this topic