Como obter o ano anterior e o mês anterior no solaris 8

3

Como obter o ano anterior e mês anterior no solaris 8?

Eu testei abaixo do comando, mas nenhum deles está correto:

date +'%m' -d 'last month'
date +'%Y' -d 'last year'
    
por Kumar1 04.06.2014 / 16:59

3 respostas

2

O Solaris date não suporta a opção -d como o GNU date .

Você pode usar 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

Ou se você tiver ksh93 :

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

Atualizado

Para o comentário de @glennjackman, encontrei uma documentação em Time :: Piece módulo:

   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.

Como o OP só deseja obter o ano e o mês anteriores, podemos definir $t[3] = 1 para corrigir esse problema.

    
por 04.06.2014 / 17:43
1

Um corretivo para a idéia perl (Existe um problema com essa solução no mês de janeiro, sim?).

O último mês é:

$ perl -MPOSIX=strftime -le '@t = localtime; @l = localtime (time - @t[3] * 86400); print strftime("%m", @l)'
    
por 02.02.2015 / 23:45
0

O Tcl tem uma implementação de data e hora muito boa, se você tiver instalado:

tclsh <<END
puts [clock format [clock scan "-1 month"] -format "%m"]
puts [clock format [clock scan "-1 year"] -format "%Y"]
END
    
por 05.06.2014 / 04:14