in reply to Split date ranges/chunks based on Unix epoch time?

Since you only want hour granularity just divide the epoch time by the number of seconds in an hour:
my %date_count; foreach my $time ( @times ) { $date_count{ ( $time / 3600 ) * 3600 }++; }
Then later you can break it down into years/months/days/hours.

Replies are listed 'Best First'.
Re^2: Split date ranges/chunks based on Unix epoch time?
by Krambambuli (Curate) on Apr 26, 2007 at 06:47 UTC
    Take care, that should then be
    my %date_count; foreach my $time ( @times ) { $date_count{ int( $time / 3600 ) * 3600 }++; }
    The division operator '/' doesn't do any implicit integer truncation - yes, I know C does... :)
      Yes, thank you, I forgot about that.    :-)

        Unfortunately, it's still broken. It doesn't account for timezones whose difference from UTC is not a multiple of 60 minutes. For example, you code doesn't work in Newfoundland.

        use Time::Local qw( timelocal ); $ENV{TZ} = 'America/St_Johns'; for my $time ( timelocal(0, 25, 16, 26, 4-1, 2007), # 16th hour of 2007-04-26 timelocal(0, 35, 16, 26, 4-1, 2007), # 16th hour of 2007-04-26 ) { my $hour = int( $time / 3600 ) * 3600; print(scalar(localtime($time)), "\n"); # Should print the same value for both, but doesn't. print("$hour\n"); }
        Thu Apr 26 16:25:00 2007 1177610400 Thu Apr 26 16:35:00 2007 1177614000