솔라리스 8에서 전년도와 지난달을 얻는 방법

솔라리스 8에서 전년도와 지난달을 얻는 방법

솔라리스 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이 문제를 해결하도록 설정할 수 있습니다.

답변2

Perl 아이디어 수정(1월의 솔루션은 버그가 있었습니다, 그렇죠?).

지난 달은 다음과 같습니다.

$ perl -MPOSIX=strftime -le '@t = localtime; @l = localtime (time - @t[3] * 86400); print strftime("%m", @l)'

답변3

Tcl을 설치했다면 날짜와 시간 구현이 매우 훌륭합니다.

tclsh <<END
puts [clock format [clock scan "-1 month"] -format "%m"]
puts [clock format [clock scan "-1 year"] -format "%Y"]
END

관련 정보