File Coverage

blib/lib/Finance/GeniusTrader/DateTime/Week.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package Finance::GeniusTrader::DateTime::Week;
2              
3             # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
4             # This file is distributed under the terms of the General Public License
5             # version 2 or (at your option) any later version.
6              
7 1     1   6 use strict;
  1         2  
  1         29  
8 1     1   5 use vars qw();
  1         2  
  1         16  
9              
10 1     1   39 use Finance::GeniusTrader::DateTime;
  1         3  
  1         172  
11 1     1   686 use Date::Calc qw(Week_of_Year Monday_of_Week);
  0            
  0            
12             #ALL# use Log::Log4perl qw(:easy);
13             use Time::Local;
14              
15             =head1 Finance::GeniusTrader::DateTime::Week
16              
17             This module treat dates describing a week. They have the following format :
18             YYYY-WW
19              
20             WW being the week number.
21              
22             =cut
23             sub map_date_to_time {
24             my ($date) = @_;
25             my ($y, $w) = split /-/, $date;
26             my ($year, $month, $day) = Monday_of_Week($w, $y);
27             return timelocal(0, 0, 0, $day, $month - 1, $year - 1900);
28             }
29              
30             sub map_time_to_date {
31             my ($time) = @_;
32             my ($sec, $min, $hour, $d, $m, $y, $wd, $yd) = localtime($time);
33             #DEB# DEBUG "$time => $y-$m-$d";
34             my ($week, $year) = Week_of_Year($y + 1900, $m + 1, $d);
35             return sprintf("%04d-%02d", $year, $week);
36             }
37              
38             sub timeframe_ratio {
39             my ($tf) = @_;
40              
41             #WAR# WARN "timeframe must be smaller than a week" unless ($tf < $WEEK);
42              
43             $tf == $DAY && return 5;
44             return Finance::GeniusTrader::DateTime::timeframe_ratio($DAY, $tf) * 5;
45             }
46              
47             1;