cancel
Showing results for 
Search instead for 
Did you mean: 

Date format

SOLVED
Go to solution
Fenglin
Regular Advisor

Date format

Hi

MTHLESS="$((`date +%m`-1))"

using the above command,I am able to get the current month -1, I would like to know whether it will work when it comes to January. Will I be able to get December as the output?

Please advice.

Regards
Feng Lin
13 REPLIES
Duncan Edmonstone
Honored Contributor

Re: Date format

That is just a simple arithmetic calculation, so in January it will resolve as follows:

01 - 1 = 0

Which I suspect is not what you want...

Why not install the GNU version of date which is available in the GNU coreutils package available here:

http://hpux.connect.org.uk/hppd/hpux/Gnu/coreutils-7.4/

(Don't miss the runtime dependencies you need too)

Once you have that you can easily do things like this:

# /usr/local/coreutils/bin/date +%m
07
# /usr/local/coreutils/bin/date -d "last month" +%m
06


HTH

Duncan

HTH

Duncan
Dennis Handly
Acclaimed Contributor

Re: Date format

>Will I be able to get December as the output?

You can get 12 if you do:
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi:

If the month is all that you want, you can do:

# awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Date format

>JRF: awk -v $(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?
Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> Dennis: I'm not sure how using awk is better than a real shell's builtin arithmetic expressions using clock arithmetic (%)?

I never said that is was. I'm merely pointing out TIMTOWDI ;-)

> Dennis: Also, I think you need to redirect stdin to /dev/null or add ";exit" to the end.

The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;print MON}'

Regards!

...JRF...
Fenglin
Regular Advisor

Re: Date format

Hi All

Thanks for the info.

Now I have the following situation : -

I have files with naming convention *yyyymmdd*. How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?

I intend to run the housekeeping job twice a month on the 1st and 16th of the month. 1st is to archive 16 - 31st day files, 16th is to archive 1 - 15th day files.

Thanks a lot.

Regards
Feng Lin
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> How am I going to take care of the zero(Jan - Sep) and one(Oct - Dec) for the month if I need to do housekeeping on those files?

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}'
06

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor
Solution

Re: Date format

>JRF: The BEGIN rule takes care of that _if_ I hadn't bungled the variable assignment:

I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN.
I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.

>How am I going to take care of the zero (fill) (Jan - Sep) and one(Oct - Dec) for the month

typeset -Z2 MTHLESS # zero fill
(( MTHLESS = ($(date +%m) + 11) % 12 + 1 ))
echo $MTHLESS
James R. Ferguson
Acclaimed Contributor

Re: Date format

Hi (again):

> Dennis: I guess the way awk works is that it doesn't bother reading the input files if there are no pattern-action statements, other than BEGIN. I don't know if this is documented, so I wouldn't depend on it. The standard seems to say that if NO actions and patterns, then the files aren't read.

I agree that the input file(s) are not read in the case of a BEGIN block with no pattern-action statements in the subsequent main block. This is easy to demonstrate.

Of course, this behavior doesn't occur if an END block is declared either alone or in concert with a BEGIN block. In that case, at least one file must be input.

I concede that relying on these subtitles may lead to confusion and/or false expectations. Hence it is probably best to amend the 'awk' snippet to:

# awk -v MON=$(date +%m) 'BEGIN{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null

...and then (similarly) this works too:

# awk -v MON=$(date +%m) 'END{MON-=1;MON=MON==0 ? 12 : MON;printf "%02d\n",MON}' /dev/null

Regards!

...JRF...
Fenglin
Regular Advisor

Re: Date format

Hi Dennis

I am using ur method,typeset etc.....

I should be getting 06 as the output for the current month,instead I am still getting 07.

Please advice.

Regards
Feng Lin
Dennis Handly
Acclaimed Contributor

Re: Date format

>I should be getting 06 as the output for the current month, instead I am still getting 07.

Oops, off by one:
(( MTHLESS = ($(date +%m) + (12-1-1)) % 12 + 1 ))

The extra -1 and +1 bias the 0..11 to 1..12. The first 12 is to prevent negative numbers because % is rem and not mod.
And of course the other -1, is the previous month.
Fenglin
Regular Advisor

Re: Date format

Hi All

Many thanks for your response. I have one last question.

I have 2 scripts, one running on the 1st, the other running on the 16th.

1st is to zip from the 16th to 31st day of the month and 16th is to zip from the 1st to 15th day of the month.

However,when the script 1st run for the next year, it won't be able to zip from the 16th to 31st day of last month.
For e.g, on 1st Jan 2010, the script won't be able to zip from the 16th dec files to 31st dec files.

How can my script take care of this last issue?

Regards
Feng Lin
Dennis Handly
Acclaimed Contributor

Re: Date format

>For e.g, on 1st Jan 2010, the script won't be able to zip from the 16th Dec files to 31st Dec files.
>How can my script take care of this last issue?

How does your script handle the previous month, if in the same year?
I assume you just check if Dec and subtract one from the year.

Have you looked at Clay's caljd script to date arithmetic?
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1232368
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1173224
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1158441
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1101623