跳到主要內容

Perl 日期運算

最近的工作環境 測試機SUSE linux 正式機 AIX unix ,
由於常常寫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);
}

留言

Ryan寫道…
剛好有用到,謝嚕

這個網誌中的熱門文章

自訂GroupBox外框

其實微軟的GroupBox是沒辦法設定外框和字型的顏色的,所以我們就來自己加 參考 http://social.msdn.microsoft.com/forums/en-US/winforms/thread/cfd34dd1-b6e5-4b56-9901-0dc3d2ca5788/ 寫了一個GroupBox控制項 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 public class myGroupBox :GroupBox { private Color _BorderColor = Color.Red; [Description("設定或取得外框顏色")] public Color BorderColor { get { return _BorderColor;} set { _BorderColor = value ; } } protected override void OnPaint (PaintEventArgs e) { //取得text字型大小 Size FontSize = TextRenderer.MeasureText( this .Text, this .Font); //畫框線 Rectangle rec = new Rectangle(e.ClipRectangle.Y, this .Font.Height / 2 , e.C lipRectangle.Width - 1 , e.ClipRectangle.Height - 1 - this .Font.Height / 2 ); e.Graphics.DrawRectangle( new Pen(BorderColor), rec); //填滿text的背景 e.Graphics.FillRectangle( new SolidBrush( this .BackColor), new Rectangle( 6 , 0 , FontSize.Width , FontSize.Height)); //text e.Graphics.DrawString( this .Text, th...