From 0370a468d9ff22b47e6eabcb995f68ee997793de Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 23 May 2013 23:25:05 +0100 Subject: DataBlock slices, better spans and stripes --- forecast.html.tt | 36 +++++++++++++++++++++------------- lib/WebService/ForecastIo/DataBlock.pm | 23 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/forecast.html.tt b/forecast.html.tt index 57edfb7..d45e2db 100644 --- a/forecast.html.tt +++ b/forecast.html.tt @@ -1,7 +1,15 @@ [% cur = f.currently; today = f.daily.data.0; - MACRO hours_length BLOCK; - span.stop_time.subtract_datetime(span.start_time).in_units('hours'); + today_start = f.hourly.data.0.time.clone + today_stop = f.hourly.data.0.time.clone.add('days'=>1); + hourly = f.hourly.slice('from', today_start, 'to', today_stop); + MACRO hours_length(span) BLOCK; + d=span.stop_time.subtract_datetime(span.start_time); + v=d.in_units('days','hours'); + v.0 * 24 + v.1; + END; + MACRO time(d) BLOCK; + d.clone.set_time_zone('Europe/London').strftime('%H:%M'); END; -%] @@ -58,10 +66,10 @@

Next 24 hours

- [% f.hourly.icon %] + [% hourly.icon %]
Summary:
-
[% f.hourly.summary %]
+
[% hourly.summary %]
@@ -79,28 +87,28 @@
Summary:
[% today.summary %]
Min temperature:
-
[% today.temperatureMin %]° [% today.temperatureMinTime.strftime('%H:%M') %]
+
[% today.temperatureMin %]° [% time(today.temperatureMinTime) %]
Max temperature:
-
[% today.temperatureMax %]° [% today.temperatureMaxTime.strftime('%H:%M') %]
+
[% today.temperatureMax %]° [% time(today.temperatureMaxTime) %]
Sunrise:
-
[% today.sunriseTime.strftime('%H:%M') %]
+
[% time(today.sunriseTime) %]
Sunset:
-
[% today.sunsetTime.strftime('%H:%M') %]
+
[% time(today.sunsetTime) %]
- [% left=0 ;FOR span IN f.hourly.spans_by_string('summary'); - width=3*hours_length(span=span) %] - [% span.value %] [% span.start_time %] [% span.stop_time %] + [% left=0 ;FOR span IN hourly.spans_by_string('summary'); + width=3*hours_length(span) %] + [% span.value %] [% left=left+width; END %]
- [% left=0; FOR span IN [ 0 .. 23 ] %] - [% span %] + [% left=0; FOR h IN hourly.data; BREAK IF loop.last %] + [% time(h.time) %] [% left=left+3; END %]
- [% left=0; FOR h IN f.hourly.data %] + [% left=0; FOR h IN hourly.data; BREAK IF loop.last %] [% h.temperature %]° [% left=left+3; END %]
diff --git a/lib/WebService/ForecastIo/DataBlock.pm b/lib/WebService/ForecastIo/DataBlock.pm index 42b3d0e..c01e1cf 100644 --- a/lib/WebService/ForecastIo/DataBlock.pm +++ b/lib/WebService/ForecastIo/DataBlock.pm @@ -42,6 +42,26 @@ sub spans_by_number { ); } +sub slice { + my ($self,%opts) = @_; + + my $from = $opts{from} // 0-'Inf'; + my $to = $opts{to} // 0+'Inf'; + + warn "slicing $from $to\n"; + + return ref($self)->new({ + summary => $self->summary, + icon => $self->icon, + data => [ + grep { + warn "looking at ".$_->time."\n"; + $_->time >= $from && $_->time <= $to } + $self->data_points + ], + }); +} + sub _spans { my ($self,$field,$comp) = @_; @@ -68,6 +88,9 @@ sub _spans { } } } + if (@$out) { + $out->[-1]->_set_stop_time($self->data->[-1]->time); + } return $out; } -- cgit v1.2.3