To improve Calculate Number of Days Command

前端 未结 2 1737
滥情空心
滥情空心 2021-01-27 16:02

Would like to generate report, which calculate the number of days, the material is in the warehouse. The number of days is the difference between date ($3 field) t

相关标签:
2条回答
  • 2021-01-27 16:09

    Here is an example in Perl:

    use feature qw(say);
    use strict;
    use warnings;
    
    use Text::CSV;
    use Time::Piece;
    
    my $csv = Text::CSV->new;
    my $te = Time::Piece->strptime('01-OCT-14', '%d-%b-%y');
    my $fn = 'Input.csv';
    open (my $fh, '<', $fn) or die "Could not open file '$fn': $!\n";
    chomp(my $head = <$fh>);
    say "$head,Ageing-NoOfDays";
    while (my $line = <$fh>) {
        chomp $line;
        if ($csv->parse($line)) {
            my $t = ($csv->fields())[2];
            my $tp = Time::Piece->strptime($t, '%d-%b-%y.%T');
            my $s = $te - $tp;
            say "$line," . $s->days;
        } else {
            warn "Line could not be parsed: $line\n";
        }
    }
    close($fh);
    
    0 讨论(0)
  • 2021-01-27 16:24

    Since you are on cygwin you are using GNU awk which has it's own built-in time functions and so you do not need to be trying to use the shell date command. Just tweak this old command I had lying around to suit your input and output format:

    function cvttime(t,     a) {
            split(t,a,"[/:]")
            match("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])
            a[2] = sprintf("%02d",(RSTART+2)/3)
            return( mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]) )
    }
    BEGIN{
    t1="01/Dec/2005:00:04:42"
    t2="01/Dec/2005:17:14:12"
    print cvttime(t2) - cvttime(t1)
    }
    

    It uses GNU awk for time functions, see http://www.gnu.org/software/gawk/manual/gawk.html#Time-Functions

    0 讨论(0)
提交回复
热议问题