Solaris 8で前年と先月を取得する方法

Solaris 8で前年と先月を取得する方法

Solaris 8で前年と先月を取得する方法は?

次のコマンドを試しましたが、どちらも正確ではありません。

date +'%m' -d 'last month'
date +'%Y' -d 'last year'

ベストアンサー1

SolarisはGNUなどのオプションをdateサポートしていません。-ddate

あなたはそれを使用することができますperl

$ perl -MPOSIX=strftime -le '@t = localtime; $t[3] = 1; $t[4]--; print strftime("%m", @t)'
05    
$ perl -MPOSIX=strftime -le '@t = localtime; $t[3] = 1; $t[5]--; print strftime("%Y", @t)'
2013

または、次の場合ksh93

$ printf "%(%m)T\n" "last month"
05  
$ printf "%(%Y)T\n" "last year"
2013

修正する

@glennjackmanのコメントについてドキュメントを見つけました。時間::彫刻基準寸法:

   The months and years can be negative for subtractions. Note that there is some "strange" behaviour when adding and subtracting months
   at the ends of months. Generally when the resulting month is shorter than the starting month then the number of overlap days is
   added. For example subtracting a month from 2008-03-31 will not result in 2008-02-31 as this is an impossible date. Instead you will
   get 2008-03-02. This appears to be consistent with other date manipulation tools.

OPは前年と月だけをインポートしたいので、$t[3] = 1この問題を解決するように設定できます。

おすすめ記事