Regarding question for GNU date


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Regarding question for GNU date
# 1  
Old 08-31-2016
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.
Code:
date -d"-2 months" +%m%d

Above gives me output as follows.
Code:
0701

2nd Scenario: When I check this in terms of days then following is the result.
Code:
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

Last edited by RavinderSingh13; 08-31-2016 at 12:44 PM..
# 2  
Old 08-31-2016
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.*

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

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

Last edited by Corona688; 08-31-2016 at 01:25 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 08-31-2016
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):
Code:
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

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bug in Gnu date?

So as I write this today is two days after the clocks go back here in the UK. I have a script that worked last week. Yesterday it developed a bug. I eventually found the culprit is Gnu Date. $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04.5 LTS... (10 Replies)
Discussion started by: apmcd47
10 Replies

2. UNIX for Dummies Questions & Answers

Getting date -1 day not using GNU date

It's easy as pie to get the date minus one day on opensolaris: date -d "-1 day" +"%Y%m%d"run this command on our crappy Solaris 10 machines however (which I'm guessing doesn't have GNU date running on it) and you get: date: illegal option -- d date: illegal option -- 1 date: illegal option --... (5 Replies)
Discussion started by: rich@ardz
5 Replies

3. Linux

Date from GNU to BSD

Dear all, This should be simple but I cannot figure it out despite reading all the man pages. Could someone please help me translate this code (GNU date) to one that can be read by BSD date?: myDate=$(date -d "$h -$l days" +%Y/%m/%d), where h is a variable of the form DD/MM/YYYY, and l is... (3 Replies)
Discussion started by: thomchad
3 Replies

4. Programming

A question about Makefile run by GNU make

Hello everybody, Currently I'm learning how to build projects (C programming) with GNU make. I have a problem with one Makefile and I would appreciate if you could kindly give me a hand. Here is the environment: OS: Redhat linux 5 compiler: gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)... (2 Replies)
Discussion started by: dariyoosh
2 Replies

5. Shell Programming and Scripting

Howto: easy date range iteration/counting on GNU systems in the shell

Should work in any shell, but requires GNU date, although GNU date seems only to be happy for input dates between 1902 and 2037, inclusive (49673 days). Assume $a and $b hold two dates, e.g. set a=2010-03-27 set b=2010-04-04 Marginally faster: iterator: seq -f "$a +%1.0f days" 1 50000 |... (0 Replies)
Discussion started by: laddiebuck
0 Replies

6. UNIX for Dummies Questions & Answers

Another Date Question - Sorry

I have been out of the UNIX World for a number of years and none of my reference books seem to be helping e with this seemingly simple question. How can I manipulate the below command to reflect the previous day and not the current date? the command is: elm -s “SAP prs for `date... (4 Replies)
Discussion started by: McRae
4 Replies

7. Shell Programming and Scripting

Date Question

How could I get a date's day of the week (ie Sunday) without using the date -d command? So if a date was specified, it would give me the day of the week. The date -d command looks something like this date -d $inputdate | cut -c 1-3 the only problem with that statement is that it won't... (2 Replies)
Discussion started by: DKNUCKLES
2 Replies

8. UNIX for Dummies Questions & Answers

GNU Date

I know there are some posts on getting the time with milliseconds included and I realize unix may not be the best on this. I have seem some posts where its advised to install the GNU date. Any one know where I can download this as I am struggling to find it. Alternatively - if you have... (5 Replies)
Discussion started by: frustrated1
5 Replies

9. Shell Programming and Scripting

Date question

hi guys, i've got a simple one.. date_time="`date '+%Y-%m-%d %H:%M:%S'`" gives me the system's date and time... which my script will use this variable to run the data on this date... my question is... if now, i want to run the script for data's that are like 6 months back... how can i... (1 Reply)
Discussion started by: 12yearold
1 Replies

10. UNIX for Advanced & Expert Users

date question

hi, how do i get the last day of the current date or a given date using unix. for e.g. if i run the command/script on 1st feb 2002 i should get 28 th feb 2002. thanks !! AnkuR. (3 Replies)
Discussion started by: ankurgupta
3 Replies
Login or Register to Ask a Question