鈍足ランナーのIT日記

走るのが好きな5流のITエンジニアのブログ。

趣味の範囲は広いけど、どれも中途半端なクソブロガー楽しめるWebアプリを作ってあっと言わせたい。サーバーサイドPerl(Mojolicious)、クライアントサイドVue.js。Arduinoにも触手を伸ばす予定。

perlのstaticおじさんです。。

staticおじさんなので今回つくているWebアプリでは、人生初Mooseを使ってみた。
日本の株取引がある日を求めるsubをオブジェクトを利用してみた。

package Niyo::Logic::StockMarket;

use Moose;
use Calendar::Japanese::Holiday;

sub isBusinessDay{

    my $self = shift;
    my $moment = shift;

    if (isHoliday($moment->year, $moment->month, $moment->day_of_month)){
        return 0;
    }

    if ($moment->day_of_week == 6 || $moment->day_of_week == 7 ){
        return 0;        
    }

    if ($moment->month == 12 && $moment->day_of_month == 31){
        return 0;
    }

    if ($moment->month == 1 && (
      $moment->day_of_month == 1 || $moment->day_of_month == 2 || $moment->day_of_month == 3)){
        return 0;
    }

    return 1;

}

1;
package Niyo::Logic::StockMarket;

use Moose;
use Calendar::Japanese::Holiday;
use Time::Moment;

has 'time_moment' => (is => 'rw', isa => 'Time::Moment');
has 'next_business_day' => (is => 'rw', isa => 'Time::Moment');

sub BUILD{
    my $self = shift;

    my $next_business_day = $self->time_moment->plus_days(1);
    while(!_isBusinessDay($next_business_day)){
        $next_business_day = $next_business_day->plus_days(1);
    }
    $self->next_business_day($next_business_day);
}

sub isBusinessDay{
    my $self = shift;
    return _isBusinessDay($self->time_moment);
}

sub _isBusinessDay{

    my $moment = shift;

    if (isHoliday($moment->year, $moment->month, $moment->day_of_month)){
        return 0;
    }

    if ($moment->day_of_week == 6 || $moment->day_of_week == 7 ){
        return 0;        
    }

    if ($moment->month == 12 && $moment->day_of_month == 31){
        return 0;
    }

    if ($moment->month == 1 && (
      $moment->day_of_month == 2 || $moment->day_of_month == 3)){
        return 0;
    }

    return 1;

}

1;