最近的工作環境 測試機SUSE linux 正式機 AIX unix ,
由於常常寫SHELL會需要日期運算,
之前公司所用的日期運算是用c寫的,常換平台後需要再編譯一次
因為現在linux平台大多內建perl所以就寫了一個日期運算的程式
不須編譯,只須把權限改成可執行就可以跑了
由於常常寫SHELL會需要日期運算,
之前公司所用的日期運算是用c寫的,常換平台後需要再編譯一次
因為現在linux平台大多內建perl所以就寫了一個日期運算的程式
不須編譯,只須把權限改成可執行就可以跑了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!/usr/bin/perl use Time::Local; foreach $item (@ARGV) { $InputStr=$InputStr.$item; } if ($InputStr !~ /^[0-9]{8}\+|\-[0-9]+/) { print "Format Error:\n"; print "ex. DateCompare 20020202+1\n"; print "or DateCompare 20020202-1\n"; } else { $Year=substr($InputStr, 0, 4); $Mon=substr($InputStr, 4, 2); $Day=substr($InputStr, 6, 2); $Mon =~ s/^0//; $Day =~ s/^0//; $InputStr =~ m/\+|\-/; $Compare=$&; $CompareDay=$'; $TimeLocal=timelocal(0,0,0,$Day,$Mon-1,$Year); if ($Compare =~ /\+/) { ($sec,$min,$hour,$day,$mon,$year) = localtime($TimeLocal + (86400 * $CompareDay)); } else { ($sec,$min,$hour,$day,$mon,$year) = localtime($TimeLocal - (86400 * $CompareDay)); } printf("%04d%02d%02d\n",$year + 1900,$mon + 1,$day); } |
留言