Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

Title:
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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (3)
    As of 2018-07-20 09:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















      Results (427 votes). Check out past polls.

      Notices?