Regarding question for GNU date

Hello All,

Greetings all !!

I have a query here, following are the points on same(Adding today's is 31st August 2016 for future reference).

1st Scenario: So while doing some work on GNU date , I wanted to check what was the month(in numbers) by GNU date so I have done following.

date -d"-2 months" +%m%d
 

Above gives me output as follows.

0701

2nd Scenario: When I check this in terms of days then following is the result.

date -d"-62 days" +%m%d
0630
AND
date -d"-61 days" +%m%d
0701

So after executing above scenarios I felt like if as a user I do -2 months with GNU date then it should ideally provide me month June , because when I do date -d"-3 months" +%m%d it shows me 0531 .

So what is my observation is even we do -month option with GNU date in backend it will count by days only, if I am right here, so isn't it something kind of bug etc or it is expected behavior.

Will be grateful to all for your suggestions and advices here.

NOTE: testing it in BASH and version is date (GNU coreutils) 8.4 .

Thanks,
R. Singh

There's a lot of corner cases. We had Feb 29th this year for example; Feb 29th minus 1 year gives you march 1st. Replicating GNU date's most obvious features was (slightly) easier than I thought, as it did pretty much what I did -- calculate the difference without trying to be too intelligent about it. It's intelligent enough to subtract the month number rather than a time offset of some sort, but doesn't correct the number of days after.

It's difficult to guess the user's intention for date math, so it's hard to call this a bug exactly. Rather it's just our convoluted, irrational calendar system and not-a-nice-round-number orbital period coming back to bite us once again.

If you wanted it to snap to months in that manner, try feeding it the first of the month and subtracting that, that should always work.*

date -d "$(date +%Y-%m-01) - 2 months"

*within timescales of 30 years. Ugh, dates are weird.

1 Like

I don't have access to a GNU date, but with ksh93 printf date formatting, the following seems to work (all test cases run on August 31, 2016):

printf '%(%B)T\n' "next month"
September
printf '%(%B)T\n' "2 months" 
October
printf '%(%B)T\n' "3 months"
November
printf '%(%B)T\n' "last month"
July
printf '%(%B)T\n' "2 months ago"
June
printf '%(%B)T\n' "3 months ago"
May
printf '%(%B)T\n' "now"
August
printf '%(%B)T\n'
August
1 Like