Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: Display tide based on time now.

by Krambambuli (Curate)
on Mar 28, 2013 at 10:06 UTC ( #1025921=note: print w/replies, xml ) Need Help??

in reply to Display tide based on time now.

Hi, with the following code replacing your main code,
my @tides = ( { time_in_seconds => 0, high_low => 'Unknown', time => 'Unknown', } ); foreach my $tides (@{$data->{data}->{item}}) { if($tides->{date} eq $today_date) { $tides->{highlow} =~ s/L/Low/; $tides->{highlow} =~ s/H/High/; my $format_date = "$tides->{date} $tides->{time}"; my $date_to_seconds = str2time($format_date); #print " Test: ".$tides->{highlow}, " Tide at: ", $tides->{time}, + "\n"; $tides->{time_in_seconds} = $date_to_seconds; push @tides, $tides; } } push @tides, { time_in_seconds => $now_in_seconds + 10000000, highlow => 'Unknown', time => 'Unknown', }; my @future_tides = grep { $_->{time_in_seconds} >= $now_in_seconds } @ +tides; my @past_tides = grep { $_->{time_in_seconds} < $now_in_seconds } @ +tides; my $next_tide = $future_tides[0]; my $previous_tide = $past_tides[ -1]; print "Next tide: ", $next_tide->{highlow}, " Tide at: ", $next_tide-> +{time}, "\n"; print "iPrevious tide: ", $previous_tide->{highlow}, " Tide at: ", $pr +evious_tide->{time}, "\n"; exit;
I get

2013/03/27, 06:05 AM - Now in Seconds is: 1364378700 Next tide: Low Tide at: 06:20 AM Previous tide: High Tide at: 12:21 AM
which might be close to what you wish. UPDATE Updated code to display something whatever the 'now' time is.

Replies are listed 'Best First'.
Re^2: Display tide based on time now.
by Anonymous Monk on Mar 28, 2013 at 13:07 UTC
    Yes it is close, but this is how your code stands:

    First tide time on 2013/03/27 - 12:21 AM - The current time has to be 12:22 AM to work.
    Last tide time on 2013/03/27 - 06:39 PM - The current time can not be over that.

    It only works OK if its one minute after the time of the first tide time and not over the time of the last tide time.

    Thanks for trying!
      Check the updated code, if you wish. It should display something for whatever 'now' time.
        That's great, I just corrected I little thing here:
        my @tides = ( { time_in_seconds => 0, highlow => 'Unknown', # it was high_low time => 'Unknown', } );
        But thanks for your help. I have to do more work on that to check the dates for the next available tide and avoid printing 'Unknown'. This XML has the dates for the whole year, but this pointed me in the right direction for sure!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1025921]
[M4ver1k]: I feel like being able to toggle a running script on the fly by updating variables from a config file using Config::Simple. But in the while loop, it seems like it doesn't update the variable after the first past. Is this generally just a bad idea?
[M4ver1k]: pass*

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2017-11-20 03:04 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (283 votes). Check out past polls.