Unix Shell - Dates

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > Dates





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Dates
addanki007@gmail.com

2007-12-06, 1:29 pm

Do you have a sample code that calculates QTR_FIRST_MNTH_END and
QTR_LAST_MNTH_END if we pass 1Q07
output should be 2007-01-31 and 2007-03-31
Thanks in advance
Janis Papanagnou

2007-12-06, 1:29 pm

addanki007@gmail.com wrote:
> Do you have a sample code that calculates QTR_FIRST_MNTH_END and
> QTR_LAST_MNTH_END if we pass 1Q07


A less cryptic description would have been more appropriate.

Here's some sample code...

qy=1Q07
q=${qy%Q*}
y=20${qy#*Q}
m3=$((q*3))
m1=$((m3-2))
d1=$( cal $m1 $y | awk 'NF{x=$NF}END{print x}' )
d3=$( cal $m3 $y | awk 'NF{x=$NF}END{print x}' )
printf "%04d-%02d-%02d\n" $y $m1 $d1 $y $m3 $d3


Janis

> output should be 2007-01-31 and 2007-03-31
> Thanks in advance

Steffen Schuler

2007-12-06, 1:29 pm

On Thu, 06 Dec 2007 10:44:18 -0800, addanki007 wrote:

> Do you have a sample code that calculates QTR_FIRST_MNTH_END and
> QTR_LAST_MNTH_END if we pass 1Q07
> output should be 2007-01-31 and 2007-03-31 Thanks in advance


A tested awk script:

$ awk -v q="1Q07" '
BEGIN {
split("01-31,03-31:04-30,06-30:07-31,09-30:10-31,12-31", quarters, ":")
split(q, t, "Q")
split(quarters[t[1]], ts, ",")
printf "20%02d-%s 20%02d-%s\n", t[2], ts[1], t[2], ts[2]
}'
2007-01-31 2007-03-31
$


Regards,

Steffen "goedel" Schuler
William James

2007-12-06, 7:25 pm

On Dec 6, 12:44 pm, addanki...@gmail.com wrote:
> Do you have a sample code that calculates QTR_FIRST_MNTH_END and
> QTR_LAST_MNTH_END if we pass 1Q07
> output should be 2007-01-31 and 2007-03-31
> Thanks in advance


ruby -rdate -ne 'q,y=split(/Q/)
puts [2,0].map{|o|
Date.new(y.to_i+2000, q.to_i*3-o, -1)}'
Stephane Chazelas

2007-12-07, 1:37 am

On Thu, 6 Dec 2007 10:44:18 -0800 (PST), addanki007@gmail.com wrote:
> Do you have a sample code that calculates QTR_FIRST_MNTH_END and
> QTR_LAST_MNTH_END if we pass 1Q07
> output should be 2007-01-31 and 2007-03-31
> Thanks in advance


get_qtr_end_months() { # <quarter>Q<year> (qQyy)
input=$1
set 01-31 03-31 04-01 06-30 07-01 09-30 10-01 12-31

shift "$(((${input%Q*} - 1) * 2))"

qtr_first_mnth_end=20${input#*Q}-$1
qtr_last_mnth_end=20${input#*Q}-$2

echo "$qtr_first_mnth_end and $qtr_last_mnth_end"
}

--
Stephane
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com