Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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]
[tye]: -1 means nobody logged in or the process was started before audit got booted
[davido]: ok, on my ubuntu system getlogin grabs from /proc/self/ loginuid (per strace)
[tye]: disable /proc and then see what it does?
[davido]: then it reads from /etc/passwd to decide who my uid is.
[davido]: sorry, typed that before you asked me to disable proc
[davido]: but you stumped me; don't know how to disable proc.
[tye]: I don't know if you can just dismount, but I thought so.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2017-06-23 20:00 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.