Date differences in nawk

  • Follow


Hi all, I'm new posting in this NG. Welcome to myself then :)
I searched over the web for some infos to do this.
I need a function which would return the difference in days between 2
given dates.
I found a thousand bizarre methods. The most interestings are using GNU
date or GNU awk.
Unfortunatly I'm using a Solaris 8 system which have anything to do
with GNU...:)
This is my problem. I need to make something under nawk.
An example:
Given dates: (format: DD-MM-YY. I'm Italian, gh :)
TODAY: 22-09-06
GENERAL DATE: 1-09-06
The function should return 21 days.
Obviously I need something which calculates days over the years. I
already made something similar in the past, in some C formula, but I
can't find it atm....
any hints would be very pleased, since this is kinda urgent....
thanks in advance.

0
Reply fixmetal (3) 9/22/2006 10:02:53 AM

Fix_Metal wrote:
> Hi all, I'm new posting in this NG. Welcome to myself then :)
> I searched over the web for some infos to do this.
> I need a function which would return the difference in days between 2
> given dates.
> I found a thousand bizarre methods. The most interestings are using GNU
> date or GNU awk.
> Unfortunatly I'm using a Solaris 8 system which have anything to do
> with GNU...:)
> This is my problem. I need to make something under nawk.
> An example:
> Given dates: (format: DD-MM-YY. I'm Italian, gh :)
> TODAY: 22-09-06
> GENERAL DATE: 1-09-06
> The function should return 21 days.
> Obviously I need something which calculates days over the years. I
> already made something similar in the past, in some C formula, but I
> can't find it atm....
> any hints would be very pleased, since this is kinda urgent....
> thanks in advance.
> 

Just to rule out the easy answer: Why can't you install GNU awk? I use 
it on Solaris and it's well worth any small effort to install it. In 
this particular instance, the solution's trivial in gawk using it's time 
functions and a pain in nawk.

	Ed.
0
Reply Ed 9/22/2006 11:15:34 AM


Fix_Metal wrote:
> Hi all, I'm new posting in this NG. Welcome to myself then :)
> I searched over the web for some infos to do this.
> I need a function which would return the difference in days between 2
> given dates.
> I found a thousand bizarre methods. The most interestings are using GNU
> date or GNU awk.
> Unfortunatly I'm using a Solaris 8 system which have anything to do
> with GNU...:)
> This is my problem. I need to make something under nawk.
> An example:
> Given dates: (format: DD-MM-YY. I'm Italian, gh :)
> TODAY: 22-09-06
> GENERAL DATE: 1-09-06
> The function should return 21 days.
> Obviously I need something which calculates days over the years. I
> already made something similar in the past, in some C formula, but I
> can't find it atm....
> any hints would be very pleased, since this is kinda urgent....
> thanks in advance.


#
# Julian day number from a given date, at midday
#
function julian(y, m, d,        u, v, t, w) {
         y = int(y + 0)
         m = int(m + 0)
         d = int(d + 0)

         u = m - 3
         v = y
         if(m <= 2) {
                 u = m + 9
                 v = y - 1
         }
         t = int(v / 100)
         w = v % 100
         return int((146097 * t) / 4) + int((1461 * w) / 4) + int((153 * 
u + 2) / 5) + d + 1721119
}

/TODAY/ {
	split($2, t, "-")
	td = t[1]
	tm = t[2]
	ty = 2000 + t[3]
	next
}

/GENERAL DATE/ {
	split($3, g, "-")
	gd = g[1]
	gm = g[2]
	gy = 2000 + g[3]
	print julian(ty, tm, td) - julian(gy, gm, gd)
	next
}
	


/Thomas
0
Reply Thomas 9/22/2006 11:29:25 AM

Thank you!THis is exactly what I was looking for!

0
Reply Fix_Metal 9/23/2006 9:21:17 AM

3 Replies
728 Views

(page loaded in 0.278 seconds)

Similiar Articles:













7/25/2012 9:32:55 AM


Reply: