Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi Monks!
I am trying to display the current tide based on the sample XML I have attached. Based on now's time changed to seconds against the times changed to seconds from the XML to show current tide, but I am missing something on the logic to show the current tide based on the time now. I hope I made it clear, here is the code I am using:
#!/usr/bin/perl use strict; use warnings; use XML::Simple; use Data::Dumper; use Time::Zone; use Date::Format; use Date::Parse; my ($today_datex, $today_time) = get_timezone(); my $today_date = "2013/03/27"; # using this to match XML attached belo +w my $now_in_seconds = str2time("$today_date $today_time"); print "\n $today_date, $today_time - Now in Seconds is: $now_in_second +s\n"; # 1364345520 # create object my $xml = new XML::Simple (KeyAttr=>[]); # read XML file my $data = $xml->XMLin("tides.xml"); my @time_in_seconds; 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"; push @time_in_seconds, $date_to_seconds; } } my $t1b = $time_in_seconds[0]; my $t2b = $time_in_seconds[1]; my $t3b = $time_in_seconds[2]; my $t4b = $time_in_seconds[3]; if( ($now_in_seconds >= $t1b) && ( $now_in_seconds < $t2b ) ) { print "\n Option 1\n"; }elsif( ($now_in_seconds >= $t2b) && ( $now_in_seconds < $t3b ) ) { print "\n Option 2\n"; }elsif( ($now_in_seconds >= $t3b ) && ( $now_in_seconds < $t4b ) ) { print "\n Option 3\n"; }elsif( $now_in_seconds >= $t4b ) { print "\n Option 4\n"; }else{ print "\n We have a problem!\n\n"; } sub get_timezone { $ENV{'TZ'} = 'America/New_York'; my $today_date = time2str( "%Y/%m/%d", time); # "%m-%d-%Y" my $today_time = time2str( "%I:%M %p", time); #"%H:%M:%S" return $today_date, $today_time; } =code <?xml version="1.0" encoding="ISO-8859-1" ?> <datainfo> <data> <item> <date>2013/03/27</date> <day>Wed</day> <time>12:21 AM</time> <predictions_in_ft>4.2</predictions_in_ft> <predictions_in_cm>128</predictions_in_cm> <highlow>H</highlow> </item> <item> <date>2013/03/27</date> <day>Wed</day> <time>06:20 AM</time> <predictions_in_ft>0.0</predictions_in_ft> <predictions_in_cm>0</predictions_in_cm> <highlow>L</highlow> </item> <item> <date>2013/03/27</date> <day>Wed</day> <time>12:50 PM</time> <predictions_in_ft>4.1</predictions_in_ft> <predictions_in_cm>125</predictions_in_cm> <highlow>H</highlow> </item> <item> <date>2013/03/27</date> <day>Wed</day> <time>06:39 PM</time> <predictions_in_ft>0.2</predictions_in_ft> <predictions_in_cm>6</predictions_in_cm> <highlow>L</highlow> </item> </data> </datainfo> =cut
Thanks for looking!

In reply to Display tide based on time now. by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (3)
    As of 2018-12-16 14:39 GMT
    Find Nodes?
      Voting Booth?
      How many stories does it take before you've heard them all?

      Results (71 votes). Check out past polls.

      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!