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

Time string parsing

Expand Messages
  • Shawn Hirn
    I am working with the Time::Interval CPAN module s getInterval feature and I have a question about it. I am getting results such as 5 hours, 8 minutes, 32
    Message 1 of 5 , Feb 28, 2006
      I am working with the Time::Interval CPAN module's getInterval feature and I have a
      question about it.

      I am getting results such as

      "5 hours, 8 minutes, 32 seconds" or
      "3 minutes, 32 seconds" or
      "15 secconds"

      in a variable called $x

      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 a couple of
      other
      manuals, but nothing makes sense in this context.


      Thanks
    • acummingsus
      ... feature and [ . . ] ... 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
      Message 2 of 5 , Mar 1, 2006
        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.

        --
        Alan.
      • Stan Horwitz
        ... 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
        Message 3 of 5 , Mar 1, 2006
          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?
        • Damien Carbery
          ... 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
          Message 4 of 5 , Mar 1, 2006
            --- 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";
          • acummingsus
            ... wrote: [ a solution arrives ] Super! (I was a bit confused as I didn t see any code in the post of Shawn s that I received). ... -w and use warnings ? [ .
            Message 5 of 5 , Mar 2, 2006
              --- In perl-beginner@yahoogroups.com, "Damien Carbery" <daymobrew@...>
              wrote:
              [ a solution arrives ]

              Super!

              (I was a bit confused as I didn't see any code in the post of Shawn's
              that I received).

              > #!/usr/bin/perl -w
              >
              > use Time::Interval;
              > use warnings;

              -w and use warnings ?
              [ . . ]
              > # Note the '%' before the '$' because $diff_time_seconds is a ref.
              > print "Seconds: ", convertInterval( %$diff_time_seconds,
              > 'ConvertTo'=>'seconds'), "\n";

              http://search.cpan.org/~ahicox/Time-Interval-1.0.5/Interval.pod

              <quote>
              Returned Data Format

              unless the 'String' option is specified, this routine returns a hash
              reference containing the following data:
              [data snipped]
              </quote>

              (even though no code in Shawn's post I received) I was just about to
              reply saying that it looked like he was accessing a ref to a hash
              rather than dereferencing and getting the hash.

              BTW I think the doc says the default if not specified is seconds
              (which in turn I think makes it optional in this case to have the
              ConvertTo'=>'seconds' parameter). no such parameter
              equates/reverts/uses to the default of seconds (unless I
              mixed_up/transposed something else (something entirely different) and
              it came out (of the brain I guess) as that.

              --
              Alan.
            Your message has been successfully submitted and would be delivered to recipients shortly.