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!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|