in reply to Extract data between certain dates using YYYYMMDDHH timestamp

Since your dates are in YYYYMMDDHH format you can use string comparisons to decide whether the datum is in the correct range. This script decrements the current epoch time value by the relevant number of seconds needed to go back to the Friday of the previous week and then another four days to get to the Monday. It then constructs start and end timestamp strings for hour 0 of the Monday and hour 23 of the Friday.

use strict; use warnings; my %daysOffsetTV = ( 0 => 86400 * 2, 1 => 86400 * 3, 2 => 86400 * 4, 3 => 86400 * 5, 4 => 86400 * 6, 5 => 86400 * 7, 6 => 86400, ); my $nowTV = time(); my $dayOfWk = ( localtime( $nowTV ) )[ 6 ]; my $prevWkFriTV = $nowTV - $daysOffsetTV{ $dayOfWk }; my( $friDay, $friMth, $friYr ) = ( localtime( $prevWkFriTV ) )[ 3 .. 5 ]; $friMth += 1; $friYr += 1900; my $endDate = sprintf q{%04d%02d%02d%02d}, $friYr, $friMth, $friDay, 23; my $prevWkMonTV = $prevWkFriTV - 86400 * 4; my( $monDay, $monMth, $monYr ) = ( localtime( $prevWkMonTV ) )[ 3 .. 5 ]; $monMth += 1; $monYr += 1900; my $startDate = sprintf q{%04d%02d%02d%02d}, $monYr, $monMth, $monDay, 0; print qq{Starting date: $startDate\n}, qq{ Ending date: $endDate\n\n}; while( <DATA> ) { my $timestamp = ( split m{,} )[ 0 ]; print unless $timestamp lt $startDate or $timestamp gt $endDate; } __END__ 2009030822,3558,1.68 2009030823,6385,5.47 2009030900,7485,1.82 2009030901,8563,4.35 2009031322,8860,3.68 2009031323,39224,14.16 2009031400,34553,7.34 2009031401,7353,5.74

The output.

Starting date: 2009030900 Ending date: 2009031323 2009030900,7485,1.82 2009030901,8563,4.35 2009031322,8860,3.68 2009031323,39224,14.16

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Extract data between certain dates using YYYYMMDDHH timestamp
by ikegami (Patriarch) on Mar 20, 2009 at 16:46 UTC
    Not all days have 86400 seconds. Looks like it can fail near a daylight savings time change (by getting Su-Th or Tu-Sa instead of M-F, I think).