For Programmers: Free Programming Magazines  


Home > Archive > AWK > September 2006 > Date differences in nawk









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 Date differences in nawk
Fix_Metal

2006-09-22, 3:56 am

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.

Ed Morton

2006-09-22, 7:56 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.
Thomas Weidenfeller

2006-09-22, 7:56 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
Fix_Metal

2006-09-23, 3:56 am

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

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com