Perl practice

 

#!/usr/bin/perl

 

use strict;

use warnings;

use Time::Local;

 

sub gen_holiday_base

{

    my $ayear = shift;

    my $ybegin = "$ayear-01-01";

    my $yend = "${ayear}1231";

    my $step = 0;

    my $date = "";

    my $weekday = "";

    my %datemap;

 

    while ( "$date" ne "$yend" ) {

       ($date, $weekday) = &date_calculate($ybegin, $step++);

       $datemap{$date} = ($weekday==0||$weekday==6)? "Y":"N";

    }

 

    %datemap;

}

 

sub date_calculate

{

    my ($date,$change) = @_;

    my ($y,$m,$d) = split('-',$date);

    my $time = timelocal(0,0,0,$d,$m-1,$y);

    $time += $change*24*60*60;

    my ($day,$mon,$year,$weekday) = (localtime($time))[3,4,5,6];

    $year += 1900;

    $mon += 1;

    (sprintf("%04d%02d%02d", $year,$mon,$day),$weekday);

}

 

 

 

sub patch_status

{

    #my $ayear = shift;

    #my $calendar_file = shift;

    my ($ayear, $calendar_file) = @_;

    my @holidays;

 

    open(FH, "<$calendar_file") or die "Can't open the Calendar file ($!)";

    while (my $line = <FH>) {

        push @holidays, $1 if $line =~ /($ayear\d+)\s*,\s*\"HSBC\"/;

    }

    close(FH);

    @holidays;

}

 

sub parse_mapping

{

    my ($line, $desired_year, $holiday_base, $stra_hash) = @_;

    my ($ctmcal, $strategy) = split(/\s+/, $line);

    # a indicator to show that the ctm calendar

    # are belong to the same site

    my $acronym = $1 if $ctmcal =~ /NFO(\w{2})/;

    my $boscal_file = &get_boscal_file($acronym);

    my $routinerel = $stra_hash->{$strategy};

    $routinerel->($desired_year, $holiday_base, $boscal_file);

}

 

sub get_boscal_file

{

    my $site = shift;

    my $dir = "./cal/boscal";

    my @files = <$dir/*.HLDYUNZIPPED>;

    for my $s (@files) {

        if ( $s =~ /$site\w+\d{8}/) {

            return $s;

        }

    }

    $site;

}

 

 

sub check_param

{

    my $year = shift;

    die "Usage:$0 YEAR <eg: $0 2012>" if (!defined $year);

    die "Usage:$0 YEAR <eg: $0 2012>" if ($year !~ /\d{4}/);

    $year;

}

 

sub stra_reva

{

    my ($year, $holiday_base, $calfile) = @_;

    my @holidays = &patch_status($year, $calfile);

    my $holiday_str = "";

    my %updated_hol = $$holiday_base;

 

    $updated_hol{$_}="Y" foreach (@holidays);

    foreach my $key (sort keys %updated_hol) {

        my $value = $updated_hol{$key};

        $value = ($value eq "Y")? "N":"Y";

        $holiday_str = $holiday_str . "$value";

    }

    $holiday_str;

}

 

sub stra_alls

{

    my ($year, $holiday_base, $calfile) = @_;

    my $holiday_str = "";

    my %updated_hol = $$holiday_base;

 

    foreach my $key (keys %updated_hol) {

        my $value = "Y";

        $holiday_str = $holiday_str . "$value";

    }

    $holiday_str;

}

 

 

sub stra_base

{

    my ($year, $holiday_base, $calfile) = @_;

    my @holidays = &patch_status($year, $calfile);

    my $holiday_str = "";

    my %updated_hol = $$holiday_base;

 

    $updated_hol{$_}="Y" foreach (@holidays);

    foreach my $key (sort keys %updated_hol) {

        my $value = $updated_hol{$key};

        $holiday_str = $holiday_str . "$value";

    }

    $holiday_str;

}

 

sub stra_forw

{

    my ($year, $holiday_base, $calfile) = @_;

    my @holidays = &patch_status($year, $calfile);

    my $holiday_str = "";

    my %updated_hol = $$holiday_base;

    my @thisyearstr;

 

    $updated_hol{$_}="Y" foreach (@holidays);

    foreach my $key (sort keys %updated_hol) {

        my $value = $updated_hol{$key};

        push(@thisyearstr, $value);

    }

    shift(@thisyearstr);

 

    my @holidays_ahead = &patch_status($year+1, $calfile);

    my %holiday_base_ahead = &gen_holiday_base($year+1);

    $holiday_base_ahead{$_}="Y" foreach(@holidays_ahead);

    foreach my $keya (sort keys %holiday_base_ahead) {

        my $valuea = $holiday_base_ahead{$keya};

        push(@thisyearstr, ("Y" eq $valuea)? "N":"Y");

        exit;

    }

    $holiday_str = scalar @thisyearstr;

    $holiday_str;

}

 

 


sub init_subroutine

{

    my %stra_hash;

    $stra_hash{"BASE"} = \&stra_base;

    $stra_hash{"REVA"} = \&stra_reva;

    $stra_hash{"FORW"} = \&stra_forw;

    $stra_hash{"ALLS"} = \&stra_alls;

    %stra_hash;

}


sub main

{

    # step 0: get the deired calendar year from input

    #         have to be careful with the param

    my $desired_year = &check_param($_[0]);


    # step 1: init the strategy hash

    my %stra_hash = &init_subroutine;


    # step 2: now we need generate our base holiday

    #         sequence

    # replace this one with

    # my %holiday_base = &gen_holiday_base($desired_year);

    my %holiday_base = &gen_holiday_base($desired_year);


    # step 3: read config file from current directory

    #         extablish a mapping

    #         has to be orderred.

    open(CONFIG, "<CALMAPPING") or die "Can not locate the config file ($!)";

    while (my $line = <CONFIG>) {

        next if $line =~ /^\s*#|^\s*$/;

        &parse_mapping($line, $desired_year, \%holiday_base, \%stra_hash);

    }

 

 

 

    #my %holiday_base = &gen_holiday_base(2012);

    #my @holidays = &patch_status(2012, "SEL20111123.HLDYUNZIPPED");

    #my $holiday_str = "";

 

    #$holiday_base{$_}="Y" foreach (@holidays);

    #foreach my $key (sort keys %holiday_base) {

    #   my $value = $holiday_base{$key};

    #   #print "$key => $value\n";

    #   $holiday_str = $holiday_str . "$value";

    #}

 

    #print "$holiday_str\n";

}

 

sub main_test

{

    my $file = &get_boscal_file("HK");

    print "$file \n";

}

 

main(@ARGV);

 

 

# directory structure

 

 

[xxx@server calendar]$ ls -R

.:

cal  CALMAPPING  ctm  gencal.pl  nfo.cal.NFOSEBUS.old.20111125.xml  SEL20111123.HLDYUNZIPPED

 

./cal:

boscal

 

./cal/boscal:

HKH20111123.HLDYUNZIPPED  SEL20111123.HLDYUNZIPPED  TKY20111123.HLDYUNZIPPED

 

./ctm:

export

 

./ctm/export:

nfo.cal.NFOSEBUS.old.20111125.xml

 

 

 

[xxx@server calendar]$ cat CALMAPPING

# ctmcal    strategy

NFOSEBUS        BASE

NFOSENOBUS      REVA

NFOSEBST        FORW

NFOSEBSP        BASE

NFOSEHOL        ALLS

NFOHKBUS        ALLS



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值